diff --git a/app/services/exports/case_log_export_service.rb b/app/services/exports/case_log_export_service.rb index 6842c4ef8..2144e5932 100644 --- a/app/services/exports/case_log_export_service.rb +++ b/app/services/exports/case_log_export_service.rb @@ -15,10 +15,10 @@ module Exports @logger = logger end - def export_case_logs - current_time = Time.zone.now - case_logs = retrieve_case_logs(current_time) - export = build_export_run(current_time) + def export_case_logs(full_update: false) + start_time = Time.zone.now + case_logs = retrieve_case_logs(start_time, full_update) + export = build_export_run(start_time, full_update) daily_run = get_daily_run_number archive_datetimes = write_export_archive(case_logs) write_master_manifest(daily_run, archive_datetimes) @@ -38,7 +38,7 @@ 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) + def build_export_run(current_time, full_update) if LogsExport.count.zero? return LogsExport.new(started_at: current_time) end @@ -109,13 +109,13 @@ module Exports archive_datetimes end - def retrieve_case_logs(current_time) + def retrieve_case_logs(start_time, full_update) recent_export = LogsExport.order("started_at").last - if recent_export - params = { from: recent_export.started_at, to: current_time } + if !full_update && recent_export + params = { from: recent_export.started_at, to: start_time } CaseLog.where("updated_at >= :from and updated_at <= :to", params) else - params = { to: current_time } + params = { to: start_time } CaseLog.where("updated_at <= :to", params) end end diff --git a/lib/tasks/data_export.rake b/lib/tasks/data_export.rake index 748a3009e..7e4f766c2 100644 --- a/lib/tasks/data_export.rake +++ b/lib/tasks/data_export.rake @@ -1,7 +1,8 @@ namespace :core do desc "Export data XMLs for import into Central Data System (CDS)" - task data_export: :environment do + task :data_export, %i[full_update] => :environment do |_task, args| storage_service = StorageService.new(PaasConfigurationService.new, ENV["EXPORT_PAAS_INSTANCE"]) - Exports::CaseLogExportService.new(storage_service).export_case_logs + full_update = args[:full_update].present? && args[:full_update] == "true" + Exports::CaseLogExportService.new(storage_service).export_case_logs(full_update:) end end diff --git a/spec/services/exports/case_log_export_service_spec.rb b/spec/services/exports/case_log_export_service_spec.rb index dc07f4818..3003fb75b 100644 --- a/spec/services/exports/case_log_export_service_spec.rb +++ b/spec/services/exports/case_log_export_service_spec.rb @@ -116,9 +116,9 @@ RSpec.describe Exports::CaseLogExportService do end end - context "and multiple case logs are available for export on same periods" do + context "and multiple case logs are available for export on same quarter" do before do - FactoryBot.create(:case_log, startdate: Time.zone.local(2022, 3, 20)) + FactoryBot.create(:case_log, startdate: Time.zone.local(2022, 2, 1)) FactoryBot.create(:case_log, startdate: Time.zone.local(2022, 3, 20)) end @@ -151,30 +151,54 @@ RSpec.describe Exports::CaseLogExportService do end context "when this is a second export (partial)" do - it "does not add any entry in the master manifest (no case logs)" do + before do start_time = Time.zone.local(2022, 4, 1) LogsExport.new(started_at: start_time).save! + end + it "does not add any entry in the master manifest (no case logs)" do expect(storage_service).to receive(:write_file).with(expected_master_manifest_rerun, any_args) do |_, csv_content| csv = CSV.parse(csv_content, headers: true) expect(csv&.count).to eq(0) end - export_service.export_case_logs end end end - context "and a previous export has run the same day" do - before { export_service.export_case_logs } + context "and a previous export has run the same day with logs" do + before do + FactoryBot.create(:case_log, startdate: Time.zone.local(2022, 2, 1)) + export_service.export_case_logs + end it "increments the master manifest number by 1" do expect(storage_service).to receive(:write_file).with(expected_master_manifest_rerun, any_args) export_service.export_case_logs end + + context "and we trigger another full update" do + it "increments the base number" do + export_service.export_case_logs(full_update: true) + expect(LogsExport.last.base_number).to eq(2) + end + + it "resets the increment number" do + export_service.export_case_logs(full_update: true) + expect(LogsExport.last.increment_number).to eq(1) + end + + it "records a ZIP archive in the master manifest (existing case logs)" do + expect(storage_service).to receive(:write_file).with(expected_master_manifest_rerun, any_args) do |_, csv_content| + csv = CSV.parse(csv_content, headers: true) + expect(csv&.count).to be > 0 + end + export_service.export_case_logs(full_update: true) + end + end end - context "when export has an error" do + context "and the export has an error" do before { allow(storage_service).to receive(:write_file).and_raise(StandardError.new("This is an exception")) } it "does not save a record in the database" do