Browse Source

feat: add locations and combined csv behaviour

pull/2083/head
natdeanlewissoftwire 2 years ago
parent
commit
2ec1eb83d5
  1. 4
      app/jobs/scheme_email_csv_job.rb
  2. 86
      app/services/csv/scheme_csv_service.rb

4
app/jobs/scheme_email_csv_job.rb

@ -8,16 +8,14 @@ class SchemeEmailCsvJob < ApplicationJob
def perform(user, search_term = nil, filters = {}, all_orgs = false, organisation = nil, download_type = "combined") # rubocop:disable Style/OptionalBooleanParameter - sidekiq can't serialise named params
unfiltered_schemes = organisation.present? && user.support? ? Scheme.where(owning_organisation_id: organisation.id) : user.schemes
filtered_schemes = FilterManager.filter_schemes(unfiltered_schemes, search_term, filters, all_orgs, user)
csv_string = Csv::SchemeCsvService.new(user:, download_type:).prepare_csv(filtered_schemes)
case download_type
when "schemes"
csv_string = Csv::SchemeCsvService.new(user:).prepare_csv(filtered_schemes)
filename = "#{['schemes', organisation&.name, Time.zone.now].compact.join('-')}.csv"
when "locations"
csv_string = Csv::LocationCsvService.new(user:).prepare_csv(filtered_schemes)
filename = "#{['locations', organisation&.name, Time.zone.now].compact.join('-')}.csv"
when "combined"
csv_string = Csv::SchemeAndLocationCsvService.new(user:).prepare_csv(filtered_schemes)
filename = "#{['schemes-and-locations', organisation&.name, Time.zone.now].compact.join('-')}.csv"
end

86
app/services/csv/scheme_csv_service.rb

@ -1,37 +1,60 @@
module Csv
class SchemeCsvService
include SchemesHelper
include LocationsHelper
def initialize(user:)
def initialize(user:, download_type:)
@user = user
@attributes = scheme_attributes
@download_type = download_type
end
def prepare_csv(schemes)
CSV.generate(headers: true) do |csv|
csv << @attributes
csv << attributes
schemes.find_each do |scheme|
csv << @attributes.map { |attribute| value(attribute, scheme) }
if @download_type == "schemes"
csv << scheme_attributes.map { |attribute| scheme_value(attribute, scheme) }
else
scheme.locations.each do |location|
case @download_type
when "locations"
csv << [scheme.id_to_display] + location_attributes.map { |attribute| location_value(attribute, location) }
when "combined"
csv << scheme_attributes.map { |attribute| scheme_value(attribute, scheme) } + location_attributes.map { |attribute| location_value(attribute, location) }
end
end
end
end
end
end
private
FIELD_FROM_ATTRIBUTE = {
"scheme_code" => %w[id_to_display],
"scheme_service_name" => %w[service_name],
"scheme_status" => %w[status],
"scheme_sensitive" => %w[sensitive],
"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],
SCHEME_FIELD_FROM_ATTRIBUTE = {
"scheme_code" => "id_to_display",
"scheme_service_name" => "service_name",
"scheme_status" => "status",
"scheme_sensitive" => "sensitive",
"scheme_registered_under_care_act" => "registered_under_care_act",
"scheme_support_services_provided_by" => "arrangement_type",
"scheme_primary_client_group" => "primary_client_group",
"scheme_has_other_client_group" => "has_other_client_group",
"scheme_secondary_client_group" => "secondary_client_group",
"scheme_support_type" => "support_type",
"scheme_intended_stay" => "intended_stay",
"scheme_created_at" => "created_at",
}.freeze
LOCATION_FIELD_FROM_ATTRIBUTE = {
"location_code" => "id",
"location_postcode" => "postcode",
"location_name" => "name",
"location_status" => "status",
"location_local_authority" => "location_code",
"location_units" => "units",
"location_type_of_unit" => "type_of_unit",
"location_mobility_type" => "mobility_type",
"location_admin_district" => "location_admin_district",
}.freeze
CUSTOM_CALL_CHAINS = {
@ -42,8 +65,8 @@ module Csv
created_at
].freeze
def value(attribute, scheme)
attribute = FIELD_FROM_ATTRIBUTE.fetch(attribute, attribute)
def scheme_value(attribute, scheme)
attribute = SCHEME_FIELD_FROM_ATTRIBUTE.fetch(attribute, attribute)
if attribute == "scheme_active_dates"
scheme_availability(scheme)
elsif CUSTOM_CALL_CHAINS.key? attribute.to_sym
@ -56,9 +79,32 @@ module Csv
end
end
def location_value(attribute, location)
attribute = LOCATION_FIELD_FROM_ATTRIBUTE.fetch(attribute, attribute)
if attribute == "location_active_dates"
location_availability(location)
else
location.public_send(attribute)
end
end
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]
end
def location_attributes
%w[location_code location_postcode location_name location_status location_local_authority location_units location_type_of_unit location_mobility_type location_admin_district location_active_dates]
end
def attributes
case @download_type
when "schemes"
scheme_attributes
when "locations"
%w[scheme_code] + location_attributes
when "combined"
scheme_attributes + location_attributes
end
end
end
end

Loading…
Cancel
Save