diff --git a/app/services/csv/lettings_log_csv_service.rb b/app/services/csv/lettings_log_csv_service.rb index 0243c3841..d6a036c8b 100644 --- a/app/services/csv/lettings_log_csv_service.rb +++ b/app/services/csv/lettings_log_csv_service.rb @@ -5,10 +5,20 @@ module Csv @export_type = export_type @year = year @attributes = lettings_log_attributes + @definitions = lettings_log_definitions end def prepare_csv(logs) CSV.generate(headers: true) do |csv| + if @year >= 2023 + csv << @attributes.map do |attribute| + definition = @definitions.find { |record| record.variable == attribute.downcase } + if definition + definition.update!(last_accessed: Time.zone.now) + definition.definition + end + end + end csv << @attributes logs.find_each do |log| @@ -258,6 +268,25 @@ module Csv @user.support? ? final_attributes : final_attributes - SUPPORT_ONLY_ATTRIBUTES - soft_validations_attributes end + def lettings_log_definitions + definitions = if @user.support? + CsvVariableDefinition.where("log_type = 'lettings'") + else + CsvVariableDefinition.where("user_type = 'user' AND log_type = 'lettings'") + end + + definitions.group_by { |record| [record.variable, record.definition] } + .map do |_, records| + record = nil + year = @year + while year >= 2021 && record.nil? + record = records.find { |r| r.log_type == "lettings" && r.year == year } + year -= 1 + end + record || records.first + end + end + def insert_derived_and_related_attributes(ordered_questions) ordered_questions.flat_map do |question| if question.type == "checkbox" diff --git a/app/services/csv/sales_log_csv_service.rb b/app/services/csv/sales_log_csv_service.rb index 844491fee..f470cb4f2 100644 --- a/app/services/csv/sales_log_csv_service.rb +++ b/app/services/csv/sales_log_csv_service.rb @@ -5,11 +5,22 @@ module Csv @export_type = export_type @year = year @attributes = sales_log_attributes + @definitions = sales_log_definitions end def prepare_csv(logs) CSV.generate(headers: true) do |csv| - csv << formatted_attribute_headers + formatted_attributes = formatted_attribute_headers + if @year >= 2023 + csv << formatted_attributes.map do |attribute| + definition = @definitions.find { |record| record.variable == attribute.downcase } + if definition + definition.update!(last_accessed: Time.zone.now) + definition.definition + end + end + end + csv << formatted_attributes logs.find_each do |log| csv << @attributes.map { |attribute| value(attribute, log) } @@ -161,6 +172,25 @@ module Csv @user.support? ? final_attributes : final_attributes - SUPPORT_ONLY_ATTRIBUTES end + def sales_log_definitions + definitions = if @user.support? + CsvVariableDefinition.where("log_type = 'sales'") + else + CsvVariableDefinition.where("user_type = 'user' AND log_type = 'sales'") + end + + definitions.group_by { |record| [record.variable, record.definition] } + .map do |_, records| + record = nil + year = @year + while year >= 2022 && record.nil? + record = records.find { |r| r.log_type == "sales" && r.year == year } + year -= 1 + end + record || records.first + end + end + def insert_derived_and_related_attributes(ordered_questions) ordered_questions.flat_map do |question| if question.type == "checkbox"