From b687ec99412336e9c443b77fefb4e26c8adba0ee Mon Sep 17 00:00:00 2001 From: Phil Lee Date: Wed, 15 Feb 2023 12:33:18 +0000 Subject: [PATCH] generate old_visible_id for organsations - This is so new organisations which will not have an existing old_visible_id are generated one - This way when performing a lookup on this field we can find the correct record - These are prefixed to prevent a clash with existing IDs that have already been generated in old core --- app/models/organisation.rb | 11 ++++++++++ spec/models/organisation_spec.rb | 22 +++++++++++++++++++ .../lettings_log_export_service_spec.rb | 4 ++-- 3 files changed, 35 insertions(+), 2 deletions(-) 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)