diff --git a/.env.test b/.env.test index 2ba1222ba..98b819be9 100644 --- a/.env.test +++ b/.env.test @@ -1,2 +1,3 @@ APP_HOST="http://localhost:3000" OS_DATA_KEY=OS_DATA_KEY +SKIP_UPRN_ISSUE_ORG_IDS=[] diff --git a/app/services/csv/missing_addresses_csv_service.rb b/app/services/csv/missing_addresses_csv_service.rb index 28ca72878..df800a19e 100644 --- a/app/services/csv/missing_addresses_csv_service.rb +++ b/app/services/csv/missing_addresses_csv_service.rb @@ -18,12 +18,16 @@ module Csv .where.not(old_form_id: nil) .where.not(address_line1: nil) - logs_with_wrong_uprn = @organisation.managed_lettings_logs - .imported - .filter_by_year(2023) - .where(needstype: 1) - .where.not(uprn: nil) - .where("uprn = propcode OR uprn = tenancycode or town_or_city = 'Bristol'") + logs_with_wrong_uprn = if JSON.parse(ENV["SKIP_UPRN_ISSUE_ORG_IDS"]).include?(@organisation.id) + [] + else + @organisation.managed_lettings_logs + .imported + .filter_by_year(2023) + .where(needstype: 1) + .where.not(uprn: nil) + .where("uprn = propcode OR uprn = tenancycode or town_or_city = 'Bristol'") + end return if logs_with_missing_addresses.empty? && logs_with_missing_town_or_city.empty? && logs_with_wrong_uprn.empty? @@ -100,12 +104,15 @@ module Csv .where.not(old_form_id: nil) .where.not(address_line1: nil) - logs_with_wrong_uprn = @organisation.sales_logs - .imported - .filter_by_year(2023) - .where.not(uprn: nil) - .where("uprn = purchid or town_or_city = 'Bristol'") - + logs_with_wrong_uprn = if JSON.parse(ENV["SKIP_UPRN_ISSUE_ORG_IDS"]).include?(@organisation.id) + [] + else + @organisation.sales_logs + .imported + .filter_by_year(2023) + .where.not(uprn: nil) + .where("uprn = purchid or town_or_city = 'Bristol'") + end return if logs_with_missing_addresses.empty? && logs_with_missing_town_or_city.empty? && logs_with_wrong_uprn.empty? CSV.generate(headers: true) do |csv| diff --git a/lib/tasks/send_missing_addresses_csv.rake b/lib/tasks/send_missing_addresses_csv.rake index a187275e3..dbadf8463 100644 --- a/lib/tasks/send_missing_addresses_csv.rake +++ b/lib/tasks/send_missing_addresses_csv.rake @@ -19,13 +19,16 @@ namespace :correct_addresses do .where.not(old_form_id: nil) .where.not(address_line1: nil).count - logs_impacted_by_uprn_issue = organisation.managed_lettings_logs - .imported - .filter_by_year(2023) - .where(needstype: 1) - .where.not(uprn: nil) - .where("uprn = propcode OR uprn = tenancycode or town_or_city = 'Bristol'") - + logs_impacted_by_uprn_issue = if JSON.parse(ENV["SKIP_UPRN_ISSUE_ORG_IDS"]).include?(organisation.id) + [] + else + organisation.managed_lettings_logs + .imported + .filter_by_year(2023) + .where(needstype: 1) + .where.not(uprn: nil) + .where("uprn = propcode OR uprn = tenancycode or town_or_city = 'Bristol'") + end if logs_impacted_by_missing_address >= MISSING_ADDRESSES_THRESHOLD || logs_impacted_by_missing_town_or_city >= MISSING_ADDRESSES_THRESHOLD || logs_impacted_by_uprn_issue.any? data_coordinators = organisation.users.where(role: 2).filter_by_active users_to_contact = data_coordinators.any? ? data_coordinators : organisation.users.filter_by_active diff --git a/spec/lib/tasks/send_missing_addresses_csv_spec.rb b/spec/lib/tasks/send_missing_addresses_csv_spec.rb index ccc975d97..9181c1036 100644 --- a/spec/lib/tasks/send_missing_addresses_csv_spec.rb +++ b/spec/lib/tasks/send_missing_addresses_csv_spec.rb @@ -207,6 +207,17 @@ RSpec.describe "correct_addresses" do task.invoke end end + + context "when org is included in SKIP_UPRN_ISSUE_ORG_IDS list" do + before do + create_list(:lettings_log, 5, :imported, startdate: Time.zone.local(2023, 9, 9), uprn: "12", propcode: "12", needstype: 1, owning_organisation: organisation, managing_organisation: organisation, created_by: organisation.users.first) + allow(ENV).to receive(:[]).with("SKIP_UPRN_ISSUE_ORG_IDS").and_return([organisation.id].to_json) + end + + it "does not enqueue the job" do + expect { task.invoke }.not_to enqueue_job(EmailMissingAddressesCsvJob) + end + end end end end diff --git a/spec/services/csv/missing_addresses_csv_service_spec.rb b/spec/services/csv/missing_addresses_csv_service_spec.rb index 97eba9853..7071d41e0 100644 --- a/spec/services/csv/missing_addresses_csv_service_spec.rb +++ b/spec/services/csv/missing_addresses_csv_service_spec.rb @@ -136,6 +136,16 @@ RSpec.describe Csv::MissingAddressesCsvService do csv = service.create_missing_lettings_addresses_csv expect(csv).to eq(expected_content) end + + context "and the organisation is in the SKIP_UPRN_ISSUE_ORG_IDS list" do + before do + allow(ENV).to receive(:[]).with("SKIP_UPRN_ISSUE_ORG_IDS").and_return([organisation.id].to_json) + end + + it "returns nil" do + expect(service.create_missing_lettings_addresses_csv).to be_nil + end + end end context "when the organisation only has supported housing logs with missing addresses or town or city" do @@ -265,6 +275,16 @@ RSpec.describe Csv::MissingAddressesCsvService do csv = service.create_missing_sales_addresses_csv expect(csv).to eq(expected_content) end + + context "and the organisation is in the SKIP_UPRN_ISSUE_ORG_IDS list" do + before do + allow(ENV).to receive(:[]).with("SKIP_UPRN_ISSUE_ORG_IDS").and_return([organisation.id].to_json) + end + + it "returns nil" do + expect(service.create_missing_sales_addresses_csv).to be_nil + end + end end context "when the organisation only has logs with missing addresses from 2022" do