diff --git a/app/services/imports/scheme_location_import_service.rb b/app/services/imports/scheme_location_import_service.rb index 17f96d33d..cdcd9448d 100644 --- a/app/services/imports/scheme_location_import_service.rb +++ b/app/services/imports/scheme_location_import_service.rb @@ -27,7 +27,7 @@ module Imports }.freeze def create_scheme(source_scheme, attributes) - Scheme.create!( + scheme = Scheme.new( scheme_type: attributes["scheme_type"], registered_under_care_act: attributes["registered_under_care_act"], support_type: attributes["support_type"], @@ -36,7 +36,6 @@ module Imports secondary_client_group: attributes["secondary_client_group"], sensitive: attributes["sensitive"], end_date: attributes["end_date"], - confirmed: true, # These values were set by the scheme import (management groups) owning_organisation_id: source_scheme.owning_organisation_id, managing_organisation_id: source_scheme.managing_organisation_id, @@ -45,10 +44,12 @@ module Imports old_id: source_scheme.old_id, old_visible_id: source_scheme.old_visible_id, ) + confirm_scheme(scheme) + scheme.save! && scheme end def update_scheme(scheme, attributes) - scheme.update!( + scheme.attributes = { scheme_type: attributes["scheme_type"], registered_under_care_act: attributes["registered_under_care_act"], support_type: attributes["support_type"], @@ -57,9 +58,18 @@ module Imports secondary_client_group: attributes["secondary_client_group"], sensitive: attributes["sensitive"], end_date: attributes["end_date"], - confirmed: true, - ) - scheme + } + confirm_scheme(scheme) + scheme.save! && scheme + end + + def confirm_scheme(scheme) + scheme.confirmed = true + scheme.validate_confirmed + unless scheme.errors.empty? + scheme.confirmed = false + scheme.errors.clear + end end def scheme_attributes(xml_doc) diff --git a/lib/tasks/full_import.rake b/lib/tasks/full_import.rake index 792eaceff..1c082b460 100644 --- a/lib/tasks/full_import.rake +++ b/lib/tasks/full_import.rake @@ -22,6 +22,7 @@ namespace :core do import_list.each do |step| if archive_service.folder_present?(step.folder) + Rails.logger.info("Start importing folder #{step.folder}") step.import_class.new(archive_service).send(step.import_method, step.folder) else Rails.logger.info("#{step.folder} does not exist, skipping #{step.import_class}") diff --git a/spec/lib/tasks/full_import_spec.rb b/spec/lib/tasks/full_import_spec.rb index c7fedc6dd..99f95f555 100644 --- a/spec/lib/tasks/full_import_spec.rb +++ b/spec/lib/tasks/full_import_spec.rb @@ -64,6 +64,7 @@ describe "rake core:full_import", type: :task do allow(archive_service).to receive(:folder_present?).and_return(true) allow(archive_service).to receive(:folder_present?).with("mgmtgroups").and_return(false) allow(archive_service).to receive(:folder_present?).with("schemes").and_return(false) + allow(Rails.logger).to receive(:info) end it "only calls import methods for existing folders" do diff --git a/spec/services/imports/scheme_location_import_service_spec.rb b/spec/services/imports/scheme_location_import_service_spec.rb index 95a5b2b9e..ebe421f45 100644 --- a/spec/services/imports/scheme_location_import_service_spec.rb +++ b/spec/services/imports/scheme_location_import_service_spec.rb @@ -157,6 +157,7 @@ RSpec.describe Imports::SchemeLocationImportService do expect(location.scheme.secondary_client_group).to be_nil expect(location.scheme.sensitive).to eq("No") expect(location.scheme.end_date).to eq("2050-12-31") + expect(location.scheme.confirmed).to be_truthy end context "and the end date is before the current date" do @@ -183,5 +184,19 @@ RSpec.describe Imports::SchemeLocationImportService do .not_to change(Location, :count) end end + + context "and the registered under care act value is missing" do + before { location_xml.at_xpath("//scheme:reg-home-type").content = "0" } + + it "sets the registered under care act to nil" do + location = location_service.create_scheme_location(location_xml) + expect(location.scheme.registered_under_care_act).to be_nil + end + + it "sets the confirmed status to false" do + location = location_service.create_scheme_location(location_xml) + expect(location.scheme.confirmed).to be_falsey + end + end end end