From be76b0ab20834464b7cf8973996092ddcf9ac7ec Mon Sep 17 00:00:00 2001 From: Manny Dinssa <44172848+Dinssa@users.noreply.github.com> Date: Thu, 17 Apr 2025 15:01:33 +0100 Subject: [PATCH] Update organisation name retrieval to support date parameter in exports --- app/models/organisation_name_change.rb | 2 +- .../exports/lettings_log_export_service.rb | 4 +- .../exports/organisation_export_service.rb | 15 ++++++- .../exports/sales_log_export_service.rb | 4 +- .../exports/organisation_new_name.xml | 27 +++++++++++ .../organisation_export_service_spec.rb | 45 ++++++++++++++++++- 6 files changed, 89 insertions(+), 8 deletions(-) create mode 100644 spec/fixtures/exports/organisation_new_name.xml diff --git a/app/models/organisation_name_change.rb b/app/models/organisation_name_change.rb index 975394b5b..ed06d4daa 100644 --- a/app/models/organisation_name_change.rb +++ b/app/models/organisation_name_change.rb @@ -36,7 +36,7 @@ class OrganisationNameChange < ApplicationRecord end def includes_date?(date) - startdate <= date && (!next_change&.startdate || next_change.startdate > date) + startdate <= date.to_date && (!next_change&.startdate || next_change.startdate > date.to_date) end def next_change diff --git a/app/services/exports/lettings_log_export_service.rb b/app/services/exports/lettings_log_export_service.rb index c52bf8635..1ad44cbe4 100644 --- a/app/services/exports/lettings_log_export_service.rb +++ b/app/services/exports/lettings_log_export_service.rb @@ -57,12 +57,12 @@ module Exports # Organisation fields if lettings_log.owning_organisation attribute_hash["owningorgid"] = lettings_log.owning_organisation.old_visible_id || (lettings_log.owning_organisation.id + LOG_ID_OFFSET) - attribute_hash["owningorgname"] = lettings_log.owning_organisation.name + attribute_hash["owningorgname"] = lettings_log.owning_organisation.name(date: lettings_log.startdate) attribute_hash["hcnum"] = lettings_log.owning_organisation.housing_registration_no end if lettings_log.managing_organisation attribute_hash["maningorgid"] = lettings_log.managing_organisation.old_visible_id || (lettings_log.managing_organisation.id + LOG_ID_OFFSET) - attribute_hash["maningorgname"] = lettings_log.managing_organisation.name + attribute_hash["maningorgname"] = lettings_log.managing_organisation.name(date: lettings_log.startdate) attribute_hash["manhcnum"] = lettings_log.managing_organisation.housing_registration_no end diff --git a/app/services/exports/organisation_export_service.rb b/app/services/exports/organisation_export_service.rb index 8ceba93a9..7bceca840 100644 --- a/app/services/exports/organisation_export_service.rb +++ b/app/services/exports/organisation_export_service.rb @@ -28,10 +28,20 @@ module Exports def retrieve_resources(recent_export, full_update, _year) if !full_update && recent_export params = { from: recent_export.started_at, to: @start_time } - Organisation.where("(updated_at >= :from AND updated_at <= :to)", params) + + Organisation + .where(updated_at: params[:from]..params[:to]) + .or( + Organisation.where(id: OrganisationNameChange.where(created_at: params[:from]..params[:to]).select(:organisation_id)), + ) else params = { to: @start_time } - Organisation.where("updated_at <= :to", params) + + Organisation + .where("updated_at <= :to", params) + .or( + Organisation.where(id: OrganisationNameChange.where("created_at <= :to", params).select(:organisation_id)), + ) end end @@ -56,6 +66,7 @@ module Exports def apply_cds_transformation(organisation) attribute_hash = organisation.attributes + attribute_hash["name"] = organisation.name(date: Time.zone.now) attribute_hash["deleted_at"] = organisation.discarded_at&.iso8601 attribute_hash["dsa_signed"] = organisation.data_protection_confirmed? attribute_hash["dsa_signed_at"] = organisation.data_protection_confirmation&.signed_at&.iso8601 diff --git a/app/services/exports/sales_log_export_service.rb b/app/services/exports/sales_log_export_service.rb index fa806f6ab..9cac14207 100644 --- a/app/services/exports/sales_log_export_service.rb +++ b/app/services/exports/sales_log_export_service.rb @@ -57,9 +57,9 @@ module Exports attribute_hash["amendedbyid"] = sales_log.updated_by_id attribute_hash["owningorgid"] = sales_log.owning_organisation&.id - attribute_hash["owningorgname"] = sales_log.owning_organisation&.name + attribute_hash["owningorgname"] = sales_log.owning_organisation&.name(date: sales_log.saledate) attribute_hash["maningorgid"] = sales_log.managing_organisation&.id - attribute_hash["maningorgname"] = sales_log.managing_organisation&.name + attribute_hash["maningorgname"] = sales_log.managing_organisation&.name(date: sales_log.saledate) attribute_hash["creationmethod"] = sales_log.creation_method_before_type_cast attribute_hash["bulkuploadid"] = sales_log.bulk_upload_id diff --git a/spec/fixtures/exports/organisation_new_name.xml b/spec/fixtures/exports/organisation_new_name.xml new file mode 100644 index 000000000..d9ffedab0 --- /dev/null +++ b/spec/fixtures/exports/organisation_new_name.xml @@ -0,0 +1,27 @@ + + +
+ {id} + MHCLG New + + 1 + 2 Marsham Street + London + SW1P 4DF + true + true + 1234 + + + + + + + true + {dsa_signed_at} + {dpo_email} + + + active + +
diff --git a/spec/services/exports/organisation_export_service_spec.rb b/spec/services/exports/organisation_export_service_spec.rb index 6ef66161a..950647a93 100644 --- a/spec/services/exports/organisation_export_service_spec.rb +++ b/spec/services/exports/organisation_export_service_spec.rb @@ -6,6 +6,7 @@ RSpec.describe Exports::OrganisationExportService do let(:storage_service) { instance_double(Storage::S3Service) } let(:xml_export_file) { File.open("spec/fixtures/exports/organisation.xml", "r:UTF-8") } + let(:new_name_xml_export_file) { File.open("spec/fixtures/exports/organisation_new_name.xml", "r:UTF-8") } let(:local_manifest_file) { File.open("spec/fixtures/exports/manifest.xml", "r:UTF-8") } let(:expected_zip_filename) { "organisations_2024_2025_apr_mar_f0001_inc0001.zip" } @@ -16,7 +17,7 @@ RSpec.describe Exports::OrganisationExportService do def replace_entity_ids(organisation, export_template) export_template.sub!(/\{id\}/, organisation["id"].to_s) - export_template.sub!(/\{name\}/, organisation["name"]) + export_template.sub!(/\{name\}/, organisation.name(date: start_time)) export_template.sub!(/\{dsa_signed_at\}/, organisation.data_protection_confirmation&.signed_at&.iso8601) export_template.sub!(/\{dpo_email\}/, organisation.data_protection_confirmation&.data_protection_officer_email) end @@ -107,6 +108,48 @@ RSpec.describe Exports::OrganisationExportService do end end + context "and one organisation with a name change is available for export" do + let!(:organisation) { create(:organisation, name: "MHCLG", address_line1: "2 Marsham Street", address_line2: "London", postcode: "SW1P 4DF", housing_registration_no: "1234") } + let!(:organisation_name_change) { create(:organisation_name_change, organisation:, name: "MHCLG New", startdate: Time.zone.local(2022, 5, 1)) } + + it "generates an XML export file with the expected content within the ZIP file" do + expected_content = replace_entity_ids(organisation, new_name_xml_export_file.read) + + expect(storage_service).to receive(:write_file).with(expected_zip_filename, any_args) do |_, content| + entry = Zip::File.open_buffer(content).find_entry(expected_data_filename) + expect(entry).not_to be_nil + expect(entry.get_input_stream.read).to eq(expected_content) + end + + export_service.export_xml_organisations + end + + it "returns the list with correct archive" do + expect(export_service.export_xml_organisations).to eq({ expected_zip_filename.gsub(".zip", "") => start_time }) + end + + context "and the organisation is merged" do + let(:expected_content) { replace_entity_ids(organisation, new_name_xml_export_file.read) } + + before do + organisation.update!(merge_date: Time.zone.yesterday) + expected_content.sub!("true", "false") + expected_content.sub!("", "#{organisation.merge_date.iso8601}") + expected_content.sub!("active", "merged") + end + + it "generates an XML export file with the expected content within the ZIP file" do + expect(storage_service).to receive(:write_file).with(expected_zip_filename, any_args) do |_, content| + entry = Zip::File.open_buffer(content).find_entry(expected_data_filename) + expect(entry).not_to be_nil + expect(entry.get_input_stream.read).to eq(expected_content) + end + + export_service.export_xml_organisations + end + end + end + context "and multiple organisations are available for export" do before do create(:organisation)