diff --git a/lib/tasks/correct_illness_from_csv.rake b/lib/tasks/correct_illness_from_csv.rake index 08fab4e17..2fc3eec74 100644 --- a/lib/tasks/correct_illness_from_csv.rake +++ b/lib/tasks/correct_illness_from_csv.rake @@ -12,4 +12,50 @@ namespace :correct_illness do Rails.logger.error("Organisation with ID #{organisation_id} not found") end end + + desc "Export data CSVs for import into Central Data System (CDS)" + task :correct_illness_from_csv, %i[file_name] => :environment do |_task, args| + file_name = args[:file_name] + + raise "Usage: rake correct_illness:correct_illness_from_csv['csv_file_name']" if file_name.blank? + + s3_service = Storage::S3Service.new(PlatformHelper.is_paas? ? Configuration::PaasConfigurationService.new : Configuration::EnvConfigurationService.new, ENV["IMPORT_PAAS_INSTANCE"]) + illness_csv = CSV.parse(s3_service.get_file_io(file_name), headers: false) + + illness_csv.each_with_index do |row, index| + next if index < 3 + + lettings_log_id = row[1] + + if lettings_log_id.blank? + Rails.logger.info("Lettings log ID not provided") + next + end + + lettings_log = LettingsLog.find_by(id: lettings_log_id) + if lettings_log.blank? + Rails.logger.info("Could not find a lettings log with id #{lettings_log_id}") + next + end + + lettings_log.illness = row[8] + lettings_log.illness_type_1 = row[9].presence || 0 + lettings_log.illness_type_2 = row[10].presence || 0 + lettings_log.illness_type_3 = row[11].presence || 0 + lettings_log.illness_type_4 = row[12].presence || 0 + lettings_log.illness_type_5 = row[13].presence || 0 + lettings_log.illness_type_6 = row[14].presence || 0 + lettings_log.illness_type_7 = row[15].presence || 0 + lettings_log.illness_type_8 = row[16].presence || 0 + lettings_log.illness_type_9 = row[17].presence || 0 + lettings_log.illness_type_10 = row[18].presence || 0 + lettings_log.values_updated_at = Time.zone.now + + if lettings_log.save + Rails.logger.info("Updated lettings log #{lettings_log_id}, with illness: #{([lettings_log.illness] + %w[illness_type_1 illness_type_2 illness_type_3 illness_type_4 illness_type_5 illness_type_6 illness_type_7 illness_type_8 illness_type_9 illness_type_10].select { |illness_type| lettings_log[illness_type] == 1 }).join(', ')}") + else + Rails.logger.error("Validation failed for lettings log with ID #{lettings_log.id}: #{lettings_log.errors.full_messages.join(', ')}}") + end + end + end end diff --git a/spec/fixtures/files/illness_update.csv b/spec/fixtures/files/illness_update.csv new file mode 100644 index 000000000..765f338f6 --- /dev/null +++ b/spec/fixtures/files/illness_update.csv @@ -0,0 +1,8 @@ +Question,Log ID,Tenancy start date,Tenant code,Property reference,Log owner,Owning organisation,Managing organisation,Does anybody in the household have a physical or mental health condition (or other illness) expected to last 12 months or more?,Does this person's condition affect their vision?,Does this person's condition affect their hearing?,Does this person's condition affect their mobility?,Does this person's condition affect their dexterity?,Does this person's condition affect their learning or understanding or concentrating?,Does this person's condition affect their memory?,Does this person's condition affect their mental health?,Does this person's condition affect their stamina or breathing or fatigue?,Does this person's condition affect them socially or behaviourally?,Does this person's condition affect them in another way? +Additional info,,,,,,,,,"For example, blindness or partial sight","For example, deafness or partial hearing",,"For example, lifting and carrying objects, or using a keyboard",,,"For example, depression or anxiety",,"Anything associated with autism spectrum disorder (ASD), including Asperger's or attention deficit hyperactivity disorder (ADHD)",, +How to answer,Do not change the answers for this field,Do not change the answers for this field,Do not change the answers for this field,Do not change the answers for this field,Do not change the answers for this field,Do not change the answers for this field,Do not change the answers for this field,1 = Yes; 2 = No; 3 = Don't know,1 = Yes; blank = No,1 = Yes; blank = No,1 = Yes; blank = No,1 = Yes; blank = No,1 = Yes; blank = No,1 = Yes; blank = No,1 = Yes; blank = No,1 = Yes; blank = No,1 = Yes; blank = No,1 = Yes; blank = No +,{id},2023-04-05,tenancycode1,propcode1,testy@example.com,Illness org,Illness org,1,,1,,,,,,,, +,{id2},2023-04-05,tenancycode1,propcode1,testy@example.com,Illness org,Illness org,2,,,,,,,,,, +,{id3},2023-04-05,tenancycode1,propcode1,testy@example.com,Illness org,Illness org,3,,,,,,,,,, +,,2023-04-05,tenancycode1,propcode1,testy@example.com,Illness org,Illness org,3,,,,,,,,,, +,fake_id,2023-04-05,tenancycode1,propcode1,testy@example.com,Illness org,Illness org,3,,,,,,,,,, diff --git a/spec/lib/tasks/correct_illness_from_csv_spec.rb b/spec/lib/tasks/correct_illness_from_csv_spec.rb index 4f4b1a95d..ef7e82a65 100644 --- a/spec/lib/tasks/correct_illness_from_csv_spec.rb +++ b/spec/lib/tasks/correct_illness_from_csv_spec.rb @@ -40,4 +40,139 @@ RSpec.describe "correct_illness" do end end end + + describe ":correct_illness_from_csv", type: :task do + def replace_entity_ids(lettings_log, second_lettings_log, third_lettings_log, export_template) + export_template.sub!(/\{id\}/, lettings_log.id.to_s) + export_template.sub!(/\{id2\}/, second_lettings_log.id.to_s) + export_template.sub!(/\{id3\}/, third_lettings_log.id.to_s) + end + + subject(:task) { Rake::Task["correct_illness:correct_illness_from_csv"] } + + let(:instance_name) { "paas_import_instance" } + let(:storage_service) { instance_double(Storage::S3Service) } + let(:env_config_service) { instance_double(Configuration::EnvConfigurationService) } + let(:paas_config_service) { instance_double(Configuration::PaasConfigurationService) } + + before do + allow(Storage::S3Service).to receive(:new).and_return(storage_service) + allow(Configuration::EnvConfigurationService).to receive(:new).and_return(env_config_service) + allow(Configuration::PaasConfigurationService).to receive(:new).and_return(paas_config_service) + allow(ENV).to receive(:[]) + allow(ENV).to receive(:[]).with("IMPORT_PAAS_INSTANCE").and_return(instance_name) + allow(ENV).to receive(:[]).with("VCAP_SERVICES").and_return("dummy") + + Rake.application.rake_require("tasks/correct_illness_from_csv") + Rake::Task.define_task(:environment) + task.reenable + end + + context "when the rake task is run" do + let(:illness_csv_path) { "illness_123.csv" } + let(:wrong_file_path) { "/test/no_csv_here.csv" } + let!(:lettings_log) do + create(:lettings_log, + :completed, + :with_illness_without_type) + end + + let!(:second_lettings_log) do + create(:lettings_log, + :completed, + illness: 1, + illness_type_1: true, + illness_type_2: true) + end + + let!(:third_lettings_log) do + create(:lettings_log, + :completed, + illness: 1, + illness_type_3: true, + illness_type_4: true) + end + + before do + allow(storage_service).to receive(:get_file_io) + .with("illness_123.csv") + .and_return(replace_entity_ids(lettings_log, second_lettings_log, third_lettings_log, File.open("./spec/fixtures/files/illness_update.csv").read)) + end + + it "sets illness to yes and sets correct illness type" do + task.invoke(illness_csv_path) + lettings_log.reload + expect(lettings_log.illness).to eq(1) + expect(lettings_log.illness_type_2).to eq(1) + %w[illness_type_1 + illness_type_3 + illness_type_4 + illness_type_5 + illness_type_6 + illness_type_7 + illness_type_8 + illness_type_9 + illness_type_10].each do |illness_type| + expect(lettings_log[illness_type]).to eq(0) + end + end + + it "sets illness to no" do + task.invoke(illness_csv_path) + second_lettings_log.reload + expect(second_lettings_log.illness).to eq(2) + %w[illness_type_1 + illness_type_2 + illness_type_3 + illness_type_4 + illness_type_5 + illness_type_6 + illness_type_7 + illness_type_8 + illness_type_9 + illness_type_10].each do |illness_type| + expect(second_lettings_log[illness_type]).to eq(nil) + end + end + + it "sets illness to don't know" do + task.invoke(illness_csv_path) + third_lettings_log.reload + expect(third_lettings_log.illness).to eq(3) + %w[illness_type_1 + illness_type_2 + illness_type_3 + illness_type_4 + illness_type_5 + illness_type_6 + illness_type_7 + illness_type_8 + illness_type_9 + illness_type_10].each do |illness_type| + expect(third_lettings_log[illness_type]).to eq(nil) + end + end + + it "logs the progress of the update" do + expect(Rails.logger).to receive(:info).with("Updated lettings log #{lettings_log.id}, with illness: 1, illness_type_2") + expect(Rails.logger).to receive(:info).with("Updated lettings log #{second_lettings_log.id}, with illness: 2") + expect(Rails.logger).to receive(:info).with("Updated lettings log #{third_lettings_log.id}, with illness: 3") + expect(Rails.logger).to receive(:info).with("Lettings log ID not provided") + expect(Rails.logger).to receive(:info).with("Could not find a lettings log with id fake_id") + + task.invoke(illness_csv_path) + end + + it "raises an error when no path is given" do + expect { task.invoke(nil) }.to raise_error(RuntimeError, "Usage: rake correct_illness:correct_illness_from_csv['csv_file_name']") + end + + it "logs an error if a validation fails" do + lettings_log.postcode_full = "invalid_format" + lettings_log.save!(validate: false) + expect(Rails.logger).to receive(:error).with(/Validation failed for lettings log with ID #{lettings_log.id}: Postcode full/) + task.invoke(illness_csv_path) + end + end + end end