Browse Source

Prevent organisations to be updated when they exist

pull/267/head
Stéphane Meny 3 years ago
parent
commit
dda6bb9ccd
No known key found for this signature in database
GPG Key ID: 9D0AFEA988527923
  1. 17
      app/services/import_service.rb
  2. 12
      spec/services/import_service_spec.rb

17
app/services/import_service.rb

@ -1,6 +1,7 @@
class ImportService class ImportService
def initialize(storage_service) def initialize(storage_service, logger = Rails.logger)
@storage_service = storage_service @storage_service = storage_service
@logger = logger
end end
def update_organisations(folder) def update_organisations(folder)
@ -15,8 +16,11 @@ private
def create_organisation(file_io) def create_organisation(file_io)
doc = Nokogiri::XML(file_io) doc = Nokogiri::XML(file_io)
Organisation.upsert({ name = field_value(doc, "name")
name: field_value(doc, "name"), old_visible_id = field_value(doc, "visible-id")
begin
Organisation.create!(
name: name,
providertype: field_value(doc, "institution-type"), providertype: field_value(doc, "institution-type"),
phone: field_value(doc, "telephone-number"), phone: field_value(doc, "telephone-number"),
holds_own_stock: to_boolean(field_value(doc, "holds-stock")), holds_own_stock: to_boolean(field_value(doc, "holds-stock")),
@ -35,8 +39,11 @@ private
supported_housing_units: field_value(doc, "supported-housing-units"), supported_housing_units: field_value(doc, "supported-housing-units"),
unspecified_units: field_value(doc, "unspecified-units"), unspecified_units: field_value(doc, "unspecified-units"),
old_org_id: field_value(doc, "id"), old_org_id: field_value(doc, "id"),
old_visible_id: field_value(doc, "visible-id"), old_visible_id: old_visible_id,
}, unique_by: "old_visible_id") )
rescue ActiveRecord::RecordNotUnique
@logger.warn("Organisation #{name} is already present with old visible ID #{old_visible_id}, skipping.")
end
end end
def field_value(doc, field) def field_value(doc, field)

12
spec/services/import_service_spec.rb

@ -2,6 +2,7 @@ require "rails_helper"
RSpec.describe ImportService do RSpec.describe ImportService do
let(:storage_service) { instance_double(StorageService) } let(:storage_service) { instance_double(StorageService) }
let(:logger) { instance_double(Rails::Rack::Logger) }
let(:folder_name) { "organisations" } let(:folder_name) { "organisations" }
let(:filenames) { %w[my_folder/my_file1.xml my_folder/my_file2.xml] } let(:filenames) { %w[my_folder/my_file1.xml my_folder/my_file2.xml] }
let(:fixture_directory) { "spec/fixtures/softwire_imports/organisations" } let(:fixture_directory) { "spec/fixtures/softwire_imports/organisations" }
@ -39,8 +40,8 @@ RSpec.describe ImportService do
end end
end end
context "when importing organisations with duplicate old visible ID" do context "when importing organisations twice" do
subject(:import_service) { described_class.new(storage_service) } subject(:import_service) { described_class.new(storage_service, logger) }
before do before do
allow(storage_service).to receive(:list_files).and_return([filenames[0]]) allow(storage_service).to receive(:list_files).and_return([filenames[0]])
@ -50,16 +51,15 @@ RSpec.describe ImportService do
) )
end end
it "successfully create and update an organisation" do it "successfully create an organisation the first time, and does not update it" do
expect(storage_service).to receive(:list_files).with(folder_name).twice expect(storage_service).to receive(:list_files).with(folder_name).twice
expect(storage_service).to receive(:get_file_io).with(filenames[0]).twice expect(storage_service).to receive(:get_file_io).with(filenames[0]).twice
expect(logger).to receive(:warn).once
expect { import_service.update_organisations(folder_name) }.to change(Organisation, :count).by(1) expect { import_service.update_organisations(folder_name) }.to change(Organisation, :count).by(1)
expect { import_service.update_organisations(folder_name) }.to change(Organisation, :count).by(0) expect { import_service.update_organisations(folder_name) }.to change(Organisation, :count).by(0)
organisation = Organisation.find_by(old_visible_id: 1) expect(Organisation).to exist(old_visible_id: 1, name: "my_organisation")
expect(organisation).not_to be_nil
expect(organisation.name).to eq("my_new_organisation")
end end
end end
end end

Loading…
Cancel
Save