Browse Source

Add variable definitions to exported CSVs

pull/2539/head
Manny Dinssa 2 years ago
parent
commit
0ac93a4727
  1. 29
      app/services/csv/lettings_log_csv_service.rb
  2. 32
      app/services/csv/sales_log_csv_service.rb

29
app/services/csv/lettings_log_csv_service.rb

@ -5,10 +5,20 @@ module Csv
@export_type = export_type @export_type = export_type
@year = year @year = year
@attributes = lettings_log_attributes @attributes = lettings_log_attributes
@definitions = lettings_log_definitions
end end
def prepare_csv(logs) def prepare_csv(logs)
CSV.generate(headers: true) do |csv| 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 csv << @attributes
logs.find_each do |log| logs.find_each do |log|
@ -258,6 +268,25 @@ module Csv
@user.support? ? final_attributes : final_attributes - SUPPORT_ONLY_ATTRIBUTES - soft_validations_attributes @user.support? ? final_attributes : final_attributes - SUPPORT_ONLY_ATTRIBUTES - soft_validations_attributes
end 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) def insert_derived_and_related_attributes(ordered_questions)
ordered_questions.flat_map do |question| ordered_questions.flat_map do |question|
if question.type == "checkbox" if question.type == "checkbox"

32
app/services/csv/sales_log_csv_service.rb

@ -5,11 +5,22 @@ module Csv
@export_type = export_type @export_type = export_type
@year = year @year = year
@attributes = sales_log_attributes @attributes = sales_log_attributes
@definitions = sales_log_definitions
end end
def prepare_csv(logs) def prepare_csv(logs)
CSV.generate(headers: true) do |csv| 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| logs.find_each do |log|
csv << @attributes.map { |attribute| value(attribute, log) } csv << @attributes.map { |attribute| value(attribute, log) }
@ -161,6 +172,25 @@ module Csv
@user.support? ? final_attributes : final_attributes - SUPPORT_ONLY_ATTRIBUTES @user.support? ? final_attributes : final_attributes - SUPPORT_ONLY_ATTRIBUTES
end 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) def insert_derived_and_related_attributes(ordered_questions)
ordered_questions.flat_map do |question| ordered_questions.flat_map do |question|
if question.type == "checkbox" if question.type == "checkbox"

Loading…
Cancel
Save