diff --git a/app/services/csv/case_log_csv_service.rb b/app/services/csv/case_log_csv_service.rb index f092ba5b5..ff6474b80 100644 --- a/app/services/csv/case_log_csv_service.rb +++ b/app/services/csv/case_log_csv_service.rb @@ -29,27 +29,22 @@ module Csv end def set_csv_attributes - @attributes = CaseLog.attribute_names + %w[unittype_sh] - replace_csv_id_fields_with_names - order_csv_attributes + metadata_fields = %w[id status created_at updated_at created_by_name is_dpo owning_organisation_name managing_organisation_name] + metadata_id_fields = %w[managing_organisation_id owning_organisation_id created_by_id] + scheme_attributes = %w[scheme_id location_id] + intersecting_attributes = ordered_form_questions & CaseLog.attribute_names - scheme_attributes + remaining_attributes = CaseLog.attribute_names - intersecting_attributes - scheme_attributes - @attributes -= CSV_FIELDS_TO_OMIT if @user.present? && !@user.support? - end + @attributes = (metadata_fields + intersecting_attributes + remaining_attributes - metadata_id_fields + %w[unittype_sh] + scheme_attributes).uniq + move_is_inferred_fields - def replace_csv_id_fields_with_names - { "managing_organisation_id": "managing_organisation_name", "owning_organisation_id": "owning_organisation_name", "created_by_id": "created_by_name" }.each { |current, new_attribute| @attributes[@attributes.index(current.to_s)] = new_attribute } + @attributes -= CSV_FIELDS_TO_OMIT if @user.present? && !@user.support? end - def order_csv_attributes + def ordered_form_questions downloaded_form_years = CaseLog.all.map(&:collection_start_year).uniq.compact - downloaded_form_fields = downloaded_form_years.count == 1 && downloaded_form_years[0].present? ? FormHandler.instance.get_form("#{downloaded_form_years[0]}_#{downloaded_form_years[0] + 1}").questions : FormHandler.instance.forms.first.second.questions - ordered_default_form_questions = move_checkbox_answer_options(downloaded_form_fields) - - @attributes = (ordered_default_form_questions & @attributes) + (@attributes - ordered_default_form_questions) - move_metadata_fields_to_front - move_is_inferred_fields - move_scheme_fields_to_back + move_checkbox_answer_options(downloaded_form_fields) end def move_checkbox_answer_options(form_questions) @@ -64,26 +59,10 @@ module Csv attributes end - def move_metadata_fields_to_front - metadata_fields = %w[managing_organisation_name owning_organisation_name is_dpo created_by_name updated_at created_at status id] - move_csv_attributes(metadata_fields, 0) - end - def move_is_inferred_fields - { is_la_inferred: "la", is_previous_la_inferred: "prevloc" }.each do |field, inferred_field| - @attributes.delete(field.to_s) - @attributes.insert(@attributes.find_index(inferred_field), field.to_s) - end - end - - def move_scheme_fields_to_back - move_csv_attributes(%w[scheme_id location_id], -1) - end - - def move_csv_attributes(fields_to_move, index) - fields_to_move.each do |field| + { la: "is_la_inferred", prevloc: "is_previous_la_inferred" }.each do |inferred_field, field| @attributes.delete(field) - @attributes.insert(index, field) + @attributes.insert(@attributes.find_index(inferred_field.to_s), field) end end end diff --git a/spec/services/csv/case_log_csv_service_spec.rb b/spec/services/csv/case_log_csv_service_spec.rb new file mode 100644 index 000000000..1815b5ea2 --- /dev/null +++ b/spec/services/csv/case_log_csv_service_spec.rb @@ -0,0 +1,210 @@ +require "rails_helper" + +RSpec.describe Csv::CaseLogCsvService do + context "when the user is support" do + let(:user) { FactoryBot.create(:user, :support) } + let(:real_2021_2022_form) { Form.new("config/forms/2021_2022.json", "2021_2022") } + + before do + CaseLog.create!(startdate: "2021-10-10") + allow(FormHandler.instance).to receive(:get_form).and_return(real_2021_2022_form) + end + + it "sets csv attributes in correct order" do + expected_csv_attributes = %w[id + status + created_at + updated_at + created_by_name + is_dpo + owning_organisation_name + managing_organisation_name + needstype + renewal + startdate + rent_type + irproduct_other + tenancycode + propcode + postcode_known + postcode_full + is_la_inferred + la + first_time_property_let_as_social_housing + unitletas + rsnvac + offered + unittype_gn + builtype + wchair + beds + voiddate + void_date_value_check + majorrepairs + mrcdate + major_repairs_date_value_check + startertenancy + tenancy + tenancyother + tenancylength + sheltered + declaration + hhmemb + pregnancy_value_check + age1_known + age1 + sex1 + ethnic_group + ethnic + national + ecstat1 + retirement_value_check + details_known_2 + relat2 + age2_known + age2 + sex2 + ecstat2 + details_known_3 + relat3 + age3_known + age3 + sex3 + ecstat3 + details_known_4 + relat4 + age4_known + age4 + sex4 + ecstat4 + details_known_5 + relat5 + age5_known + age5 + sex5 + ecstat5 + details_known_6 + relat6 + age6_known + age6 + sex6 + ecstat6 + details_known_7 + relat7 + age7_known + age7 + sex7 + ecstat7 + details_known_8 + relat8 + age8_known + age8 + sex8 + ecstat8 + armedforces + leftreg + reservist + preg_occ + housingneeds_a + housingneeds_b + housingneeds_c + housingneeds_f + housingneeds_g + housingneeds_h + illness + illness_type_4 + illness_type_5 + illness_type_2 + illness_type_6 + illness_type_7 + illness_type_3 + illness_type_9 + illness_type_8 + illness_type_1 + illness_type_10 + illness_type_0 + layear + waityear + reason + reasonother + prevten + underoccupation_benefitcap + homeless + ppcodenk + ppostcode_full + previous_la_known + is_previous_la_inferred + prevloc + reasonpref + rp_homeless + rp_insan_unsat + rp_medwel + rp_hardship + rp_dontknow + cbl + cap + chr + letting_allocation_unknown + referral + net_income_known + earnings + incfreq + net_income_value_check + hb + benefits + household_charge + period + is_carehome + chcharge + brent + scharge + pscharge + supcharg + tcharge + rent_value_check + hbrentshortfall + tshortfall_known + tshortfall + property_owner_organisation + property_manager_organisation + sale_or_letting + purchaser_code + property_relet + incref + sale_completion_date + renttype + lettype + totchild + totelder + totadult + nocharge + ethnic_other + has_benefits + wrent + wscharge + wpschrge + wsupchrg + wtcharge + wtshortfall + refused + housingneeds + wchchrg + newprop + old_form_id + lar + irproduct + old_id + joint + hhtype + new_old + vacdays + housingneeds_type + housingneeds_other + unittype_sh + scheme_id + location_id] + csv = CSV.parse(described_class.new(user).to_csv) + expect(csv.first).to eq(expected_csv_attributes) + end + end +end