From c47bdeeb45ed0fd663fa998e223897bd8c4047ba Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?St=C3=A9phane=20Meny?= Date: Wed, 18 May 2022 15:33:55 +0100 Subject: [PATCH] Add zip information to master manifest --- .../exports/case_log_export_service.rb | 21 ++++++++++++------- .../exports/case_log_export_service_spec.rb | 18 ++++++++++------ 2 files changed, 25 insertions(+), 14 deletions(-) diff --git a/app/services/exports/case_log_export_service.rb b/app/services/exports/case_log_export_service.rb index 3b355ba78..6842c4ef8 100644 --- a/app/services/exports/case_log_export_service.rb +++ b/app/services/exports/case_log_export_service.rb @@ -20,8 +20,8 @@ module Exports case_logs = retrieve_case_logs(current_time) export = build_export_run(current_time) daily_run = get_daily_run_number - archive_list = write_export_archive(case_logs) - write_master_manifest(daily_run, archive_list) + archive_datetimes = write_export_archive(case_logs) + write_master_manifest(daily_run, archive_datetimes) export.save! end @@ -38,8 +38,8 @@ module Exports LogsExport.where(created_at: today.beginning_of_day..today.end_of_day).count + 1 end - def build_export_run(current_time, full_update = false) - if LogsExport.count == 0 + def build_export_run(current_time, full_update: false) + if LogsExport.count.zero? return LogsExport.new(started_at: current_time) end @@ -56,13 +56,13 @@ module Exports LogsExport.new(started_at: current_time, base_number:, increment_number:) end - def write_master_manifest(daily_run, archive_list) + def write_master_manifest(daily_run, archive_datetimes) today = Time.zone.today increment_number = daily_run.to_s.rjust(4, "0") month = today.month.to_s.rjust(2, "0") day = today.day.to_s.rjust(2, "0") file_path = "Manifest_#{today.year}_#{month}_#{day}_#{increment_number}.csv" - string_io = build_manifest_csv_io + string_io = build_manifest_csv_io(archive_datetimes) @storage_service.write_file(file_path, string_io) end @@ -88,6 +88,7 @@ module Exports end # Write all archives + archive_datetimes = {} case_logs_per_archive.each do |archive, case_logs_to_export| manifest_xml = build_manifest_xml(case_logs_to_export.count) zip_io = Zip::File.open_buffer(StringIO.new) @@ -102,9 +103,10 @@ module Exports end @storage_service.write_file("#{archive}.zip", zip_io.write_buffer) + archive_datetimes[archive] = Time.zone.now end - case_logs_per_archive.keys + archive_datetimes end def retrieve_case_logs(current_time) @@ -118,10 +120,13 @@ module Exports end end - def build_manifest_csv_io + def build_manifest_csv_io(archive_datetimes) headers = ["zip-name", "date-time zipped folder generated", "zip-file-uri"] csv_string = CSV.generate do |csv| csv << headers + archive_datetimes.each do |archive, datetime| + csv << [archive, datetime, "#{archive}.zip"] + end end StringIO.new(csv_string) end diff --git a/spec/services/exports/case_log_export_service_spec.rb b/spec/services/exports/case_log_export_service_spec.rb index 30d9c58df..dc07f4818 100644 --- a/spec/services/exports/case_log_export_service_spec.rb +++ b/spec/services/exports/case_log_export_service_spec.rb @@ -11,9 +11,7 @@ RSpec.describe Exports::CaseLogExportService do let(:expected_zip_filename) { "core_2021_2022_jan_mar_f0001_inc0001.zip" } let(:expected_manifest_filename) { "manifest.xml" } - let!(:case_log) { FactoryBot.create(:case_log, :completed) } - - def replace_entity_ids(export_template) + def replace_entity_ids(case_log, export_template) export_template.sub!(/\{id\}/, (case_log["id"] + Exports::CaseLogExportService::LOG_ID_OFFSET).to_s) export_template.sub!(/\{owning_org_id\}/, case_log["owning_organisation_id"].to_s) export_template.sub!(/\{managing_org_id\}/, case_log["managing_organisation_id"].to_s) @@ -51,6 +49,7 @@ RSpec.describe Exports::CaseLogExportService do end context "and one case log is available for export" do + let!(:case_log) { FactoryBot.create(:case_log, :completed) } let(:expected_data_filename) { "core_2021_2022_jan_mar_f0001_inc0001_pt001.xml" } it "generates a ZIP export file with the expected filename" do @@ -88,7 +87,7 @@ RSpec.describe Exports::CaseLogExportService do end it "generates an XML export file with the expected content within the ZIP file" do - expected_content = replace_entity_ids(export_file.read) + expected_content = replace_entity_ids(case_log, export_file.read) expect(storage_service).to receive(:write_file).with(expected_zip_filename, any_args) do |_, content| entry = Zip::File.open_buffer(content).find_entry(expected_data_filename) expect(entry).not_to be_nil @@ -101,7 +100,11 @@ RSpec.describe Exports::CaseLogExportService do context "and multiple case logs are available for export on different periods" do let(:expected_zip_filename2) { "core_2022_2023_apr_jun_f0001_inc0001.zip" } - before { FactoryBot.create(:case_log, startdate: Time.zone.local(2022, 4, 1)) } + + before do + FactoryBot.create(:case_log, startdate: Time.zone.local(2022, 2, 1)) + FactoryBot.create(:case_log, startdate: Time.zone.local(2022, 4, 1)) + end context "when case logs are across multiple quarters" do it "generates multiple ZIP export files with the expected filenames" do @@ -114,7 +117,10 @@ RSpec.describe Exports::CaseLogExportService do end context "and multiple case logs are available for export on same periods" do - before { FactoryBot.create(:case_log, startdate: Time.zone.local(2022, 3, 20)) } + before do + FactoryBot.create(:case_log, startdate: Time.zone.local(2022, 3, 20)) + FactoryBot.create(:case_log, startdate: Time.zone.local(2022, 3, 20)) + end it "generates an XML manifest file with the expected content within the ZIP file" do expected_content = replace_record_number(local_manifest_file.read, 2)