Browse Source

Set managing organisation as user organisation

pull/2077/head
Kat 3 years ago
parent
commit
af00a15f44
  1. 6
      app/controllers/lettings_logs_controller.rb
  2. 3
      app/controllers/logs_controller.rb
  3. 3
      app/models/lettings_log.rb
  4. 3
      app/models/log.rb
  5. 8
      app/models/organisation.rb
  6. 2
      app/models/sales_log.rb
  7. 10
      app/models/user.rb
  8. 27
      spec/requests/sales_logs_controller_spec.rb

6
app/controllers/lettings_logs_controller.rb

@ -123,12 +123,6 @@ private
FilterManager.new(current_user:, session:, params:, filter_type: "lettings_logs") FilterManager.new(current_user:, session:, params:, filter_type: "lettings_logs")
end end
def org_params
super.merge(
{ "managing_organisation_id" => current_user.organisation.id },
)
end
def authenticate_scope! def authenticate_scope!
head :unauthorized and return if codes_only_export? && !current_user.support? head :unauthorized and return if codes_only_export? && !current_user.support?
end end

3
app/controllers/logs_controller.rb

@ -63,10 +63,11 @@ private
end end
def org_params 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, "owning_organisation_id" => owning_organisation_id,
"created_by_id" => current_user.id, "created_by_id" => current_user.id,
"managing_organisation_id" => current_user.organisation.id,
} }
end end

3
app/models/lettings_log.rb

@ -59,9 +59,6 @@ class LettingsLog < Log
} }
scope :after_date, ->(date) { where("lettings_logs.startdate >= ?", date) } scope :after_date, ->(date) { where("lettings_logs.startdate >= ?", date) }
scope :unresolved, -> { where(unresolved: true) } 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 :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 :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])) } scope :chcharge_answered, -> { where.not(chcharge: nil).or(where(is_carehome: [nil, 0])) }

3
app/models/log.rb

@ -49,6 +49,9 @@ class Log < ApplicationRecord
scope :has_old_form_id, -> { where.not(old_form_id: nil) } 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_with_old_form_id, -> { imported.filter_by_year(2023).has_old_form_id }
scope :imported_2023, -> { imported.filter_by_year(2023) } 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 attr_accessor :skip_update_status, :skip_update_uprn_confirmed, :skip_dpo_validation

8
app/models/organisation.rb

@ -74,7 +74,7 @@ class Organisation < ApplicationRecord
end end
def sales_logs def sales_logs
SalesLog.filter_by_owning_organisation(absorbed_organisations + [self]) SalesLog.filter_by_organisation(absorbed_organisations + [self])
end end
def owned_lettings_logs def owned_lettings_logs
@ -82,13 +82,17 @@ class Organisation < ApplicationRecord
end end
def owned_sales_logs def owned_sales_logs
sales_logs SalesLog.filter_by_owning_organisation(absorbed_organisations + [self])
end end
def managed_lettings_logs def managed_lettings_logs
LettingsLog.filter_by_managing_organisation(absorbed_organisations + [self]) LettingsLog.filter_by_managing_organisation(absorbed_organisations + [self])
end end
def managed_sales_logs
SalesLog.filter_by_managing_organisation(absorbed_organisations + [self])
end
def address_string def address_string
%i[address_line1 address_line2 postcode].map { |field| public_send(field) }.join("\n") %i[address_line1 address_line2 postcode].map { |field| public_send(field) }.join("\n")
end end

2
app/models/sales_log.rb

@ -42,8 +42,6 @@ class SalesLog < Log
.or(filter_by_postcode(param)) .or(filter_by_postcode(param))
.or(filter_by_id(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 :age1_answered, -> { where.not(age1: nil).or(where(age1_known: [1, 2])) }
scope :duplicate_logs, lambda { |log| scope :duplicate_logs, lambda { |log|
visible.where(log.slice(*DUPLICATE_LOG_ATTRIBUTES)) visible.where(log.slice(*DUPLICATE_LOG_ATTRIBUTES))

10
app/models/user.rb

@ -87,7 +87,7 @@ class User < ApplicationRecord
if support? if support?
SalesLog.all SalesLog.all
else else
SalesLog.filter_by_owning_organisation(organisation.absorbed_organisations + [organisation]) SalesLog.filter_by_organisation(organisation.absorbed_organisations + [organisation])
end end
end end
@ -99,6 +99,14 @@ class User < ApplicationRecord
LettingsLog.filter_by_managing_organisation(organisation.absorbed_organisations + [organisation]) LettingsLog.filter_by_managing_organisation(organisation.absorbed_organisations + [organisation])
end 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? def is_key_contact?
is_key_contact is_key_contact
end end

27
spec/requests/sales_logs_controller_spec.rb

@ -171,10 +171,16 @@ RSpec.describe SalesLogsController, type: :request do
post "/sales-logs", headers: post "/sales-logs", headers:
end 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] created_id = response.location.match(/[0-9]+/)[0]
sales_log = SalesLog.find_by(id: created_id) 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 end
end end
@ -879,6 +885,23 @@ RSpec.describe SalesLogsController, type: :request do
end end
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 context "with sales logs from a closed collection period before the previous collection" do
before do before do
sign_in user sign_in user

Loading…
Cancel
Save