Browse Source

Add import report service

pull/1818/head
Kat 3 years ago
parent
commit
b06bd15c36
  1. 27
      app/services/imports/import_report_service.rb
  2. 39
      spec/services/imports/import_report_service_spec.rb

27
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

39
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
Loading…
Cancel
Save