From da82ad6c05b45695ebef3f63f83fb3020a6f27cb Mon Sep 17 00:00:00 2001 From: Kat Date: Mon, 5 Aug 2024 10:22:21 +0100 Subject: [PATCH] Allow exporting users individually --- app/services/exports/export_service.rb | 30 +++++-- lib/tasks/data_export.rake | 4 +- spec/services/exports/export_service_spec.rb | 92 ++++++++++++++++++++ 3 files changed, 118 insertions(+), 8 deletions(-) diff --git a/app/services/exports/export_service.rb b/app/services/exports/export_service.rb index 288c7513a..fd7aae017 100644 --- a/app/services/exports/export_service.rb +++ b/app/services/exports/export_service.rb @@ -7,15 +7,23 @@ module Exports @logger = logger end - def export_xml(full_update: false, collection_year: nil) + def export_xml(full_update: false, collection: nil) start_time = Time.zone.now daily_run_number = get_daily_run_number + lettings_archives_for_manifest = {} + users_archives_for_manifest = {} - lettings_export_service = Exports::LettingsLogExportService.new(@storage_service, start_time) - lettings_archives_for_manifest = lettings_export_service.export_xml_lettings_logs(full_update:, collection_year:) - - users_export_service = Exports::UserExportService.new(@storage_service, start_time) - users_archives_for_manifest = users_export_service.export_xml_users(full_update:) + if collection.present? + case collection + when "users" + users_archives_for_manifest = get_user_archives(start_time, full_update) + else + lettings_archives_for_manifest = get_lettings_archives(start_time, full_update, collection) + end + else + users_archives_for_manifest = get_user_archives(start_time, full_update) + lettings_archives_for_manifest = get_lettings_archives(start_time, full_update, collection) + end write_master_manifest(daily_run_number, lettings_archives_for_manifest.merge(users_archives_for_manifest)) end @@ -47,5 +55,15 @@ module Exports end StringIO.new(csv_string) end + + def get_user_archives(start_time, full_update) + users_export_service = Exports::UserExportService.new(@storage_service, start_time) + users_export_service.export_xml_users(full_update:) + end + + def get_lettings_archives(start_time, full_update, collection) + lettings_export_service = Exports::LettingsLogExportService.new(@storage_service, start_time) + lettings_export_service.export_xml_lettings_logs(full_update:, collection_year: collection) + end end end diff --git a/lib/tasks/data_export.rake b/lib/tasks/data_export.rake index e2445638b..903806b78 100644 --- a/lib/tasks/data_export.rake +++ b/lib/tasks/data_export.rake @@ -8,10 +8,10 @@ namespace :core do desc "Export all data XMLs for import into Central Data System (CDS)" task :full_data_export_xml, %i[year] => :environment do |_task, args| - collection_year = args[:year].present? ? args[:year].to_i : nil + collection = args[:year].present? ? args[:year].to_i : nil storage_service = Storage::S3Service.new(Configuration::EnvConfigurationService.new, ENV["EXPORT_BUCKET"]) export_service = Exports::ExportService.new(storage_service) - export_service.export_xml(full_update: true, collection_year:) + export_service.export_xml(full_update: true, collection:) end end diff --git a/spec/services/exports/export_service_spec.rb b/spec/services/exports/export_service_spec.rb index b560e8c3c..aaab77e62 100644 --- a/spec/services/exports/export_service_spec.rb +++ b/spec/services/exports/export_service_spec.rb @@ -141,4 +141,96 @@ RSpec.describe Exports::ExportService do end end end + + context "when exporting specific lettings log collection" do + context "and no lettings archives get created in lettings logs export" do + let(:lettings_logs_export_service) { instance_double("Exports::LettingsLogExportService", export_xml_lettings_logs: {}) } + + context "and user archive gets created in user export" do + let(:users_export_service) { instance_double("Exports::UserExportService", export_xml_users: { "some_user_file_base_name" => start_time }) } + + it "generates a master manifest with the correct name" do + expect(storage_service).to receive(:write_file).with(expected_master_manifest_filename, any_args) + export_service.export_xml(full_update: true, collection: "2022") + end + + it "does not write user data" do + actual_content = nil + expected_content = "zip-name,date-time zipped folder generated,zip-file-uri\n" + allow(storage_service).to receive(:write_file).with(expected_master_manifest_filename, any_args) { |_, arg2| actual_content = arg2&.string } + + export_service.export_xml(full_update: true, collection: "2022") + expect(actual_content).to eq(expected_content) + end + end + end + + context "and lettings archive gets created in lettings logs export" do + let(:lettings_logs_export_service) { instance_double("Exports::LettingsLogExportService", export_xml_lettings_logs: { "some_file_base_name" => start_time }) } + + context "and user archive gets created in user export" do + let(:users_export_service) { instance_double("Exports::UserExportService", export_xml_users: { "some_user_file_base_name" => start_time }) } + + it "generates a master manifest with the correct name" do + expect(storage_service).to receive(:write_file).with(expected_master_manifest_filename, any_args) + export_service.export_xml(full_update: true, collection: "2023") + end + + it "does not write user data" do + actual_content = nil + expected_content = "zip-name,date-time zipped folder generated,zip-file-uri\nsome_file_base_name,2022-05-01 00:00:00 +0100,some_file_base_name.zip\n" + allow(storage_service).to receive(:write_file).with(expected_master_manifest_filename, any_args) { |_, arg2| actual_content = arg2&.string } + + export_service.export_xml(full_update: true, collection: "2023") + expect(actual_content).to eq(expected_content) + end + end + end + end + + context "when exporting user collection" do + context "and no user archives get created in users export" do + let(:users_export_service) { instance_double("Exports::UserExportService", export_xml_users: {}) } + + context "and lettings log archive gets created in lettings logs export" do + let(:lettings_logs_export_service) { instance_double("Exports::LettingsLogExportService", export_xml_lettings_logs: { "some_file_base_name" => start_time }) } + + it "generates a master manifest with the correct name" do + expect(storage_service).to receive(:write_file).with(expected_master_manifest_filename, any_args) + export_service.export_xml(full_update: true, collection: "users") + end + + it "does not write lettings log data" do + actual_content = nil + expected_content = "zip-name,date-time zipped folder generated,zip-file-uri\n" + allow(storage_service).to receive(:write_file).with(expected_master_manifest_filename, any_args) { |_, arg2| actual_content = arg2&.string } + + export_service.export_xml(full_update: true, collection: "users") + expect(actual_content).to eq(expected_content) + end + end + end + + context "and users archive gets created in users export" do + let(:lettings_logs_export_service) { instance_double("Exports::LettingsLogExportService", export_xml_lettings_logs: { "some_file_base_name" => start_time }) } + + context "and lettings log archive gets created in lettings log export" do + let(:users_export_service) { instance_double("Exports::UserExportService", export_xml_users: { "some_user_file_base_name" => start_time }) } + + it "generates a master manifest with the correct name" do + expect(storage_service).to receive(:write_file).with(expected_master_manifest_filename, any_args) + export_service.export_xml(full_update: true, collection: "users") + end + + it "does not write lettings log data" do + actual_content = nil + expected_content = "zip-name,date-time zipped folder generated,zip-file-uri\nsome_user_file_base_name,2022-05-01 00:00:00 +0100,some_user_file_base_name.zip\n" + allow(storage_service).to receive(:write_file).with(expected_master_manifest_filename, any_args) { |_, arg2| actual_content = arg2&.string } + + export_service.export_xml(full_update: true, collection: "users") + expect(actual_content).to eq(expected_content) + end + end + end + end end