diff --git a/app/models/organisation.rb b/app/models/organisation.rb index 89d7d1f34..941d0b1f6 100644 --- a/app/models/organisation.rb +++ b/app/models/organisation.rb @@ -35,6 +35,8 @@ class Organisation < ApplicationRecord validates :name, presence: { message: I18n.t("validations.organisation.name_missing") } validates :provider_type, presence: { message: I18n.t("validations.organisation.provider_type_missing") } + after_create :persist_missing_old_visible_id + def lettings_logs LettingsLog.filter_by_organisation(self) end @@ -96,4 +98,13 @@ class Organisation < ApplicationRecord def has_managing_agents? managing_agents.count.positive? end + +private + + def persist_missing_old_visible_id + if old_visible_id.blank? + self.old_visible_id ||= "ORG#{id}" + update_column(:old_visible_id, old_visible_id) + end + end end diff --git a/spec/models/organisation_spec.rb b/spec/models/organisation_spec.rb index 0b69fd504..37992d13c 100644 --- a/spec/models/organisation_spec.rb +++ b/spec/models/organisation_spec.rb @@ -239,4 +239,26 @@ RSpec.describe Organisation, type: :model do end end end + + describe "callbacks" do + describe "after create" do + context "when old_visible_id present" do + subject(:model) { described_class.new(name: "foo", provider_type: "LA", old_visible_id: "123") } + + it "keeps old_visible_id" do + model.save! + expect(model.old_visible_id).to eql("123") + end + end + + context "when old_visible_id not present" do + subject(:model) { described_class.new(name: "foo", provider_type: "LA") } + + it "generates an old_visible_id" do + model.save! + expect(model.old_visible_id).to eql("ORG#{model.id}") + end + end + end + end end diff --git a/spec/services/exports/lettings_log_export_service_spec.rb b/spec/services/exports/lettings_log_export_service_spec.rb index 36adda071..60d891b1b 100644 --- a/spec/services/exports/lettings_log_export_service_spec.rb +++ b/spec/services/exports/lettings_log_export_service_spec.rb @@ -20,8 +20,8 @@ RSpec.describe Exports::LettingsLogExportService do def replace_entity_ids(lettings_log, export_template) export_template.sub!(/\{id\}/, (lettings_log["id"] + Exports::LettingsLogExportService::LOG_ID_OFFSET).to_s) - export_template.sub!(/\{owning_org_id\}/, (lettings_log["owning_organisation_id"] + Exports::LettingsLogExportService::LOG_ID_OFFSET).to_s) - export_template.sub!(/\{managing_org_id\}/, (lettings_log["managing_organisation_id"] + Exports::LettingsLogExportService::LOG_ID_OFFSET).to_s) + export_template.sub!(/\{owning_org_id\}/, "ORG#{lettings_log['owning_organisation_id']}") + export_template.sub!(/\{managing_org_id\}/, "ORG#{lettings_log['managing_organisation_id']}") end def replace_record_number(export_template, record_number)