From 00220e32d0662c8641251b8155a23603fa82928f Mon Sep 17 00:00:00 2001 From: natdeanlewissoftwire Date: Mon, 11 Dec 2023 10:19:39 +0000 Subject: [PATCH] feat: update csv service --- app/services/csv/scheme_csv_service.rb | 44 ++++++++++++++++++++++++-- 1 file changed, 41 insertions(+), 3 deletions(-) diff --git a/app/services/csv/scheme_csv_service.rb b/app/services/csv/scheme_csv_service.rb index 980e79b7b..c3436b604 100644 --- a/app/services/csv/scheme_csv_service.rb +++ b/app/services/csv/scheme_csv_service.rb @@ -1,5 +1,7 @@ module Csv class SchemeCsvService + include SchemesHelper + def initialize(user:) @user = user @attributes = scheme_attributes @@ -17,12 +19,48 @@ module Csv private - def value(attribute, log) - log.public_send(attribute) + CUSTOM_CALL_CHAINS = { + scheme_owning_organisation_name: %i[owning_organisation name], + }.freeze + + SYSTEM_DATE_FIELDS = %w[ + created_at + ].freeze + + def value(attribute, scheme) + if attribute == "scheme_active_dates" + scheme_availability(scheme) + elsif CUSTOM_CALL_CHAINS.key? attribute.to_sym + call_chain = CUSTOM_CALL_CHAINS[attribute.to_sym] + call_chain.reduce(scheme) { |object, next_call| object&.public_send(next_call) } + elsif SYSTEM_DATE_FIELDS.include? attribute + scheme.public_send(attribute)&.iso8601 + else + scheme.public_send(attribute) + end end + ATTRIBUTE_MAPPINGS = { + "scheme_code" => %w[id_to_display], + "scheme_service_name" => %w[service_name], + "scheme_status" => %w[status], + "scheme_sensitive" => %w[sensitive], + "scheme_type" => [], + "scheme_registered_under_care_act" => %w[registered_under_care_act], + "scheme_support_services_provided_by" => %w[arrangement_type], + "scheme_primary_client_group" => %w[primary_client_group], + "scheme_has_other_client_group" => %w[has_other_client_group], + "scheme_secondary_client_group" => %w[secondary_client_group], + "scheme_support_type" => %w[support_type], + "scheme_intended_stay" => %w[intended_stay], + "scheme_created_at" => %w[created_at], + }.freeze + def scheme_attributes - %w[scheme_code scheme_service_name scheme_status scheme_sensitive scheme_type scheme_registered_under_care_act scheme_owning_organisation_name scheme_support_services_provided_by scheme_primary_client_group scheme_has_other_client_group scheme_secondary_client_group scheme_support_type scheme_intended_stay scheme_created_at scheme_active_dates] + attributes = %w[scheme_code scheme_service_name scheme_status scheme_sensitive scheme_type scheme_registered_under_care_act scheme_owning_organisation_name scheme_support_services_provided_by scheme_primary_client_group scheme_has_other_client_group scheme_secondary_client_group scheme_support_type scheme_intended_stay scheme_created_at scheme_active_dates] + attributes.flat_map do |attribute| + ATTRIBUTE_MAPPINGS.fetch(attribute, attribute) + end end end end