Browse Source

update csv email job to accomodate sales log export as well as lettings

add to tests to reflec the changes made
pull/1568/head
Arthur Campbell 3 years ago
parent
commit
4ee2aa60d1
  1. 19
      app/jobs/email_csv_job.rb
  2. 39
      spec/jobs/email_csv_job_spec.rb

19
app/jobs/email_csv_job.rb

@ -5,14 +5,25 @@ class EmailCsvJob < ApplicationJob
EXPIRATION_TIME = 3.hours.to_i
def perform(user, search_term = nil, filters = {}, all_orgs = false, organisation = nil, codes_only_export = false) # rubocop:disable Style/OptionalBooleanParameter - sidekiq can't serialise named params
unfiltered_logs = organisation.present? && user.support? ? LettingsLog.visible.where(owning_organisation_id: organisation.id) : user.lettings_logs.visible
def perform(user, search_term = nil, filters = {}, all_orgs = false, organisation = nil, codes_only_export = false, log_type = "lettings") # rubocop:disable Style/OptionalBooleanParameter - sidekiq can't serialise named params
if log_type == "lettings"
unfiltered_logs = organisation.present? && user.support? ? LettingsLog.visible.where(owning_organisation_id: organisation.id) : user.lettings_logs.visible
else
unfiltered_logs = organisation.present? && user.support? ? SalesLog.visible.where(owning_organisation_id: organisation.id) : user.sales_logs.visible
end
filtered_logs = FilterService.filter_logs(unfiltered_logs, search_term, filters, all_orgs, user)
filename = organisation.present? ? "logs-#{organisation.name}-#{Time.zone.now}.csv" : "logs-#{Time.zone.now}.csv"
filename = [log_type, "logs", organisation&.name, Time.zone.now].compact.join("-") + ".csv"
csv_string = if log_type == "sales"
export_type = codes_only_export ? "codes" : "labels"
Csv::SalesLogCsvService.new(export_type:).prepare_csv(filtered_logs)
else
filtered_logs.to_csv(user, codes_only_export:)
end
storage_service = Storage::S3Service.new(Configuration::EnvConfigurationService.new, ENV["CSV_DOWNLOAD_PAAS_INSTANCE"])
storage_service.write_file(filename, BYTE_ORDER_MARK + filtered_logs.to_csv(user, codes_only_export:))
storage_service.write_file(filename, BYTE_ORDER_MARK + csv_string)
url = storage_service.get_presigned_url(filename, EXPIRATION_TIME)

39
spec/jobs/email_csv_job_spec.rb

@ -26,6 +26,7 @@ describe EmailCsvJob do
let(:storage_service) { instance_double(Storage::S3Service) }
let(:mailer) { instance_double(CsvDownloadMailer) }
let(:sales_log_csv_service) { instance_double(Csv::SalesLogCsvService) }
before do
FactoryBot.create(:lettings_log,
@ -39,18 +40,44 @@ describe EmailCsvJob do
allow(storage_service).to receive(:write_file)
allow(storage_service).to receive(:get_presigned_url).and_return(test_url)
allow(Csv::SalesLogCsvService).to receive(:new).and_return(sales_log_csv_service)
allow(sales_log_csv_service).to receive(:prepare_csv).and_return ("")
allow(CsvDownloadMailer).to receive(:new).and_return(mailer)
allow(mailer).to receive(:send_csv_download_mail)
end
it "uses an appropriate filename in S3" do
expect(storage_service).to receive(:write_file).with(/logs-.*\.csv/, anything)
job.perform(user)
context "when exporting lettings logs" do
it "uses an appropriate filename in S3" do
expect(storage_service).to receive(:write_file).with(/lettings-logs-.*\.csv/, anything)
job.perform(user)
end
it "includes the organisation name in the filename when one is provided" do
expect(storage_service).to receive(:write_file).with(/lettings-logs-#{organisation.name}-.*\.csv/, anything)
job.perform(user, nil, {}, nil, organisation)
end
end
it "includes the organisation name in the filename when one is provided" do
expect(storage_service).to receive(:write_file).with(/logs-#{organisation.name}-.*\.csv/, anything)
job.perform(user, nil, {}, nil, organisation)
context "When exporting sales logs" do
it "uses an appropriate filename in S3" do
expect(storage_service).to receive(:write_file).with(/sales-logs-.*\.csv/, anything)
job.perform(user, nil, {}, nil, nil, nil, "sales")
end
it "includes the organisation name in the filename when one is provided" do
expect(storage_service).to receive(:write_file).with(/sales-logs-#{organisation.name}-.*\.csv/, anything)
job.perform(user, nil, {}, nil, organisation, nil, "sales")
end
it "creates a SalesLogCsvService with the correct export type" do
expect(Csv::SalesLogCsvService).to receive(:new).with(export_type: "labels")
codes_only = false
job.perform(user, nil, {}, nil, nil, codes_only, "sales")
expect(Csv::SalesLogCsvService).to receive(:new).with(export_type: "codes")
codes_only = true
job.perform(user, nil, {}, nil, nil, codes_only, "sales")
end
end
it "sends an E-mail with the presigned URL and duration" do

Loading…
Cancel
Save