From 5947a2027a7738813ede04b04defbb19334bfb7b Mon Sep 17 00:00:00 2001 From: kosiakkatrina <54268893+kosiakkatrina@users.noreply.github.com> Date: Thu, 27 Jun 2024 18:02:51 +0100 Subject: [PATCH] CLDC-3496 Move period creation to log (#2490) * Move period creation to log * Allow adding rent periods to organisations * Update more tests --- spec/factories/lettings_log.rb | 7 +++++++ spec/factories/organisation.rb | 10 ++++------ spec/helpers/organisations_helper_spec.rb | 2 +- spec/models/form/lettings/questions/period_spec.rb | 2 +- spec/models/lettings_log_derived_fields_spec.rb | 4 ++-- spec/models/organisation_spec.rb | 2 +- spec/models/validations/financial_validations_spec.rb | 2 +- .../organisations_controller_rent_periods_spec.rb | 4 ++-- spec/requests/lettings_logs_controller_spec.rb | 2 +- spec/services/bulk_upload/lettings/log_creator_spec.rb | 2 +- .../bulk_upload/lettings/year2023/row_parser_spec.rb | 2 +- .../bulk_upload/lettings/year2024/row_parser_spec.rb | 2 +- spec/services/bulk_upload/processor_spec.rb | 2 +- .../services/merge/merge_organisations_service_spec.rb | 8 ++++---- 14 files changed, 28 insertions(+), 23 deletions(-) diff --git a/spec/factories/lettings_log.rb b/spec/factories/lettings_log.rb index cbbdc6c37..62f0cb514 100644 --- a/spec/factories/lettings_log.rb +++ b/spec/factories/lettings_log.rb @@ -6,6 +6,13 @@ FactoryBot.define do managing_organisation { assigned_to.organisation } created_at { Time.zone.today } updated_at { Time.zone.today } + + before(:create) do |log, _evaluator| + if log.period && !log.managing_organisation.organisation_rent_periods.exists?(rent_period: log.period) + log.managing_organisation.organisation_rent_periods << build(:organisation_rent_period, rent_period: log.period) + end + end + trait :setup_completed do startdate_today renewal { 0 } diff --git a/spec/factories/organisation.rb b/spec/factories/organisation.rb index 6a0aac85f..c42a9d6a2 100644 --- a/spec/factories/organisation.rb +++ b/spec/factories/organisation.rb @@ -15,14 +15,12 @@ FactoryBot.define do end transient do - skip_rent_period_creation { false } + rent_periods { [] } end - after(:build) do |organisation, evaluator| - unless evaluator.skip_rent_period_creation - (1..11).each do |rent_period| - organisation.organisation_rent_periods << build(:organisation_rent_period, organisation:, rent_period:) - end + after(:create) do |organisation, evaluator| + evaluator.rent_periods.each do |rent_period| + organisation.organisation_rent_periods << build(:organisation_rent_period, rent_period:) end end diff --git a/spec/helpers/organisations_helper_spec.rb b/spec/helpers/organisations_helper_spec.rb index bdd774b18..9ebe12d1f 100644 --- a/spec/helpers/organisations_helper_spec.rb +++ b/spec/helpers/organisations_helper_spec.rb @@ -3,7 +3,7 @@ require "rails_helper" RSpec.describe OrganisationsHelper do include TagHelper describe "display_organisation_attributes" do - let(:organisation) { create(:organisation, skip_rent_period_creation: true) } + let(:organisation) { create(:organisation) } it "has the correct values" do expect(display_organisation_attributes(organisation)).to eq( diff --git a/spec/models/form/lettings/questions/period_spec.rb b/spec/models/form/lettings/questions/period_spec.rb index 4798903c8..88b11deef 100644 --- a/spec/models/form/lettings/questions/period_spec.rb +++ b/spec/models/form/lettings/questions/period_spec.rb @@ -28,7 +28,7 @@ RSpec.describe Form::Lettings::Questions::Period, type: :model do end context "when managing organisation has rent periods" do - let(:managing_organisation) { create(:organisation, skip_rent_period_creation: true) } + let(:managing_organisation) { create(:organisation) } let(:log) { create(:lettings_log, managing_organisation:) } before do diff --git a/spec/models/lettings_log_derived_fields_spec.rb b/spec/models/lettings_log_derived_fields_spec.rb index 572b30da5..54dc438bc 100644 --- a/spec/models/lettings_log_derived_fields_spec.rb +++ b/spec/models/lettings_log_derived_fields_spec.rb @@ -2,7 +2,7 @@ require "rails_helper" require "shared/shared_examples_for_derived_fields" RSpec.describe LettingsLog, type: :model do - let(:organisation) { build(:organisation, name: "derived fields org", skip_rent_period_creation: true) } + let(:organisation) { build(:organisation, name: "derived fields org") } let(:user) { build(:user, organisation:) } let(:log) { build(:lettings_log, :startdate_today, assigned_to: user) } @@ -956,7 +956,7 @@ RSpec.describe LettingsLog, type: :model do end describe "variables dependent on whether a letting is a renewal" do - let(:organisation) { create(:organisation, skip_rent_period_creation: true) } + let(:organisation) { create(:organisation) } let(:user) { create(:user, organisation:) } let(:startdate) { Time.zone.today } let(:persisted_renewal_lettings_log) { create(:lettings_log, :setup_completed, startdate:, renewal: 1, assigned_to: user) } diff --git a/spec/models/organisation_spec.rb b/spec/models/organisation_spec.rb index 2aa033ccf..d00f79039 100644 --- a/spec/models/organisation_spec.rb +++ b/spec/models/organisation_spec.rb @@ -109,7 +109,7 @@ RSpec.describe Organisation, type: :model do end context "with associated rent periods" do - let(:organisation) { create(:organisation, skip_rent_period_creation: true) } + let(:organisation) { create(:organisation) } let(:period_1_label) { "Every minute" } let(:fake_rent_periods) do { diff --git a/spec/models/validations/financial_validations_spec.rb b/spec/models/validations/financial_validations_spec.rb index 5dd738f0d..c743381f7 100644 --- a/spec/models/validations/financial_validations_spec.rb +++ b/spec/models/validations/financial_validations_spec.rb @@ -139,7 +139,7 @@ RSpec.describe Validations::FinancialValidations do end describe "rent period validations" do - let(:organisation) { create(:organisation, skip_rent_period_creation: true) } + let(:organisation) { create(:organisation) } let(:user) { create(:user, organisation:) } let(:record) { create(:lettings_log, owning_organisation: organisation, managing_organisation: organisation, assigned_to: user) } let(:used_period) { 2 } diff --git a/spec/requests/OrganisationsController/organisations_controller_rent_periods_spec.rb b/spec/requests/OrganisationsController/organisations_controller_rent_periods_spec.rb index 126a3a624..6ac50f2bb 100644 --- a/spec/requests/OrganisationsController/organisations_controller_rent_periods_spec.rb +++ b/spec/requests/OrganisationsController/organisations_controller_rent_periods_spec.rb @@ -53,7 +53,7 @@ RSpec.describe OrganisationsController, type: :request do end describe "#edit" do - let(:organisation) { create(:organisation, skip_rent_period_creation: true) } + let(:organisation) { create(:organisation) } let(:fake_rent_periods) do { "1" => { "value" => "Every minute" }, @@ -83,7 +83,7 @@ RSpec.describe OrganisationsController, type: :request do end describe "#update" do - let(:organisation) { create(:organisation, skip_rent_period_creation: true) } + let(:organisation) { create(:organisation) } let(:initially_checked_rent_period_id) { "1" } let(:initially_unchecked_rent_period_id) { "2" } let(:params) do diff --git a/spec/requests/lettings_logs_controller_spec.rb b/spec/requests/lettings_logs_controller_spec.rb index 292b8e095..4cd0a0609 100644 --- a/spec/requests/lettings_logs_controller_spec.rb +++ b/spec/requests/lettings_logs_controller_spec.rb @@ -1,7 +1,7 @@ require "rails_helper" RSpec.describe LettingsLogsController, type: :request do - let(:user) { FactoryBot.create(:user) } + let(:user) { FactoryBot.create(:user, organisation: create(:organisation, rent_periods: [2])) } let(:owning_organisation) { user.organisation } let(:managing_organisation) { owning_organisation } let(:api_username) { "test_user" } diff --git a/spec/services/bulk_upload/lettings/log_creator_spec.rb b/spec/services/bulk_upload/lettings/log_creator_spec.rb index 842e0b066..85cf310f6 100644 --- a/spec/services/bulk_upload/lettings/log_creator_spec.rb +++ b/spec/services/bulk_upload/lettings/log_creator_spec.rb @@ -3,7 +3,7 @@ require "rails_helper" RSpec.describe BulkUpload::Lettings::LogCreator do subject(:service) { described_class.new(bulk_upload:, path: "") } - let(:owning_org) { create(:organisation, old_visible_id: 123) } + let(:owning_org) { create(:organisation, old_visible_id: 123, rent_periods: [2]) } let(:user) { create(:user, organisation: owning_org) } let(:bulk_upload) { create(:bulk_upload, :lettings, user:) } diff --git a/spec/services/bulk_upload/lettings/year2023/row_parser_spec.rb b/spec/services/bulk_upload/lettings/year2023/row_parser_spec.rb index 37291cf08..ee8675bd3 100644 --- a/spec/services/bulk_upload/lettings/year2023/row_parser_spec.rb +++ b/spec/services/bulk_upload/lettings/year2023/row_parser_spec.rb @@ -10,7 +10,7 @@ RSpec.describe BulkUpload::Lettings::Year2023::RowParser do let(:user) { create(:user, organisation: owning_org) } let(:owning_org) { create(:organisation, :with_old_visible_id) } - let(:managing_org) { create(:organisation, :with_old_visible_id) } + let(:managing_org) { create(:organisation, :with_old_visible_id, rent_periods: [4]) } let(:scheme) { create(:scheme, :with_old_visible_id, owning_organisation: owning_org) } let(:location) { create(:location, :with_old_visible_id, scheme:) } diff --git a/spec/services/bulk_upload/lettings/year2024/row_parser_spec.rb b/spec/services/bulk_upload/lettings/year2024/row_parser_spec.rb index 4df484416..175925cac 100644 --- a/spec/services/bulk_upload/lettings/year2024/row_parser_spec.rb +++ b/spec/services/bulk_upload/lettings/year2024/row_parser_spec.rb @@ -10,7 +10,7 @@ RSpec.describe BulkUpload::Lettings::Year2024::RowParser do let(:user) { create(:user, organisation: owning_org) } let(:owning_org) { create(:organisation, :with_old_visible_id) } - let(:managing_org) { create(:organisation, :with_old_visible_id) } + let(:managing_org) { create(:organisation, :with_old_visible_id, rent_periods: [4, 1]) } let(:scheme) { create(:scheme, :with_old_visible_id, owning_organisation: owning_org) } let(:location) { create(:location, :with_old_visible_id, scheme:) } diff --git a/spec/services/bulk_upload/processor_spec.rb b/spec/services/bulk_upload/processor_spec.rb index 2b420dc7e..e07b57ccb 100644 --- a/spec/services/bulk_upload/processor_spec.rb +++ b/spec/services/bulk_upload/processor_spec.rb @@ -5,7 +5,7 @@ RSpec.describe BulkUpload::Processor do let(:bulk_upload) { create(:bulk_upload, :lettings, user:) } let(:user) { create(:user, organisation: owning_org) } - let(:owning_org) { create(:organisation, old_visible_id: 123) } + let(:owning_org) { create(:organisation, old_visible_id: 123, rent_periods: [2]) } let(:mock_validator) do instance_double( BulkUpload::Lettings::Validator, diff --git a/spec/services/merge/merge_organisations_service_spec.rb b/spec/services/merge/merge_organisations_service_spec.rb index 5c582a95f..6eea8635f 100644 --- a/spec/services/merge/merge_organisations_service_spec.rb +++ b/spec/services/merge/merge_organisations_service_spec.rb @@ -75,8 +75,8 @@ RSpec.describe Merge::MergeOrganisationsService do end context "and merging organisation rent periods" do - let(:absorbing_organisation) { create(:organisation, holds_own_stock: false, name: "absorbing org", skip_rent_period_creation: true) } - let(:merging_organisation) { create(:organisation, holds_own_stock: true, name: "fake org", skip_rent_period_creation: true) } + let(:absorbing_organisation) { create(:organisation, holds_own_stock: false, name: "absorbing org") } + let(:merging_organisation) { create(:organisation, holds_own_stock: true, name: "fake org") } before do OrganisationRentPeriod.create!(organisation: absorbing_organisation, rent_period: 1) @@ -1160,8 +1160,8 @@ RSpec.describe Merge::MergeOrganisationsService do end context "and merging organisation rent periods" do - let(:new_absorbing_organisation) { create(:organisation, :without_dpc, holds_own_stock: false, skip_rent_period_creation: true) } - let(:merging_organisation) { create(:organisation, holds_own_stock: true, name: "fake org", skip_rent_period_creation: true) } + let(:new_absorbing_organisation) { create(:organisation, :without_dpc, holds_own_stock: false) } + let(:merging_organisation) { create(:organisation, holds_own_stock: true, name: "fake org") } before do OrganisationRentPeriod.create!(organisation: new_absorbing_organisation, rent_period: 1)