diff --git a/app/controllers/lettings_logs_controller.rb b/app/controllers/lettings_logs_controller.rb index 058240b67..dffdec69f 100644 --- a/app/controllers/lettings_logs_controller.rb +++ b/app/controllers/lettings_logs_controller.rb @@ -123,12 +123,6 @@ private FilterManager.new(current_user:, session:, params:, filter_type: "lettings_logs") end - def org_params - super.merge( - { "managing_organisation_id" => current_user.organisation.id }, - ) - end - def authenticate_scope! head :unauthorized and return if codes_only_export? && !current_user.support? end diff --git a/app/controllers/logs_controller.rb b/app/controllers/logs_controller.rb index 599f3a7b9..a1a23b736 100644 --- a/app/controllers/logs_controller.rb +++ b/app/controllers/logs_controller.rb @@ -63,10 +63,11 @@ private end def org_params - owning_organisation_id = instance_of?(SalesLogsController) || current_user.organisation.holds_own_stock? ? current_user.organisation.id : nil + owning_organisation_id = current_user.organisation.holds_own_stock? ? current_user.organisation.id : nil { "owning_organisation_id" => owning_organisation_id, "created_by_id" => current_user.id, + "managing_organisation_id" => current_user.organisation.id, } end diff --git a/app/models/lettings_log.rb b/app/models/lettings_log.rb index a38f1eba1..d86f20424 100644 --- a/app/models/lettings_log.rb +++ b/app/models/lettings_log.rb @@ -59,9 +59,6 @@ class LettingsLog < Log } scope :after_date, ->(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)) } - scope :filter_by_owning_organisation, ->(owning_organisation, _user = nil) { where(owning_organisation:) } - scope :filter_by_managing_organisation, ->(managing_organisation, _user = nil) { where(managing_organisation:) } scope :age1_answered, -> { where.not(age1: nil).or(where(age1_known: 1)) } scope :tcharge_answered, -> { where.not(tcharge: nil).or(where(household_charge: 1)).or(where(is_carehome: 1)) } scope :chcharge_answered, -> { where.not(chcharge: nil).or(where(is_carehome: [nil, 0])) } diff --git a/app/models/log.rb b/app/models/log.rb index ca10b94e5..b588e1269 100644 --- a/app/models/log.rb +++ b/app/models/log.rb @@ -49,6 +49,9 @@ class Log < ApplicationRecord scope :has_old_form_id, -> { where.not(old_form_id: nil) } scope :imported_2023_with_old_form_id, -> { imported.filter_by_year(2023).has_old_form_id } scope :imported_2023, -> { imported.filter_by_year(2023) } + scope :filter_by_organisation, ->(org, _user = nil) { where(owning_organisation: org).or(where(managing_organisation: org)) } + scope :filter_by_owning_organisation, ->(owning_organisation, _user = nil) { where(owning_organisation:) } + scope :filter_by_managing_organisation, ->(managing_organisation, _user = nil) { where(managing_organisation:) } attr_accessor :skip_update_status, :skip_update_uprn_confirmed, :skip_dpo_validation diff --git a/app/models/organisation.rb b/app/models/organisation.rb index da6a92d0d..76d14dfde 100644 --- a/app/models/organisation.rb +++ b/app/models/organisation.rb @@ -74,7 +74,7 @@ class Organisation < ApplicationRecord end def sales_logs - SalesLog.filter_by_owning_organisation(absorbed_organisations + [self]) + SalesLog.filter_by_organisation(absorbed_organisations + [self]) end def owned_lettings_logs @@ -82,13 +82,17 @@ class Organisation < ApplicationRecord end def owned_sales_logs - sales_logs + SalesLog.filter_by_owning_organisation(absorbed_organisations + [self]) end def managed_lettings_logs LettingsLog.filter_by_managing_organisation(absorbed_organisations + [self]) end + def managed_sales_logs + SalesLog.filter_by_managing_organisation(absorbed_organisations + [self]) + end + def address_string %i[address_line1 address_line2 postcode].map { |field| public_send(field) }.join("\n") end diff --git a/app/models/sales_log.rb b/app/models/sales_log.rb index fd330f5c5..847a94f99 100644 --- a/app/models/sales_log.rb +++ b/app/models/sales_log.rb @@ -42,8 +42,6 @@ class SalesLog < Log .or(filter_by_postcode(param)) .or(filter_by_id(param)) } - scope :filter_by_organisation, ->(owning_organisation, _user = nil) { where(owning_organisation:) } - scope :filter_by_owning_organisation, ->(owning_organisation, _user = nil) { where(owning_organisation:) } scope :age1_answered, -> { where.not(age1: nil).or(where(age1_known: [1, 2])) } scope :duplicate_logs, lambda { |log| visible.where(log.slice(*DUPLICATE_LOG_ATTRIBUTES)) diff --git a/app/models/user.rb b/app/models/user.rb index 52e2c850d..4e80f7382 100644 --- a/app/models/user.rb +++ b/app/models/user.rb @@ -87,7 +87,7 @@ class User < ApplicationRecord if support? SalesLog.all else - SalesLog.filter_by_owning_organisation(organisation.absorbed_organisations + [organisation]) + SalesLog.filter_by_organisation(organisation.absorbed_organisations + [organisation]) end end @@ -99,6 +99,14 @@ class User < ApplicationRecord LettingsLog.filter_by_managing_organisation(organisation.absorbed_organisations + [organisation]) end + def owned_sales_logs + SalesLog.filter_by_owning_organisation(organisation.absorbed_organisations + [organisation]) + end + + def managed_sales_logs + SalesLog.filter_by_managing_organisation(organisation.absorbed_organisations + [organisation]) + end + def is_key_contact? is_key_contact end diff --git a/spec/requests/sales_logs_controller_spec.rb b/spec/requests/sales_logs_controller_spec.rb index b7e63661f..7a1c55579 100644 --- a/spec/requests/sales_logs_controller_spec.rb +++ b/spec/requests/sales_logs_controller_spec.rb @@ -171,10 +171,16 @@ RSpec.describe SalesLogsController, type: :request do post "/sales-logs", headers: end - it "sets the stock-owning org as user's org" do + it "does not set owning organisation" do created_id = response.location.match(/[0-9]+/)[0] sales_log = SalesLog.find_by(id: created_id) - expect(sales_log.owning_organisation.name).to eq("User org") + expect(sales_log.owning_organisation).to be_nil + end + + it "sets managing organisation as the user organisation" do + created_id = response.location.match(/[0-9]+/)[0] + sales_log = SalesLog.find_by(id: created_id) + expect(sales_log.managing_organisation.name).to eq("User org") end end end @@ -879,6 +885,23 @@ RSpec.describe SalesLogsController, type: :request do end end + context "with sales logs that are managed by your organisation" do + before do + completed_sales_log.update!(managing_organisation_id: user.organisation.id, owning_organisation_id: nil) + get "/sales-logs/#{completed_sales_log.id}", headers:, params: {} + end + + after do + Timecop.return + Singleton.__init__(FormHandler) + end + + it "shows the tasklist for sales logs you have access to" do + expect(response.body).to match("Log") + expect(response.body).to match(completed_sales_log.id.to_s) + end + end + context "with sales logs from a closed collection period before the previous collection" do before do sign_in user