Browse Source

Set managing org to created by org on date change (#2292)

pull/2329/head^2
kosiakkatrina 9 months ago committed by GitHub
parent
commit
284dc0c263
No known key found for this signature in database
GPG Key ID: B5690EEEBB952194
  1. 31
      app/controllers/form_controller.rb
  2. 96
      spec/requests/form_controller_spec.rb

31
app/controllers/form_controller.rb

@ -1,4 +1,6 @@
class FormController < ApplicationController class FormController < ApplicationController
include CollectionTimeHelper
before_action :authenticate_user! before_action :authenticate_user!
before_action :find_resource, only: %i[review] before_action :find_resource, only: %i[review]
before_action :find_resource_by_named_id, except: %i[review] before_action :find_resource_by_named_id, except: %i[review]
@ -92,6 +94,11 @@ private
Date.new(0, 1, 1) Date.new(0, 1, 1)
end end
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 next unless question_params
if %w[checkbox validation_override].include?(question.type) if %w[checkbox validation_override].include?(question.type)
@ -104,13 +111,8 @@ private
if question.id == "owning_organisation_id" if question.id == "owning_organisation_id"
owning_organisation = result["owning_organisation_id"].present? ? Organisation.find(result["owning_organisation_id"]) : nil 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 result["managing_organisation_id"] = owning_organisation.id if set_managing_organisation_to_owning_organisation?(owning_organisation)
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
end end
result result
@ -321,4 +323,19 @@ private
duplicate.update!(duplicate_set_id: log.duplicate_set_id) if duplicate.duplicate_set_id != log.duplicate_set_id duplicate.update!(duplicate_set_id: log.duplicate_set_id) if duplicate.duplicate_set_id != log.duplicate_set_id
end end
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 end

96
spec/requests/form_controller_spec.rb

@ -321,6 +321,34 @@ RSpec.describe FormController, type: :request do
expect(lettings_log.created_by).to eq(user) expect(lettings_log.created_by).to eq(user)
end end
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 end
context "when a user is signed in" do context "when a user is signed in" do
@ -881,6 +909,74 @@ RSpec.describe FormController, type: :request do
end end
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 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(: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) } let(:duplicate_log) { create(:lettings_log, :duplicate, created_by: user, duplicate_set_id: 1) }

Loading…
Cancel
Save