Browse Source

Update routed to method

pull/1821/head
Kat 3 years ago
parent
commit
7bd1fb0eff
  1. 17
      app/models/form/sales/pages/organisation.rb
  2. 32
      spec/models/form/sales/pages/organisation_spec.rb

17
app/models/form/sales/pages/organisation.rb

@ -10,24 +10,17 @@ class Form::Sales::Pages::Organisation < ::Form::Page
]
end
def routed_to?(log, current_user)
def routed_to?(_log, current_user)
return false unless current_user
return true if current_user.support?
stock_owners = current_user.organisation.stock_owners + current_user.organisation.absorbed_organisations.where(holds_own_stock: true)
absorbed_stock_owners = current_user.organisation.absorbed_organisations.where(holds_own_stock: true)
if current_user.organisation.holds_own_stock?
if current_user.organisation.absorbed_organisations.any?(&:holds_own_stock?)
return true
end
return true if stock_owners.count >= 1
log.update!(owning_organisation: current_user.organisation)
return true if absorbed_stock_owners.count >= 1
else
return false if stock_owners.count.zero?
return true if stock_owners.count > 1
log.update!(owning_organisation: stock_owners.first)
return false if absorbed_stock_owners.count.zero?
return true if absorbed_stock_owners.count > 1
end
false

32
spec/models/form/sales/pages/organisation_spec.rb

@ -88,8 +88,8 @@ RSpec.describe Form::Sales::Pages::Organisation, type: :model do
expect(page.routed_to?(log, user)).to eq(false)
end
it "updates owning_organisation_id" do
expect { page.routed_to?(log, user) }.to change(log.reload, :owning_organisation).from(nil).to(stock_owner)
it "does not update owning_organisation_id" do
expect { page.routed_to?(log, user) }.not_to change(log.reload, :owning_organisation)
end
end
@ -111,11 +111,7 @@ RSpec.describe Form::Sales::Pages::Organisation, type: :model do
end
it "is not shown" do
expect(page.routed_to?(log, user)).to eq(true)
end
it "updates owning_organisation_id" do
expect { page.routed_to?(log, user) }.not_to change(log.reload, :owning_organisation)
expect(page.routed_to?(log, user)).to eq(false)
end
end
end
@ -129,12 +125,6 @@ RSpec.describe Form::Sales::Pages::Organisation, type: :model do
it "is not shown" do
expect(page.routed_to?(log, user)).to eq(false)
end
it "updates owning_organisation_id to user organisation" do
expect {
page.routed_to?(log, user)
}.to change(log.reload, :owning_organisation).from(nil).to(user.organisation)
end
end
context "with >0 stock_owners" do
@ -143,12 +133,20 @@ RSpec.describe Form::Sales::Pages::Organisation, type: :model do
create(:organisation_relationship, child_organisation: user.organisation)
end
it "is shown" do
expect(page.routed_to?(log, user)).to eq(true)
it "is not shown" do
expect(page.routed_to?(log, user)).to eq(false)
end
end
it "does not update owning_organisation_id" do
expect { page.routed_to?(log, user) }.not_to change(log.reload, :owning_organisation).from(nil)
context "with merged organisations" do
let(:merged_org) { create(:organisation) }
before do
merged_org.update!(absorbing_organisation: user.organisation, merge_date: Time.zone.now)
end
it "is shown" do
expect(page.routed_to?(log, user)).to eq(true)
end
end
end

Loading…
Cancel
Save