From 9bd2463ecf0b1492ac83b5c941fa6f75befc37ad Mon Sep 17 00:00:00 2001 From: natdeanlewissoftwire Date: Mon, 27 Nov 2023 15:21:27 +0000 Subject: [PATCH] feat: use latest merge date/start date where available for availability text for schemes and locations --- app/models/location.rb | 4 +--- app/models/scheme.rb | 4 ++-- app/services/merge/merge_organisations_service.rb | 4 ++-- db/migrate/20231127120659_add_merge_date_to_schemes.rb | 5 +++++ db/migrate/20231127125729_add_merge_date_to_locations.rb | 5 +++++ db/schema.rb | 4 +++- 6 files changed, 18 insertions(+), 8 deletions(-) create mode 100644 db/migrate/20231127120659_add_merge_date_to_schemes.rb create mode 100644 db/migrate/20231127125729_add_merge_date_to_locations.rb diff --git a/app/models/location.rb b/app/models/location.rb index 65e0466b3..eceed7251 100644 --- a/app/models/location.rb +++ b/app/models/location.rb @@ -122,9 +122,7 @@ class Location < ApplicationRecord end def available_from - return startdate if startdate.present? - - FormHandler.instance.earliest_open_collection_start_date(now: created_at) + [merge_date, startdate].compact.max || FormHandler.instance.earliest_open_collection_start_date(now: created_at) end def open_deactivation diff --git a/app/models/scheme.rb b/app/models/scheme.rb index 9d760998d..a01678981 100644 --- a/app/models/scheme.rb +++ b/app/models/scheme.rb @@ -243,7 +243,7 @@ class Scheme < ApplicationRecord end def validate_confirmed - required_attributes = attribute_names - %w[id created_at updated_at old_id old_visible_id confirmed end_date sensitive secondary_client_group total_units deactivation_date deactivation_date_type] + required_attributes = attribute_names - %w[id created_at updated_at old_id old_visible_id confirmed end_date sensitive secondary_client_group total_units deactivation_date deactivation_date_type merge_date] if confirmed == true required_attributes.any? do |attribute| @@ -262,7 +262,7 @@ class Scheme < ApplicationRecord end def available_from - FormHandler.instance.earliest_open_collection_start_date(now: created_at) + merge_date || FormHandler.instance.earliest_open_collection_start_date(now: created_at) end def open_deactivation diff --git a/app/services/merge/merge_organisations_service.rb b/app/services/merge/merge_organisations_service.rb index 46426a119..e0417704c 100644 --- a/app/services/merge/merge_organisations_service.rb +++ b/app/services/merge/merge_organisations_service.rb @@ -68,9 +68,9 @@ private merging_organisation.owned_schemes.each do |scheme| next if scheme.deactivated? - new_scheme = Scheme.create!(scheme.attributes.except("id", "owning_organisation_id", "old_id", "old_visible_id").merge(owning_organisation: @absorbing_organisation)) + new_scheme = Scheme.create!(scheme.attributes.except("id", "owning_organisation_id", "old_id", "old_visible_id").merge(owning_organisation: @absorbing_organisation, merge_date: @merge_date)) scheme.locations.each do |location| - new_scheme.locations << Location.new(location.attributes.except("id", "scheme_id", "old_id", "old_visible_id")) unless location.deactivated? + new_scheme.locations << Location.new(location.attributes.except("id", "scheme_id", "old_id", "old_visible_id").merge(merge_date: @merge_date)) unless location.deactivated? end @merged_schemes[merging_organisation.name] << { name: new_scheme.service_name, code: new_scheme.id } SchemeDeactivationPeriod.create!(scheme:, deactivation_date: @merge_date) diff --git a/db/migrate/20231127120659_add_merge_date_to_schemes.rb b/db/migrate/20231127120659_add_merge_date_to_schemes.rb new file mode 100644 index 000000000..b1dd4de94 --- /dev/null +++ b/db/migrate/20231127120659_add_merge_date_to_schemes.rb @@ -0,0 +1,5 @@ +class AddMergeDateToSchemes < ActiveRecord::Migration[7.0] + def change + add_column :schemes, :merge_date, :datetime + end +end diff --git a/db/migrate/20231127125729_add_merge_date_to_locations.rb b/db/migrate/20231127125729_add_merge_date_to_locations.rb new file mode 100644 index 000000000..5a8a24dff --- /dev/null +++ b/db/migrate/20231127125729_add_merge_date_to_locations.rb @@ -0,0 +1,5 @@ +class AddMergeDateToLocations < ActiveRecord::Migration[7.0] + def change + add_column :locations, :merge_date, :datetime + end +end diff --git a/db/schema.rb b/db/schema.rb index 7eda570d1..55b1d48dd 100644 --- a/db/schema.rb +++ b/db/schema.rb @@ -10,7 +10,7 @@ # # It's strongly recommended that you check this file into your version control system. -ActiveRecord::Schema[7.0].define(version: 2023_10_23_142854) do +ActiveRecord::Schema[7.0].define(version: 2023_11_27_125729) do # These are extensions that must be enabled in order to support this database enable_extension "plpgsql" @@ -355,6 +355,7 @@ ActiveRecord::Schema[7.0].define(version: 2023_10_23_142854) do t.datetime "startdate" t.string "location_admin_district" t.boolean "confirmed" + t.datetime "merge_date" t.index ["old_id"], name: "index_locations_on_old_id", unique: true t.index ["scheme_id"], name: "index_locations_on_scheme_id" end @@ -659,6 +660,7 @@ ActiveRecord::Schema[7.0].define(version: 2023_10_23_142854) do t.string "old_visible_id" t.integer "total_units" t.boolean "confirmed" + t.datetime "merge_date" t.index ["owning_organisation_id"], name: "index_schemes_on_owning_organisation_id" end