From b06bd15c3662abcbfc2cb3375da735d1e8130206 Mon Sep 17 00:00:00 2001 From: Kat Date: Wed, 2 Aug 2023 16:06:42 +0100 Subject: [PATCH] Add import report service --- app/services/imports/import_report_service.rb | 27 +++++++++++++ .../imports/import_report_service_spec.rb | 39 +++++++++++++++++++ 2 files changed, 66 insertions(+) create mode 100644 app/services/imports/import_report_service.rb create mode 100644 spec/services/imports/import_report_service_spec.rb diff --git a/app/services/imports/import_report_service.rb b/app/services/imports/import_report_service.rb new file mode 100644 index 000000000..c4fe5a1b2 --- /dev/null +++ b/app/services/imports/import_report_service.rb @@ -0,0 +1,27 @@ +module Imports + class ImportReportService + def initialize(storage_service, old_organisation_ids, logger = Rails.logger) + @storage_service = storage_service + @logger = logger + @old_organisation_ids = old_organisation_ids + end + + BYTE_ORDER_MARK = "\uFEFF".freeze # Required to ensure Excel always reads CSV as UTF-8 + + def create_report(report_directory) + write_missing_data_coordinators_report(report_directory) + end + + def write_missing_data_coordinators_report(report_directory) + csv_string = "Organisation ID,Old Organisation ID,Organisation Name\n" + @old_organisation_ids.each do |old_organisation_id| + organisation = Organisation.find_by(old_visible_id: old_organisation_id) + if organisation.users.none?(&:data_coordinator?) + csv_string += "#{organisation.id},#{old_organisation_id},#{organisation.name}\n" + end + end + + @storage_service.write_file("#{report_directory}/organisations_without_data_coordinators.csv", BYTE_ORDER_MARK + csv_string) + end + end +end diff --git a/spec/services/imports/import_report_service_spec.rb b/spec/services/imports/import_report_service_spec.rb new file mode 100644 index 000000000..53416d8c9 --- /dev/null +++ b/spec/services/imports/import_report_service_spec.rb @@ -0,0 +1,39 @@ +require "rails_helper" + +RSpec.describe Imports::ImportReportService do + subject(:report_service) { described_class.new(storage_service, old_organisation_ids) } + + let(:storage_service) { instance_double(Storage::S3Service) } + + context "when all organisations have data coordinators" do + let(:organisation) { create(:organisation, old_visible_id: "1") } + let(:old_organisation_ids) { [organisation.old_visible_id] } + + before do + create(:user, :data_coordinator, organisation:) + end + + it "writes an empty organisations without a data coordinators report" do + expect(storage_service).to receive(:write_file).with("report_directory/organisations_without_data_coordinators.csv", "\uFEFFOrganisation ID,Old Organisation ID,Organisation Name\n") + + report_service.create_report("report_directory") + end + end + + context "when some organisations have no data coordinators" do + let(:organisation) { create(:organisation, old_visible_id: "") } + let(:organisation2) { create(:organisation, old_visible_id: "2") } + let(:organisation3) { create(:organisation, old_visible_id: "3") } + let(:old_organisation_ids) { [organisation.old_visible_id, organisation2.old_visible_id, organisation3.old_visible_id] } + + before do + create(:user, :data_coordinator, organisation:) + end + + it "writes an empty organisations without a data coordinators report" do + expect(storage_service).to receive(:write_file).with("report_directory/organisations_without_data_coordinators.csv", "\uFEFFOrganisation ID,Old Organisation ID,Organisation Name\n#{organisation2.id},2,#{organisation2.name}\n#{organisation3.id},3,#{organisation3.name}\n") + + report_service.create_report("report_directory") + end + end +end