Browse Source

Allow updating mandatory collection resources

pull/2676/head
Kat 2 years ago
parent
commit
eaf3a3fed5
  1. 27
      app/controllers/collection_resources_controller.rb
  2. 6
      app/services/upload_collection_resources_service.rb
  3. 2
      app/views/collection_resources/_collection_resource_summary_list.erb
  4. 2
      app/views/collection_resources/edit.html.erb
  5. 3
      config/routes.rb
  6. 116
      spec/requests/collection_resources_controller_spec.rb
  7. 19
      spec/services/upload_collection_resources_service_spec.rb

27
app/controllers/collection_resources_controller.rb

@ -23,7 +23,7 @@ class CollectionResourcesController < ApplicationController
download_resource(resource.download_filename)
end
def update_mandatory_collection_resource
def edit
return render_not_found unless current_user.support?
year = params[:year].to_i
@ -39,6 +39,31 @@ class CollectionResourcesController < ApplicationController
render "collection_resources/edit"
end
def update
return render_not_found unless current_user.support?
year = resource_params[:year].to_i
resource_type = resource_params[:resource_type]
log_type = resource_params[:log_type]
file = resource_params[:file]
return render_not_found unless resource_for_year_can_be_updated?(year)
@collection_resource = MandatoryCollectionResourcesService.generate_resource(log_type, year, resource_type)
render_not_found unless @collection_resource
filename = @collection_resource.download_filename
begin
UploadCollectionResourcesService.upload_collection_resource(filename, file)
rescue StandardError
@collection_resource.errors.add(:file, "There was an error uploading this file.")
return render "collection_resources/edit"
end
flash[:notice] = "The #{log_type} #{text_year_range_format(year)} #{@collection_resource.short_display_name.downcase} has been updated"
redirect_to collection_resources_path
end
private
def resource_params

6
app/services/upload_collection_resources_service.rb

@ -0,0 +1,6 @@
class UploadCollectionResourcesService
def self.upload_collection_resource(filename, file)
storage_service = Storage::S3Service.new(Configuration::EnvConfigurationService.new, ENV["COLLECTION_RESOURCES_BUCKET"])
storage_service.write_file(filename, file)
end
end

2
app/views/collection_resources/_collection_resource_summary_list.erb

@ -10,7 +10,7 @@
<% end %>
<% row.with_action(
text: "Change",
href: update_mandatory_collection_resource_path(year: resource.year, log_type: resource.log_type, resource_type: resource.resource_type),
href: edit_mandatory_collection_resource_path(year: resource.year, log_type: resource.log_type, resource_type: resource.resource_type),
) %>
<% else %>
<% row.with_value do %>

2
app/views/collection_resources/edit.html.erb

@ -4,7 +4,7 @@
<div class="govuk-grid-row">
<div class="govuk-grid-column-two-thirds">
<%= form_with model: @collection_resource, scope: :form, url: update_mandatory_collection_resource_path, method: :patch do |f| %>
<%= form_with model: @collection_resource, url: update_mandatory_collection_resource_path, method: :patch do |f| %>
<%= f.hidden_field :year %>
<%= f.hidden_field :log_type %>
<%= f.hidden_field :resource_type %>

3
config/routes.rb

@ -42,7 +42,8 @@ Rails.application.routes.draw do
get "collection-resources", to: "collection_resources#index"
get "/collection-resources/:log_type/:year/:resource_type/download", to: "collection_resources#download_mandatory_collection_resource", as: :download_mandatory_collection_resource
get "/collection-resources/:log_type/:year/:resource_type/edit", to: "collection_resources#update_mandatory_collection_resource", as: :update_mandatory_collection_resource
get "/collection-resources/:log_type/:year/:resource_type/edit", to: "collection_resources#edit", as: :edit_mandatory_collection_resource
patch "/collection-resources", to: "collection_resources#update", as: :update_mandatory_collection_resource
resources :collection_resources, path: "/collection-resources" do
get "/download", to: "collection_resources#download_additional_collection_resource" # when we get to adding them

116
spec/requests/collection_resources_controller_spec.rb

@ -86,19 +86,19 @@ RSpec.describe CollectionResourcesController, type: :request do
it "displays change links" do
expect(page).to have_selector(:link_or_button, "Change", count: 12)
expect(page).to have_link("Change", href: update_mandatory_collection_resource_path(year: 2024, log_type: "lettings", resource_type: "paper_form"))
expect(page).to have_link("Change", href: update_mandatory_collection_resource_path(year: 2024, log_type: "lettings", resource_type: "bulk_upload_template"))
expect(page).to have_link("Change", href: update_mandatory_collection_resource_path(year: 2024, log_type: "lettings", resource_type: "bulk_upload_specification"))
expect(page).to have_link("Change", href: update_mandatory_collection_resource_path(year: 2024, log_type: "sales", resource_type: "paper_form"))
expect(page).to have_link("Change", href: update_mandatory_collection_resource_path(year: 2024, log_type: "sales", resource_type: "bulk_upload_template"))
expect(page).to have_link("Change", href: update_mandatory_collection_resource_path(year: 2024, log_type: "sales", resource_type: "bulk_upload_specification"))
expect(page).to have_link("Change", href: edit_mandatory_collection_resource_path(year: 2024, log_type: "lettings", resource_type: "paper_form"))
expect(page).to have_link("Change", href: edit_mandatory_collection_resource_path(year: 2024, log_type: "lettings", resource_type: "bulk_upload_template"))
expect(page).to have_link("Change", href: edit_mandatory_collection_resource_path(year: 2024, log_type: "lettings", resource_type: "bulk_upload_specification"))
expect(page).to have_link("Change", href: edit_mandatory_collection_resource_path(year: 2024, log_type: "sales", resource_type: "paper_form"))
expect(page).to have_link("Change", href: edit_mandatory_collection_resource_path(year: 2024, log_type: "sales", resource_type: "bulk_upload_template"))
expect(page).to have_link("Change", href: edit_mandatory_collection_resource_path(year: 2024, log_type: "sales", resource_type: "bulk_upload_specification"))
expect(page).to have_link("Change", href: update_mandatory_collection_resource_path(year: 2025, log_type: "lettings", resource_type: "paper_form"))
expect(page).to have_link("Change", href: update_mandatory_collection_resource_path(year: 2025, log_type: "lettings", resource_type: "bulk_upload_template"))
expect(page).to have_link("Change", href: update_mandatory_collection_resource_path(year: 2025, log_type: "lettings", resource_type: "bulk_upload_specification"))
expect(page).to have_link("Change", href: update_mandatory_collection_resource_path(year: 2025, log_type: "sales", resource_type: "paper_form"))
expect(page).to have_link("Change", href: update_mandatory_collection_resource_path(year: 2025, log_type: "sales", resource_type: "bulk_upload_template"))
expect(page).to have_link("Change", href: update_mandatory_collection_resource_path(year: 2025, log_type: "sales", resource_type: "bulk_upload_specification"))
expect(page).to have_link("Change", href: edit_mandatory_collection_resource_path(year: 2025, log_type: "lettings", resource_type: "paper_form"))
expect(page).to have_link("Change", href: edit_mandatory_collection_resource_path(year: 2025, log_type: "lettings", resource_type: "bulk_upload_template"))
expect(page).to have_link("Change", href: edit_mandatory_collection_resource_path(year: 2025, log_type: "lettings", resource_type: "bulk_upload_specification"))
expect(page).to have_link("Change", href: edit_mandatory_collection_resource_path(year: 2025, log_type: "sales", resource_type: "paper_form"))
expect(page).to have_link("Change", href: edit_mandatory_collection_resource_path(year: 2025, log_type: "sales", resource_type: "bulk_upload_template"))
expect(page).to have_link("Change", href: edit_mandatory_collection_resource_path(year: 2025, log_type: "sales", resource_type: "bulk_upload_specification"))
end
end
@ -196,10 +196,10 @@ RSpec.describe CollectionResourcesController, type: :request do
end
end
describe "GET #update_mandatory_collection_resource" do
describe "GET #edit_mandatory_collection_resource" do
context "when user is not signed in" do
it "redirects to the sign in page" do
get update_mandatory_collection_resource_path(year: 2024, log_type: "sales", resource_type: "bulk_upload_template")
get edit_mandatory_collection_resource_path(year: 2024, log_type: "sales", resource_type: "bulk_upload_template")
expect(response).to redirect_to(new_user_session_path)
end
end
@ -212,7 +212,7 @@ RSpec.describe CollectionResourcesController, type: :request do
end
it "returns page not found" do
get update_mandatory_collection_resource_path(year: 2024, log_type: "sales", resource_type: "bulk_upload_template")
get edit_mandatory_collection_resource_path(year: 2024, log_type: "sales", resource_type: "bulk_upload_template")
expect(response).to have_http_status(:not_found)
end
end
@ -225,7 +225,7 @@ RSpec.describe CollectionResourcesController, type: :request do
end
it "returns page not found" do
get update_mandatory_collection_resource_path(year: 2024, log_type: "sales", resource_type: "bulk_upload_template")
get edit_mandatory_collection_resource_path(year: 2024, log_type: "sales", resource_type: "bulk_upload_template")
expect(response).to have_http_status(:not_found)
end
end
@ -240,7 +240,7 @@ RSpec.describe CollectionResourcesController, type: :request do
end
it "displays update collection resources page content" do
get update_mandatory_collection_resource_path(year: 2024, log_type: "sales", resource_type: "bulk_upload_template")
get edit_mandatory_collection_resource_path(year: 2024, log_type: "sales", resource_type: "bulk_upload_template")
expect(page).to have_content("Sales 2024 to 2025")
expect(page).to have_content("Change the bulk upload template")
@ -252,4 +252,86 @@ RSpec.describe CollectionResourcesController, type: :request do
end
end
end
describe "PATCH #update_mandatory_collection_resource" do
let(:some_file) { File.open(file_fixture("blank_bulk_upload_sales.csv")) }
let(:params) { { collection_resource: { year: 2024, log_type: "sales", resource_type: "bulk_upload_template", file: some_file } } }
before do
allow(UploadCollectionResourcesService).to receive(:upload_collection_resource)
end
context "when user is not signed in" do
it "redirects to the sign in page" do
patch update_mandatory_collection_resource_path(year: 2024, log_type: "sales", resource_type: "bulk_upload_template", file: some_file)
expect(response).to redirect_to(new_user_session_path)
end
end
context "when user is signed in as a data coordinator" do
let(:user) { create(:user, :data_coordinator) }
before do
sign_in user
end
it "returns page not found" do
patch update_mandatory_collection_resource_path, params: params
expect(response).to have_http_status(:not_found)
end
end
context "when user is signed in as a data provider" do
let(:user) { create(:user, :data_provider) }
before do
sign_in user
end
it "returns page not found" do
patch update_mandatory_collection_resource_path, params: params
expect(response).to have_http_status(:not_found)
end
end
context "when user is signed in as a support user" do
let(:user) { create(:user, :support) }
before do
allow(Time.zone).to receive(:today).and_return(Time.zone.local(2025, 1, 8))
allow(user).to receive(:need_two_factor_authentication?).and_return(false)
# rubocop:disable RSpec/AnyInstance
allow_any_instance_of(CollectionResourcesHelper).to receive(:editable_collection_resource_years).and_return([2024, 2025])
# rubocop:enable RSpec/AnyInstance
sign_in user
end
it "correcty updates a sales file" do
params = { collection_resource: { year: 2024, log_type: "sales", resource_type: "bulk_upload_template", file: some_file } }
patch update_mandatory_collection_resource_path, params: params
expect(response).to redirect_to(collection_resources_path)
expect(UploadCollectionResourcesService).to have_received(:upload_collection_resource).with("bulk-upload-sales-template-2024-25.xlsx", anything)
expect(flash[:notice]).to eq("The sales 2024 to 2025 bulk upload template has been updated")
end
it "correcty updates a lettings file" do
params = { collection_resource: { year: 2025, log_type: "lettings", resource_type: "paper_form", file: some_file } }
patch update_mandatory_collection_resource_path, params: params
expect(response).to redirect_to(collection_resources_path)
expect(UploadCollectionResourcesService).to have_received(:upload_collection_resource).with("2025_26_lettings_paper_form.pdf", anything)
expect(flash[:notice]).to eq("The lettings 2025 to 2026 paper form has been updated")
end
it "displays error message if the upload fails" do
allow(UploadCollectionResourcesService).to receive(:upload_collection_resource).and_raise(StandardError)
params = { collection_resource: { year: 2025, log_type: "lettings", resource_type: "paper_form", file: some_file } }
patch update_mandatory_collection_resource_path, params: params
expect(page).to have_content("There was an error uploading this file.")
end
end
end
end

19
spec/services/upload_collection_resources_service_spec.rb

@ -0,0 +1,19 @@
require "rails_helper"
describe UploadCollectionResourcesService do
let(:service) { described_class }
let(:some_file) { File.open(file_fixture("blank_bulk_upload_sales.csv")) }
let(:storage_service) { instance_double(Storage::S3Service) }
describe "#upload_collection_resource" do
before do
allow(Storage::S3Service).to receive(:new).and_return(storage_service)
allow(storage_service).to receive(:write_file)
end
it "calls write_file on S3 service" do
expect(storage_service).to receive(:write_file).with("2025_26_lettings_paper_form.pdf", some_file)
service.upload_collection_resource("2025_26_lettings_paper_form.pdf", some_file)
end
end
end
Loading…
Cancel
Save