Browse Source

feat: add scheme csv service

pull/2083/head
natdeanlewissoftwire 2 years ago
parent
commit
f19abb6634
  1. 2
      app/controllers/organisations_controller.rb
  2. 2
      app/controllers/schemes_controller.rb
  3. 4
      app/jobs/schemes_email_csv_job.rb
  4. 28
      app/services/csv/scheme_csv_service.rb
  5. 2
      app/views/schemes/download_csv.html.erb

2
app/controllers/organisations_controller.rb

@ -37,7 +37,7 @@ class OrganisationsController < ApplicationController
end end
def email_schemes_csv def email_schemes_csv
SchemesEmailCsvJob.perform_later(current_user, search_term, session_filters, false, @organisation) SchemeEmailCsvJob.perform_later(current_user, search_term, session_filters, false, @organisation, params[:download_type])
redirect_to schemes_csv_confirmation_organisation_path redirect_to schemes_csv_confirmation_organisation_path
end end

2
app/controllers/schemes_controller.rb

@ -213,7 +213,7 @@ class SchemesController < ApplicationController
def email_csv def email_csv
all_orgs = params["organisation_select"] == "all" all_orgs = params["organisation_select"] == "all"
SchemesEmailCsvJob.perform_later(current_user, search_term, session_filters, all_orgs, nil) SchemeEmailCsvJob.perform_later(current_user, search_term, session_filters, all_orgs, nil, params[:download_type])
redirect_to csv_confirmation_schemes_path redirect_to csv_confirmation_schemes_path
end end

4
app/jobs/email_scheme_csv_job.rb → app/jobs/schemes_email_csv_job.rb

@ -1,4 +1,4 @@
class EmailSchemeCsvJob < ApplicationJob class SchemeEmailCsvJob < ApplicationJob
queue_as :default queue_as :default
BYTE_ORDER_MARK = "\uFEFF".freeze # Required to ensure Excel always reads CSV as UTF-8 BYTE_ORDER_MARK = "\uFEFF".freeze # Required to ensure Excel always reads CSV as UTF-8
@ -6,7 +6,7 @@ class EmailSchemeCsvJob < ApplicationJob
EXPIRATION_TIME = 24.hours.to_i EXPIRATION_TIME = 24.hours.to_i
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 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.visible 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) filtered_schemes = FilterManager.filter_schemes(unfiltered_schemes, search_term, filters, all_orgs, user)
case download_type case download_type

28
app/services/csv/scheme_csv_service.rb

@ -0,0 +1,28 @@
module Csv
class SchemeCsvService
def initialize(user:)
@user = user
@attributes = scheme_attributes
end
def prepare_csv(schemes)
CSV.generate(headers: true) do |csv|
csv << @attributes
schemes.find_each do |scheme|
csv << @attributes.map { |attribute| value(attribute, scheme) }
end
end
end
private
def value(attribute, log)
log.public_send(attribute)
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
end
end

2
app/views/schemes/download_csv.html.erb

@ -11,6 +11,6 @@
<p class="govuk-body">We'll send a secure download link to your email address <strong><%= @current_user.email %></strong>.</p> <p class="govuk-body">We'll send a secure download link to your email address <strong><%= @current_user.email %></strong>.</p>
<p class="govuk-body"><%= selected_schemes_and_locations_text(download_type, schemes) %></p> <p class="govuk-body"><%= selected_schemes_and_locations_text(download_type, schemes) %></p>
<%= govuk_button_to "Send email", post_path, method: :post, params: { search: search_term } %> <%= govuk_button_to "Send email", post_path, method: :post, params: { search: search_term, download_type: download_type } %>
</div> </div>
</div> </div>

Loading…
Cancel
Save