Browse Source

Add variable definitions as the first row of csv downloads

pull/2586/head
Manny Dinssa 2 years ago
parent
commit
c259b099b5
  1. 17
      app/services/csv/lettings_log_csv_service.rb
  2. 20
      app/services/csv/sales_log_csv_service.rb

17
app/services/csv/lettings_log_csv_service.rb

@ -5,10 +5,17 @@ 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|
record = @definitions.find { |r| r.variable == attribute.downcase }
record&.tap { |r| r.update!(last_accessed: Time.zone.now) }&.definition
end
end
csv << @attributes
logs.find_each do |log|
@ -258,6 +265,16 @@ module Csv
@user.support? ? final_attributes : final_attributes - SUPPORT_ONLY_ATTRIBUTES - soft_validations_attributes
end
def lettings_log_definitions
CsvVariableDefinition.lettings.group_by { |record| [record.variable, record.definition] }
.map do |_, options|
exact_match = options.find { |definition| definition.year == @year }
next exact_match if exact_match
options.max_by(&:year)
end
end
def insert_derived_and_related_attributes(ordered_questions)
ordered_questions.flat_map do |question|
if question.type == "checkbox"

20
app/services/csv/sales_log_csv_service.rb

@ -5,11 +5,19 @@ 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|
record = @definitions.find { |r| r.variable == attribute.downcase }
record&.tap { |r| r.update!(last_accessed: Time.zone.now) }&.definition
end
end
csv << formatted_attributes
logs.find_each do |log|
csv << @attributes.map { |attribute| value(attribute, log) }
@ -161,6 +169,16 @@ module Csv
@user.support? ? final_attributes : final_attributes - SUPPORT_ONLY_ATTRIBUTES
end
def sales_log_definitions
CsvVariableDefinition.sales.group_by { |record| [record.variable, record.definition] }
.map do |_, options|
exact_match = options.find { |definition| definition.year == @year }
next exact_match if exact_match
options.max_by(&:year)
end
end
def insert_derived_and_related_attributes(ordered_questions)
ordered_questions.flat_map do |question|
if question.type == "checkbox"

Loading…
Cancel
Save