From 65f15effab8a1d48aaa22a517ac40a1fa57fcd39 Mon Sep 17 00:00:00 2001 From: James Rose Date: Thu, 30 Mar 2023 11:15:28 +0100 Subject: [PATCH] Add collection to lettings export --- .../exports/lettings_log_export_service.rb | 29 ++++++++++--------- ...330084455_add_collection_to_logs_export.rb | 5 ++++ 2 files changed, 21 insertions(+), 13 deletions(-) create mode 100644 db/migrate/20230330084455_add_collection_to_logs_export.rb diff --git a/app/services/exports/lettings_log_export_service.rb b/app/services/exports/lettings_log_export_service.rb index 350b7150a..33b6cd14e 100644 --- a/app/services/exports/lettings_log_export_service.rb +++ b/app/services/exports/lettings_log_export_service.rb @@ -8,29 +8,32 @@ module Exports @logger = logger end - def export_xml_lettings_logs(collection:, full_update: false) + def export_xml_lettings_logs(full_update: false) start_time = Time.zone.now - lettings_logs = retrieve_lettings_logs(start_time, full_update) - export = build_export_run(start_time, full_update) - daily_run = get_daily_run_number - archive_datetimes = write_export_archive(export, lettings_logs) - export.empty_export = archive_datetimes.empty? - write_master_manifest(daily_run, archive_datetimes) - export.save! + logs = retrieve_lettings_logs(start_time, full_update) + archive_datetimes = [] + logs.group_by(&:collection_start_year).each do |collection, lettings_logs| + export = build_export_run(collection, start_time, full_update) + archive_datetimes.push(write_export_archive(collection, export, lettings_logs)) + export.empty_export = archive_datetimes.empty? + export.save! + end + # This is _not_ the increment number re: archives, which are independent. + write_master_manifest(get_daily_run_number, archive_datetimes.flatten) end private def get_daily_run_number today = Time.zone.today - LogsExport.where(created_at: today.beginning_of_day..today.end_of_day).count + 1 + LogsExport.where(created_at: today.beginning_of_day..today.end_of_day).select(:started_at).distinct.count + 1 end - def build_export_run(current_time, full_update) - previous_exports_with_data = LogsExport.where(empty_export: false) + def build_export_run(collection, current_time, full_update) + previous_exports_with_data = LogsExport.where(collection:, empty_export: false) if previous_exports_with_data.empty? - return LogsExport.new(started_at: current_time) + return LogsExport.new(collection:, started_at: current_time) end base_number = previous_exports_with_data.maximum(:base_number) @@ -43,7 +46,7 @@ module Exports increment_number += 1 end - LogsExport.new(started_at: current_time, base_number:, increment_number:) + LogsExport.new(collection:, started_at: current_time, base_number:, increment_number:) end def write_master_manifest(daily_run, archive_datetimes) diff --git a/db/migrate/20230330084455_add_collection_to_logs_export.rb b/db/migrate/20230330084455_add_collection_to_logs_export.rb new file mode 100644 index 000000000..4fa4b1000 --- /dev/null +++ b/db/migrate/20230330084455_add_collection_to_logs_export.rb @@ -0,0 +1,5 @@ +class AddCollectionToLogsExport < ActiveRecord::Migration[7.0] + def change + add_column :logs_exports, :collection, :string + end +end