diff --git a/app/components/log_summary_component.html.erb b/app/components/log_summary_component.html.erb index 8093c65c3..1a62bb8eb 100644 --- a/app/components/log_summary_component.html.erb +++ b/app/components/log_summary_component.html.erb @@ -43,7 +43,7 @@
Owned by
<%= log.owning_organisation&.name %>
- <% if log.managing_organisation %> + <% if log.instance_of?(LettingsLog) && log.managing_organisation %>
Managed by
<%= log.managing_organisation&.name %>
diff --git a/app/controllers/lettings_logs_controller.rb b/app/controllers/lettings_logs_controller.rb index 40560fca8..165682954 100644 --- a/app/controllers/lettings_logs_controller.rb +++ b/app/controllers/lettings_logs_controller.rb @@ -20,7 +20,9 @@ class LettingsLogsController < LogsController end def create - super { LettingsLog.new(log_params) } + super do + LettingsLog.new(log_params) + end end def update @@ -101,6 +103,13 @@ class LettingsLogsController < LogsController end end + def org_params + { + "owning_organisation_id" => current_user.organisation.id, + "managing_organisation_id" => current_user.organisation.id, + "created_by_id" => current_user.id, + } + end private def permitted_log_params diff --git a/app/controllers/logs_controller.rb b/app/controllers/logs_controller.rb index 3982d4a54..586d9ae5f 100644 --- a/app/controllers/logs_controller.rb +++ b/app/controllers/logs_controller.rb @@ -59,14 +59,6 @@ private permitted end - def org_params - { - "owning_organisation_id" => current_user.organisation.id, - "managing_organisation_id" => current_user.organisation.id, - "created_by_id" => current_user.id, - } - end - def search_term params["search"] end diff --git a/app/controllers/sales_logs_controller.rb b/app/controllers/sales_logs_controller.rb index 8a6c9937f..cf770576f 100644 --- a/app/controllers/sales_logs_controller.rb +++ b/app/controllers/sales_logs_controller.rb @@ -43,4 +43,11 @@ class SalesLogsController < LogsController def permitted_log_params params.require(:sales_log).permit(SalesLog.editable_fields) end + + def org_params + { + "owning_organisation_id" => current_user.organisation.id, + "created_by_id" => current_user.id, + } + end end diff --git a/app/models/lettings_log.rb b/app/models/lettings_log.rb index 0041f3200..b49bd89fa 100644 --- a/app/models/lettings_log.rb +++ b/app/models/lettings_log.rb @@ -34,6 +34,7 @@ class LettingsLog < Log belongs_to :scheme, optional: true belongs_to :location, optional: true + belongs_to :managing_organisation, class_name: "Organisation", optional: true scope :filter_by_year, ->(year) { where(startdate: Time.zone.local(year.to_i, 4, 1)...Time.zone.local(year.to_i + 1, 4, 1)) } scope :filter_by_tenant_code, ->(tenant_code) { where("tenancycode ILIKE ?", "%#{tenant_code}%") } @@ -49,6 +50,7 @@ class LettingsLog < Log } scope :filter_by_before_startdate, ->(date) { where("lettings_logs.startdate >= ?", date) } scope :unresolved, -> { where(unresolved: true) } + scope :filter_by_organisation, ->(org, _user = nil) { where(owning_organisation: org).or(where(managing_organisation: org)) } AUTOGENERATED_FIELDS = %w[id status created_at updated_at discarded_at].freeze OPTIONAL_FIELDS = %w[first_time_property_let_as_social_housing tenancycode propcode].freeze @@ -519,6 +521,18 @@ class LettingsLog < Log update(unresolved: false) end + def managing_organisation_provider_type + managing_organisation&.provider_type + end + + def reset_created_by! + return unless updated_by&.support? + return if owning_organisation.blank? || managing_organisation.blank? || created_by.blank? + return if created_by&.organisation == managing_organisation || created_by&.organisation == owning_organisation + + update!(created_by: nil) + end + private def reset_derived_questions diff --git a/app/models/log.rb b/app/models/log.rb index 798b78af7..b99515c75 100644 --- a/app/models/log.rb +++ b/app/models/log.rb @@ -2,7 +2,6 @@ class Log < ApplicationRecord self.abstract_class = true belongs_to :owning_organisation, class_name: "Organisation", optional: true - belongs_to :managing_organisation, class_name: "Organisation", optional: true belongs_to :created_by, class_name: "User", optional: true belongs_to :updated_by, class_name: "User", optional: true before_save :update_status! @@ -10,7 +9,6 @@ class Log < ApplicationRecord STATUS = { "not_started" => 0, "in_progress" => 1, "completed" => 2 }.freeze enum status: STATUS - scope :filter_by_organisation, ->(org, _user = nil) { where(owning_organisation: org).or(where(managing_organisation: org)) } scope :filter_by_status, ->(status, _user = nil) { where status: } scope :filter_by_years, lambda { |years, _user = nil| first_year = years.shift @@ -42,10 +40,6 @@ class Log < ApplicationRecord ethnic_group == 17 end - def managing_organisation_provider_type - managing_organisation&.provider_type - end - def collection_period_open? form.end_date > Time.zone.today end @@ -81,14 +75,6 @@ private reset_created_by! end - def reset_created_by! - return unless updated_by&.support? - return if owning_organisation.blank? || managing_organisation.blank? || created_by.blank? - return if created_by&.organisation == managing_organisation || created_by&.organisation == owning_organisation - - update!(created_by: nil) - end - PIO = PostcodeService.new def process_previous_postcode_changes! diff --git a/app/models/sales_log.rb b/app/models/sales_log.rb index 22f750148..9b966d8f0 100644 --- a/app/models/sales_log.rb +++ b/app/models/sales_log.rb @@ -26,6 +26,7 @@ class SalesLog < Log scope :filter_by_year, ->(year) { where(saledate: Time.zone.local(year.to_i, 4, 1)...Time.zone.local(year.to_i + 1, 4, 1)) } scope :search_by, ->(param) { filter_by_id(param) } + scope :filter_by_organisation, ->(org, _user = nil) { where(owning_organisation: org) } OPTIONAL_FIELDS = %w[purchid].freeze @@ -151,4 +152,12 @@ class SalesLog < Log def mortgage_not_used? mortgageused == 2 end + + def reset_created_by! + return unless updated_by&.support? + return if owning_organisation.blank? || created_by.blank? + return if created_by&.organisation == owning_organisation + + update!(created_by: nil) + end end diff --git a/app/services/filter_service.rb b/app/services/filter_service.rb index 8fe1030de..c9dd45f35 100644 --- a/app/services/filter_service.rb +++ b/app/services/filter_service.rb @@ -17,6 +17,14 @@ class FilterService logs = logs.public_send("filter_by_#{category}", values, user) end logs = logs.order(created_at: :desc) - user.support? ? logs.all.includes(:owning_organisation, :managing_organisation) : logs + if user.support? + if logs.first.instance_of?(LettingsLog) + logs.all.includes(:owning_organisation, :managing_organisation) + else + logs.all.includes(:owning_organisation) + end + else + logs + end end end diff --git a/db/schema.rb b/db/schema.rb index 2d5e6502f..dafd3bb47 100644 --- a/db/schema.rb +++ b/db/schema.rb @@ -420,6 +420,9 @@ ActiveRecord::Schema[7.0].define(version: 2023_01_10_094518) do t.string "relat5" t.string "relat6" t.integer "hb" + t.string "sex4" + t.string "sex5" + t.string "sex6" t.integer "savings_value_check" t.integer "deposit_value_check" t.integer "frombeds" @@ -456,21 +459,17 @@ ActiveRecord::Schema[7.0].define(version: 2023_01_10_094518) do t.integer "hhregres" t.integer "hhregresstill" t.integer "proplen" + t.integer "mscharge_known" + t.decimal "mscharge", precision: 10, scale: 2 t.integer "prevten" t.integer "mortgageused" t.integer "wchair" t.integer "armedforcesspouse" - t.integer "mscharge_known" - t.decimal "mscharge", precision: 10, scale: 2 - t.string "sex4" - t.string "sex5" - t.string "sex6" - t.integer "mortlen" t.datetime "hodate", precision: nil t.integer "hoday" t.integer "homonth" t.integer "hoyear" - t.integer "extrabor" + t.integer "mortlen" t.index ["created_by_id"], name: "index_sales_logs_on_created_by_id" t.index ["owning_organisation_id"], name: "index_sales_logs_on_owning_organisation_id" t.index ["updated_by_id"], name: "index_sales_logs_on_updated_by_id" diff --git a/spec/components/log_summary_component_spec.rb b/spec/components/log_summary_component_spec.rb index 78d5a73ad..4a40af9db 100644 --- a/spec/components/log_summary_component_spec.rb +++ b/spec/components/log_summary_component_spec.rb @@ -19,7 +19,7 @@ RSpec.describe LogSummaryComponent, type: :component do expect(result).to have_text("Created 8 February 2022") expect(result).to have_text("by Danny Rojas") expect(result).to have_content("Owned by\n DLUHC") - expect(result).to have_content("Managed by\n DLUHC") + expect(result).not_to have_content("Managed by\n DLUHC") end end diff --git a/spec/factories/sales_log.rb b/spec/factories/sales_log.rb index 7381b31e3..3d9162c09 100644 --- a/spec/factories/sales_log.rb +++ b/spec/factories/sales_log.rb @@ -2,7 +2,6 @@ FactoryBot.define do factory :sales_log do created_by { FactoryBot.create(:user) } owning_organisation { created_by.organisation } - managing_organisation { created_by.organisation } created_at { Time.utc(2022, 2, 8, 16, 52, 15) } updated_at { Time.utc(2022, 2, 8, 16, 52, 15) } trait :in_progress do diff --git a/spec/features/form/check_answers_page_lettings_logs_spec.rb b/spec/features/form/check_answers_page_lettings_logs_spec.rb index f8f0b8064..9062e1abc 100644 --- a/spec/features/form/check_answers_page_lettings_logs_spec.rb +++ b/spec/features/form/check_answers_page_lettings_logs_spec.rb @@ -143,7 +143,7 @@ RSpec.describe "Lettings Log Check Answers Page" do end context "when the user is checking their answers for the household characteristics subsection" do - it "they see a seperate summary card for each member of the household" do + it "they see a separate summary card for each member of the household" do visit("/lettings-logs/#{completed_lettings_log.id}/#{subsection}/check-answers") assert_selector ".x-govuk-summary-card__title", text: "Lead tenant", count: 1 assert_selector ".x-govuk-summary-card__title", text: "Person 2", count: 1 diff --git a/spec/features/form/check_answers_page_sales_logs_spec.rb b/spec/features/form/check_answers_page_sales_logs_spec.rb index 9d1f33d64..2c6dc050d 100644 --- a/spec/features/form/check_answers_page_sales_logs_spec.rb +++ b/spec/features/form/check_answers_page_sales_logs_spec.rb @@ -39,7 +39,7 @@ RSpec.describe "Sales Log Check Answers Page" do context "when the user is checking their answers for the household characteristics subsection" do context "and the log is for a joint purchase" do - it "they see a seperate summary card for each member of the household" do + it "they see a separate summary card for each member of the household" do visit("/sales-logs/#{completed_sales_log_joint_purchase.id}/#{subsection}/check-answers") assert_selector ".x-govuk-summary-card__title", text: "Buyer 1", count: 1 assert_selector ".x-govuk-summary-card__title", text: "Buyer 2", count: 1 @@ -49,7 +49,7 @@ RSpec.describe "Sales Log Check Answers Page" do end context "and the log is for a non-joint purchase" do - it "they see a seperate summary card for each member of the household" do + it "they see a separate summary card for each member of the household" do visit("/sales-logs/#{completed_sales_log_non_joint_purchase.id}/#{subsection}/check-answers") assert_selector ".x-govuk-summary-card__title", text: "Buyer 1", count: 1 assert_selector ".x-govuk-summary-card__title", text: "Buyer 2", count: 0 diff --git a/spec/features/organisation_spec.rb b/spec/features/organisation_spec.rb index 9d2478d9f..bc6aa243a 100644 --- a/spec/features/organisation_spec.rb +++ b/spec/features/organisation_spec.rb @@ -215,7 +215,7 @@ RSpec.describe "User Features" do let(:number_of_sales_logs) { SalesLog.count } before do - FactoryBot.create_list(:sales_log, 4, owning_organisation_id: organisation.id, managing_organisation_id: organisation.id) + FactoryBot.create_list(:sales_log, 4, owning_organisation_id: organisation.id) visit("/organisations/#{org_id}/sales-logs") end diff --git a/spec/models/organisation_spec.rb b/spec/models/organisation_spec.rb index 17781eede..7d1212271 100644 --- a/spec/models/organisation_spec.rb +++ b/spec/models/organisation_spec.rb @@ -200,7 +200,7 @@ RSpec.describe Organisation, type: :model do let!(:user) { FactoryBot.create(:user, :support, last_sign_in_at: Time.zone.now, organisation:) } let!(:scheme_to_delete) { FactoryBot.create(:scheme, owning_organisation: user.organisation) } let!(:log_to_delete) { FactoryBot.create(:lettings_log, owning_organisation: user.organisation) } - let!(:sales_log_to_delete) { FactoryBot.create(:sales_log, owning_organisation: user.organisation, managing_organisation: user.organisation) } + let!(:sales_log_to_delete) { FactoryBot.create(:sales_log, owning_organisation: user.organisation) } context "when organisation is deleted" do it "child relationships ie logs, schemes and users are deleted too - application" do diff --git a/spec/models/sales_log_spec.rb b/spec/models/sales_log_spec.rb index 8f46db42d..99b518f29 100644 --- a/spec/models/sales_log_spec.rb +++ b/spec/models/sales_log_spec.rb @@ -80,21 +80,20 @@ RSpec.describe SalesLog, type: :model do let(:organisation_3) { create(:organisation) } before do - create(:sales_log, :in_progress, owning_organisation: organisation_1, managing_organisation: organisation_1) - create(:sales_log, :completed, owning_organisation: organisation_1, managing_organisation: organisation_2) - create(:sales_log, :completed, owning_organisation: organisation_2, managing_organisation: organisation_1) - create(:sales_log, :completed, owning_organisation: organisation_2, managing_organisation: organisation_2) + create(:sales_log, :in_progress, owning_organisation: organisation_1) + create(:sales_log, :completed, owning_organisation: organisation_1) + create(:sales_log, :completed, owning_organisation: organisation_2) end it "filters by given organisation id" do - expect(described_class.filter_by_organisation([organisation_1.id]).count).to eq(3) - expect(described_class.filter_by_organisation([organisation_1.id, organisation_2.id]).count).to eq(4) + expect(described_class.filter_by_organisation([organisation_1.id]).count).to eq(2) + expect(described_class.filter_by_organisation([organisation_1.id, organisation_2.id]).count).to eq(3) expect(described_class.filter_by_organisation([organisation_3.id]).count).to eq(0) end it "filters by given organisation" do - expect(described_class.filter_by_organisation([organisation_1]).count).to eq(3) - expect(described_class.filter_by_organisation([organisation_1, organisation_2]).count).to eq(4) + expect(described_class.filter_by_organisation([organisation_1]).count).to eq(2) + expect(described_class.filter_by_organisation([organisation_1, organisation_2]).count).to eq(3) expect(described_class.filter_by_organisation([organisation_3]).count).to eq(0) end end @@ -143,7 +142,6 @@ RSpec.describe SalesLog, type: :model do let!(:address_sales_log) do described_class.create({ - managing_organisation: owning_organisation, owning_organisation:, created_by: created_by_user, ppcodenk: 1, diff --git a/spec/requests/organisations_controller_spec.rb b/spec/requests/organisations_controller_spec.rb index ab5dcb6af..f63dcf54e 100644 --- a/spec/requests/organisations_controller_spec.rb +++ b/spec/requests/organisations_controller_spec.rb @@ -734,8 +734,8 @@ RSpec.describe OrganisationsController, type: :request do let(:number_of_org2_sales_logs) { 4 } before do - FactoryBot.create_list(:sales_log, number_of_org1_sales_logs, owning_organisation_id: organisation.id, managing_organisation_id: organisation.id) - FactoryBot.create_list(:sales_log, number_of_org2_sales_logs, owning_organisation_id: unauthorised_organisation.id, managing_organisation_id: unauthorised_organisation.id) + FactoryBot.create_list(:sales_log, number_of_org1_sales_logs, owning_organisation_id: organisation.id) + FactoryBot.create_list(:sales_log, number_of_org2_sales_logs, owning_organisation_id: unauthorised_organisation.id) get "/organisations/#{organisation.id}/sales-logs", headers:, params: {} end diff --git a/spec/requests/sales_logs_controller_spec.rb b/spec/requests/sales_logs_controller_spec.rb index 150997423..f8690a71b 100644 --- a/spec/requests/sales_logs_controller_spec.rb +++ b/spec/requests/sales_logs_controller_spec.rb @@ -3,7 +3,6 @@ require "rails_helper" RSpec.describe SalesLogsController, type: :request do let(:user) { FactoryBot.create(:user) } let(:owning_organisation) { user.organisation } - let(:managing_organisation) { owning_organisation } let(:api_username) { "test_user" } let(:api_password) { "test_password" } let(:basic_credentials) do @@ -14,7 +13,6 @@ RSpec.describe SalesLogsController, type: :request do let(:params) do { "owning_organisation_id": owning_organisation.id, - "managing_organisation_id": managing_organisation.id, "created_by_id": user.id, } end @@ -51,7 +49,6 @@ RSpec.describe SalesLogsController, type: :request do it "creates a sales log with the values passed" do json_response = JSON.parse(response.body) expect(json_response["owning_organisation_id"]).to eq(owning_organisation.id) - expect(json_response["managing_organisation_id"]).to eq(managing_organisation.id) expect(json_response["created_by_id"]).to eq(user.id) end @@ -94,14 +91,12 @@ RSpec.describe SalesLogsController, type: :request do FactoryBot.create( :sales_log, owning_organisation: organisation, - managing_organisation: organisation, ) end let!(:unauthorized_sales_log) do FactoryBot.create( :sales_log, owning_organisation: other_organisation, - managing_organisation: other_organisation, ) end @@ -146,13 +141,11 @@ RSpec.describe SalesLogsController, type: :request do let!(:not_started_sales_log) do FactoryBot.create(:sales_log, owning_organisation: organisation, - managing_organisation: organisation, created_by: user) end let!(:completed_sales_log) do FactoryBot.create(:sales_log, :completed, owning_organisation: organisation_2, - managing_organisation: organisation, created_by: user_2) end @@ -189,14 +182,12 @@ RSpec.describe SalesLogsController, type: :request do let!(:sales_log_2021) do FactoryBot.create(:sales_log, :in_progress, owning_organisation: organisation, - saledate: Time.zone.local(2022, 3, 1), - managing_organisation: organisation) + saledate: Time.zone.local(2022, 3, 1)) end let!(:sales_log_2022) do sales_log = FactoryBot.build(:sales_log, :completed, owning_organisation: organisation, - saledate: Time.zone.local(2022, 12, 1), - managing_organisation: organisation) + saledate: Time.zone.local(2022, 12, 1)) sales_log.save!(validate: false) sales_log end @@ -227,14 +218,12 @@ RSpec.describe SalesLogsController, type: :request do FactoryBot.create(:sales_log, :completed, owning_organisation: organisation, saledate: Time.zone.local(2022, 3, 1), - managing_organisation: organisation, created_by: user) end let!(:sales_log_2022) do FactoryBot.create(:sales_log, owning_organisation: organisation, saledate: Time.zone.local(2022, 12, 1), - managing_organisation: organisation, created_by: user) end @@ -362,7 +351,7 @@ RSpec.describe SalesLogsController, type: :request do context "when there are more than 20 logs" do before do - FactoryBot.create_list(:sales_log, 25, owning_organisation: organisation, managing_organisation: organisation) + FactoryBot.create_list(:sales_log, 25, owning_organisation: organisation) end context "when on the first page" do