diff --git a/app/models/organisation_la.rb b/app/models/organisation_la.rb index 18ab26d26..452b65048 100644 --- a/app/models/organisation_la.rb +++ b/app/models/organisation_la.rb @@ -1,2 +1,3 @@ class OrganisationLa < ApplicationRecord + belongs_to :organisation end diff --git a/app/services/imports/organisation_la_import_service.rb b/app/services/imports/organisation_la_import_service.rb new file mode 100644 index 000000000..5d83b1aeb --- /dev/null +++ b/app/services/imports/organisation_la_import_service.rb @@ -0,0 +1,23 @@ +module Imports + class OrganisationLaImportService < ImportService + def create_organisation_las(folder) + import_from(folder, :create_organisation_la) + end + + private + + def create_organisation_la(xml_document) + xml_doc = xml_document.remove_namespaces! + organisation = Organisation.find_by(old_org_id: record_field_value(xml_document, "InstitutionId")) + + OrganisationLa.create!( + organisation:, + ons_code: record_field_value(xml_document, "ONSCode"), + ) + end + + def record_field_value(xml_document, field) + xml_document.at_xpath("//#{field}")&.text + end + end +end diff --git a/lib/tasks/data_import.rake b/lib/tasks/data_import.rake index b119b735d..daeaa7ab0 100644 --- a/lib/tasks/data_import.rake +++ b/lib/tasks/data_import.rake @@ -14,6 +14,8 @@ namespace :core do Imports::UserImportService.new(storage_service).create_users(path) when "data-protection-confirmation" Imports::DataProtectionConfirmationImportService.new(storage_service).create_data_protection_confirmations(path) + when "organisation_las" + Imports::OrganisationLaImportService.new(storage_service).create_organisation_la(path) else raise "Type #{type} is not supported by data_import" end diff --git a/spec/fixtures/softwire_imports/organisation_la/00013f30e159d7f72a3abe9ea93fb5b685d311e4.xml b/spec/fixtures/softwire_imports/organisation_las/00013f30e159d7f72a3abe9ea93fb5b685d311e4.xml similarity index 100% rename from spec/fixtures/softwire_imports/organisation_la/00013f30e159d7f72a3abe9ea93fb5b685d311e4.xml rename to spec/fixtures/softwire_imports/organisation_las/00013f30e159d7f72a3abe9ea93fb5b685d311e4.xml diff --git a/spec/services/imports/organisation_la_import_service_spec.rb b/spec/services/imports/organisation_la_import_service_spec.rb new file mode 100644 index 000000000..1dd20470c --- /dev/null +++ b/spec/services/imports/organisation_la_import_service_spec.rb @@ -0,0 +1,39 @@ +require "rails_helper" + +RSpec.describe Imports::OrganisationLaImportService do + let(:fixture_directory) { "spec/fixtures/softwire_imports/organisation_las" } + let(:old_org_id) { "44026acc7ed5c29516b26f2a5deb639e5e37966d" } + let(:old_id) { "00013f30e159d7f72a3abe9ea93fb5b685d311e4" } + let(:import_file) { File.open("#{fixture_directory}/#{old_id}.xml") } + let(:storage_service) { instance_double(StorageService) } + + context "when importing data protection confirmations" do + subject(:import_service) { described_class.new(storage_service) } + + before do + allow(storage_service) + .to receive(:list_files) + .and_return(["organisation_la_directory/#{old_id}.xml"]) + allow(storage_service) + .to receive(:get_file_io) + .with("organisation_la_directory/#{old_id}.xml") + .and_return(import_file) + end + + context "when the organisation in the import file doesn't exist in the system" do + it "does not create an organisation la record" do + expect { import_service.create_organisation_las("organisation_la_directory") } + .to raise_error(ActiveRecord::RecordInvalid, /Organisation must exist/) + end + end + + context "when the organisation does exist" do + let!(:organisation) { FactoryBot.create(:organisation, old_org_id:) } + + it "successfully create an organisation la record with the expected data" do + import_service.create_organisation_las("organisation_la_directory") + expect(Organisation.find_by(old_org_id:).organisation_las.pluck("ons_code")).to eq(["E07000041"]) + end + end + end +end