Browse Source

Update sales csv with 2025 mappings

pull/3021/head
Manny Dinssa 1 month ago
parent
commit
93c470f90d
  1. 148
      app/services/csv/sales_log_csv_service.rb
  2. 20
      spec/services/csv/sales_log_csv_service_spec.rb

148
app/services/csv/sales_log_csv_service.rb

@ -68,6 +68,30 @@ module Csv
labels: %i[monthly_charges_value_check], labels: %i[monthly_charges_value_check],
codes: %i[monthly_charges_value_check], codes: %i[monthly_charges_value_check],
}, },
stairlastday: {
labels: %i[lasttransaction day],
codes: %i[lasttransaction day],
},
stairlastmonth: {
labels: %i[lasttransaction month],
codes: %i[lasttransaction month],
},
stairlastyear: {
labels: %i[lasttransaction year],
codes: %i[lasttransaction year],
},
stairinitialday: {
labels: %i[initialpurchase day],
codes: %i[initialpurchase day],
},
stairinitialmonth: {
labels: %i[initialpurchase month],
codes: %i[initialpurchase month],
},
stairinitialyear: {
labels: %i[initialpurchase year],
codes: %i[initialpurchase year],
},
}.freeze }.freeze
PERSON_DETAILS = {}.tap { |hash| PERSON_DETAILS = {}.tap { |hash|
@ -152,6 +176,18 @@ module Csv
"uprn_confirmed" => "UPRNCONFIRMED", "uprn_confirmed" => "UPRNCONFIRMED",
}.freeze }.freeze
SUPPORT_ATTRIBUTE_NAME_MAPPINGS_2025 = {
"hholdcount" => "HHOLDCOUNT",
"created_by_id" => "CREATEDBYID",
"owning_organisation_id" => "OWNINGORGID",
"managing_organisation_id" => "MANINGORGID",
"assigned_to_id" => "USERNAMEID",
"updated_by" => "AMENDEDBY",
"updated_by_id" => "AMENDEDBYID",
"has_management_fee" => "HASESTATEFEE",
"management_fee" => "ESTATEFEE",
}.freeze
UPRN_CONFIRMED_LABELS = { UPRN_CONFIRMED_LABELS = {
0 => "No", 0 => "No",
1 => "Yes", 1 => "Yes",
@ -161,20 +197,56 @@ module Csv
"uprn_confirmed" => UPRN_CONFIRMED_LABELS, "uprn_confirmed" => UPRN_CONFIRMED_LABELS,
}.freeze }.freeze
ATTRIBUTE_MAPPINGS = {
"saledate" => %w[day month year],
"exdate" => %w[exday exmonth exyear],
"hodate" => %w[hoday homonth hoyear],
"ppostcode_full" => %w[ppostc1 ppostc2],
"la" => %w[la la_label],
"prevloc" => %w[prevloc prevloc_label],
"assigned_to_id" => %w[created_by assigned_to],
"owning_organisation_id" => %w[owning_organisation_name],
"managing_organisation_id" => %w[managing_organisation_name],
"value" => %w[value value_value_check],
"mscharge" => %w[mscharge mscharge_value_check],
}.freeze
ATTRIBUTE_MAPPINGS_2024 = {
"uprn" => %w[uprn uprn_confirmed address_line1_input postcode_full_input uprn_selection],
}.freeze
ATTRIBUTE_MAPPINGS_2025 = {
"duplicate_set_id" => %w[duplicate_set_id owning_organisation_name owning_organisation_id managing_organisation_name managing_organisation_id],
"created_by_id" => %w[created_by created_by_id assigned_to assigned_to_id],
"updated_by_id" => %w[updated_by updated_by_id],
"bulk_upload_id" => %w[bulk_upload_id collection_start_year],
"prevten" => %w[hhtype prevten],
"mrent" => %w[mrent has_management_fee management_fee],
"lasttransaction" => %w[stairlastday stairlastmonth stairlastyear],
"initialpurchase" => %w[stairinitialday stairinitialmonth stairinitialyear],
"mrentprestaircasing" => %w[mrentprestaircasing grant discount extrabor has_mscharge mscharge mscharge_value_check],
}.freeze
def formatted_attribute_headers def formatted_attribute_headers
return @attributes unless @user.support? return @attributes unless @user.support?
mappings = SUPPORT_ATTRIBUTE_NAME_MAPPINGS
mappings = mappings.merge(SUPPORT_ATTRIBUTE_NAME_MAPPINGS_2025) if @year == 2025
@attributes.map do |attribute| @attributes.map do |attribute|
SUPPORT_ATTRIBUTE_NAME_MAPPINGS[attribute] || attribute.upcase mappings[attribute] || attribute.upcase
end end
end end
def sales_log_attributes def sales_log_attributes
ordered_questions = FormHandler.instance.ordered_questions_for_year(@year, "sales") ordered_questions = FormHandler.instance.ordered_questions_for_year(@year, "sales")
ordered_questions.reject! { |q| q.id.match?(/((?<!la)_known)|(_check)|(_asked)|nationality_all_group|nationality_all_buyer2_group/) } ordered_questions.reject! { |q| q.id.match?(/((?<!la)_known)|(_check)|(_asked)|nationality_all_group|nationality_all_buyer2_group/) }
attributes = insert_derived_and_related_attributes(ordered_questions) ordered_questions.reject! { |q| q.id.match?(/organisation_id|created_by|assigned_to|soctenant|has_management_fee|management_fee|grant|discount|has_mscharge|mscharge|extrabor/) } if @year >= 2025
order_address_fields_for_support(attributes) # binding.pry
final_attributes = non_question_fields + attributes attributes = insert_checkbox_options(ordered_questions)
final_attributes = insert_derived_and_related_attributes(non_question_fields + attributes)
order_address_fields_for_support(final_attributes)
# final_attributes = non_question_fields + attributes
@user.support? ? final_attributes : final_attributes - SUPPORT_ONLY_ATTRIBUTES @user.support? ? final_attributes : final_attributes - SUPPORT_ONLY_ATTRIBUTES
end end
@ -187,38 +259,64 @@ module Csv
}.compact }.compact
end end
def insert_derived_and_related_attributes(ordered_questions) def insert_checkbox_options(ordered_questions)
ordered_questions.flat_map do |question| ordered_questions.flat_map do |question|
if question.type == "checkbox" if question.type == "checkbox"
question.answer_options.keys question.answer_options.keys
elsif attribute_mappings.key? question.id
attribute_mappings[question.id]
else else
question.id question.id
end end
end end
end end
def insert_derived_and_related_attributes(attributes)
attributes.flat_map do |attribute|
if attribute_mappings.key? attribute
attribute_mappings[attribute]
else
attribute
end
end
end
# def insert_derived_and_related_attributes(ordered_questions)
# ordered_questions.flat_map do |question|
# if question.type == "checkbox"
# question.answer_options.keys
# elsif attribute_mappings.key? question.id
# attribute_mappings[question.id]
# else
# question.id
# end
# end
# end
# def attribute_mappings
# mappings = ATTRIBUTE_MAPPINGS
# unless @user.support? && @year >= 2024
# mappings = mappings.merge({"postcode_full" => %w[pcode1 pcode2]})
# end
# if @year >= 2024
# mappings = mappings.merge(ATTRIBUTE_MAPPINGS_2024)
# end
# mappings
# end
def attribute_mappings def attribute_mappings
mappings = { mappings = case @year
"saledate" => %w[day month year], when 2024
"exdate" => %w[exday exmonth exyear], ATTRIBUTE_MAPPINGS.merge(ATTRIBUTE_MAPPINGS_2024)
"hodate" => %w[hoday homonth hoyear], when 2025
"ppostcode_full" => %w[ppostc1 ppostc2], ATTRIBUTE_MAPPINGS.merge(ATTRIBUTE_MAPPINGS_2024).merge(ATTRIBUTE_MAPPINGS_2025)
"la" => %w[la la_label], else
"prevloc" => %w[prevloc prevloc_label], ATTRIBUTE_MAPPINGS
"assigned_to_id" => %w[created_by assigned_to], end
"owning_organisation_id" => %w[owning_organisation_name],
"managing_organisation_id" => %w[managing_organisation_name],
"value" => %w[value value_value_check],
"mscharge" => %w[mscharge mscharge_value_check],
}
unless @user.support? && @year >= 2024 unless @user.support? && @year >= 2024
mappings["postcode_full"] = %w[pcode1 pcode2] mappings = mappings.merge({"postcode_full" => %w[pcode1 pcode2]})
end
if @year >= 2024
mappings["uprn"] = %w[uprn uprn_confirmed address_line1_input postcode_full_input uprn_selection]
end end
# binding.pry
mappings mappings
end end
@ -240,6 +338,8 @@ module Csv
%w[id status duplicate_set_id created_at updated_at old_form_id collection_start_year creation_method is_dpo] %w[id status duplicate_set_id created_at updated_at old_form_id collection_start_year creation_method is_dpo]
when 2024 when 2024
%w[id status duplicate_set_id created_at updated_at collection_start_year creation_method bulk_upload_id is_dpo] %w[id status duplicate_set_id created_at updated_at collection_start_year creation_method bulk_upload_id is_dpo]
when 2025
%w[id status duplicate_set_id created_at created_by_id updated_at updated_by_id creation_method bulk_upload_id]
else else
%w[id status duplicate_set_id created_at updated_at collection_start_year creation_method bulk_upload_id is_dpo] %w[id status duplicate_set_id created_at updated_at collection_start_year creation_method bulk_upload_id is_dpo]
end end

20
spec/services/csv/sales_log_csv_service_spec.rb

@ -213,6 +213,26 @@ RSpec.describe Csv::SalesLogCsvService do
end end
end end
context "when the requested form is 2025" do
let(:now) { Time.zone.local(2025, 5, 1) }
let(:year) { 2025 }
let(:fixed_time) { Time.zone.local(2025, 5, 1) }
before do
log.update!(nationality_all: 36, manual_address_entry_selected: false, uprn: "1", uprn_known: 1)
end
it "exports the CSV with the 2025 ordering and all values correct" do
expected_content = CSV.read("spec/fixtures/files/sales_logs_csv_export_labels_25.csv")
values_to_delete = %w[ID OWNINGORGID MANINGORGID CREATEDBYID USERNAMEID AMENDEDBYID]
values_to_delete.each do |attribute|
index = attribute_line.index(attribute)
content_line[index] = nil
end
expect(csv[1..]).to eq expected_content[1..]
end
end
context "when the requested form is 2023" do context "when the requested form is 2023" do
let(:now) { Time.zone.local(2024, 1, 1) } let(:now) { Time.zone.local(2024, 1, 1) }
let(:year) { 2023 } let(:year) { 2023 }

Loading…
Cancel
Save