Browse Source

Update organisation name retrieval to support date parameter in exports

pull/3056/head
Manny Dinssa 2 weeks ago
parent
commit
be76b0ab20
  1. 2
      app/models/organisation_name_change.rb
  2. 4
      app/services/exports/lettings_log_export_service.rb
  3. 15
      app/services/exports/organisation_export_service.rb
  4. 4
      app/services/exports/sales_log_export_service.rb
  5. 27
      spec/fixtures/exports/organisation_new_name.xml
  6. 45
      spec/services/exports/organisation_export_service_spec.rb

2
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

4
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

15
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

4
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

27
spec/fixtures/exports/organisation_new_name.xml vendored

@ -0,0 +1,27 @@
<?xml version="1.0" encoding="UTF-8"?>
<forms>
<form>
<id>{id}</id>
<name>MHCLG New</name>
<phone/>
<provider_type>1</provider_type>
<address_line1>2 Marsham Street</address_line1>
<address_line2>London</address_line2>
<postcode>SW1P 4DF</postcode>
<holds_own_stock>true</holds_own_stock>
<active>true</active>
<housing_registration_no>1234</housing_registration_no>
<old_org_id/>
<old_visible_id/>
<merge_date/>
<absorbing_organisation_id/>
<available_from/>
<deleted_at/>
<dsa_signed>true</dsa_signed>
<dsa_signed_at>{dsa_signed_at}</dsa_signed_at>
<dpo_email>{dpo_email}</dpo_email>
<profit_status/>
<group/>
<status>active</status>
</form>
</forms>

45
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!("<active>true</active>", "<active>false</active>")
expected_content.sub!("<merge_date/>", "<merge_date>#{organisation.merge_date.iso8601}</merge_date>")
expected_content.sub!("<status>active</status>", "<status>merged</status>")
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)

Loading…
Cancel
Save