From 284dc0c2639719a8ecc22c7c96c57dce580b314d Mon Sep 17 00:00:00 2001 From: kosiakkatrina <54268893+kosiakkatrina@users.noreply.github.com> Date: Wed, 20 Mar 2024 16:29:20 +0000 Subject: [PATCH] Set managing org to created by org on date change (#2292) --- app/controllers/form_controller.rb | 31 +++++++-- spec/requests/form_controller_spec.rb | 96 +++++++++++++++++++++++++++ 2 files changed, 120 insertions(+), 7 deletions(-) diff --git a/app/controllers/form_controller.rb b/app/controllers/form_controller.rb index 3bef3944c..ae23e564b 100644 --- a/app/controllers/form_controller.rb +++ b/app/controllers/form_controller.rb @@ -1,4 +1,6 @@ class FormController < ApplicationController + include CollectionTimeHelper + before_action :authenticate_user! before_action :find_resource, only: %i[review] before_action :find_resource_by_named_id, except: %i[review] @@ -92,6 +94,11 @@ private Date.new(0, 1, 1) end end + + if question.id == "saledate" && set_managing_organisation_to_created_by_organisation?(result["saledate"]) + result["managing_organisation_id"] = @log.created_by.organisation_id + end + next unless question_params if %w[checkbox validation_override].include?(question.type) @@ -104,13 +111,8 @@ private if question.id == "owning_organisation_id" owning_organisation = result["owning_organisation_id"].present? ? Organisation.find(result["owning_organisation_id"]) : nil - if current_user.support? && @log.managing_organisation.blank? && owning_organisation&.managing_agents&.empty? - result["managing_organisation_id"] = owning_organisation.id - elsif owning_organisation&.absorbing_organisation == current_user.organisation - result["managing_organisation_id"] = owning_organisation.id - elsif @log.managing_organisation&.absorbing_organisation == current_user.organisation && owning_organisation == current_user.organisation - result["managing_organisation_id"] = owning_organisation.id - end + + result["managing_organisation_id"] = owning_organisation.id if set_managing_organisation_to_owning_organisation?(owning_organisation) end result @@ -321,4 +323,19 @@ private duplicate.update!(duplicate_set_id: log.duplicate_set_id) if duplicate.duplicate_set_id != log.duplicate_set_id end end + + def set_managing_organisation_to_owning_organisation?(owning_organisation) + return true if current_user.support? && @log.managing_organisation.blank? && owning_organisation&.managing_agents&.empty? + return true if owning_organisation&.absorbing_organisation == current_user.organisation + return true if @log.managing_organisation&.absorbing_organisation == current_user.organisation && owning_organisation == current_user.organisation + + false + end + + def set_managing_organisation_to_created_by_organisation?(saledate) + return false if current_user.support? + return false if collection_start_year_for_date(saledate) >= 2024 + + true + end end diff --git a/spec/requests/form_controller_spec.rb b/spec/requests/form_controller_spec.rb index 4fe03baf3..c06facd94 100644 --- a/spec/requests/form_controller_spec.rb +++ b/spec/requests/form_controller_spec.rb @@ -321,6 +321,34 @@ RSpec.describe FormController, type: :request do expect(lettings_log.created_by).to eq(user) end end + + context "when the sale date changes from 2024 to 2023" do + let(:sales_log) { create(:sales_log, owning_organisation: organisation, managing_organisation:, created_by: user) } + let(:params) do + { + id: sales_log.id, + sales_log: { + page: "completion_date", + "saledate(3i)" => 30, + "saledate(2i)" => 6, + "saledate(1i)" => 2023, + }, + } + end + + before do + sales_log.saledate = Time.zone.local(2024, 12, 1) + sales_log.save!(validate: false) + sales_log.reload + end + + it "does not set managing organisation to created by organisation" do + post "/sales-logs/#{sales_log.id}/completion-date", params: params + sales_log.reload + expect(sales_log.owning_organisation).to eq(organisation) + expect(sales_log.managing_organisation).to eq(managing_organisation) + end + end end context "when a user is signed in" do @@ -881,6 +909,74 @@ RSpec.describe FormController, type: :request do end end + context "when the sale date changes from 2024 to 2023" do + let(:sales_log) { create(:sales_log, owning_organisation: organisation, managing_organisation:, created_by: user) } + let(:params) do + { + id: sales_log.id, + sales_log: { + page: "completion_date", + "saledate(3i)" => 30, + "saledate(2i)" => 6, + "saledate(1i)" => 2023, + }, + } + end + let(:managing_organisation) { create(:organisation) } + + before do + organisation.managing_agents << managing_organisation + organisation.reload + sales_log.saledate = Time.zone.local(2024, 12, 1) + sales_log.save!(validate: false) + sales_log.reload + end + + it "sets managing organisation to created by organisation" do + post "/sales-logs/#{sales_log.id}/completion-date", params: params + sales_log.reload + expect(sales_log.owning_organisation).to eq(organisation) + expect(sales_log.managing_organisation).to eq(organisation) + end + end + + context "when the sale date changes from 2024 to a different date in 2024" do + let(:sales_log) { create(:sales_log, owning_organisation: organisation, managing_organisation:, created_by: user) } + let(:params) do + { + id: sales_log.id, + sales_log: { + page: "completion_date", + "saledate(3i)" => 30, + "saledate(2i)" => 6, + "saledate(1i)" => 2024, + }, + } + end + let(:managing_organisation) { create(:organisation) } + + before do + Timecop.freeze(Time.zone.local(2024, 12, 1)) + Singleton.__init__(FormHandler) + organisation.managing_agents << managing_organisation + organisation.reload + sales_log.update!(saledate: Time.zone.local(2024, 12, 1)) + sales_log.reload + end + + after do + Timecop.return + Singleton.__init__(FormHandler) + end + + it "does not set managing organisation to created by organisation" do + post "/sales-logs/#{sales_log.id}/completion-date", params: params + sales_log.reload + expect(sales_log.owning_organisation).to eq(organisation) + expect(sales_log.managing_organisation).to eq(managing_organisation) + end + end + context "when the question was accessed from a duplicate logs screen" do let(:lettings_log) { create(:lettings_log, :duplicate, created_by: user, duplicate_set_id: 1) } let(:duplicate_log) { create(:lettings_log, :duplicate, created_by: user, duplicate_set_id: 1) }