Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Enterprises Controller: reset_distributor must be called before any call to memoized current_distributor #5501

Merged
merged 2 commits into from
May 26, 2020
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
5 changes: 4 additions & 1 deletion app/controllers/enterprises_controller.rb
Original file line number Diff line number Diff line change
Expand Up @@ -65,7 +65,10 @@ def enough_stock?
def reset_order
order = current_order(true)

OrderCartReset.new(order, params[:id], try_spree_current_user, current_customer).call
# reset_distributor must be called before any call to current_customer or current_distributor
order_cart_reset = OrderCartReset.new(order, params[:id])
order_cart_reset.reset_distributor
order_cart_reset.reset_other!(try_spree_current_user, current_customer)
rescue ActiveRecord::RecordNotFound
flash[:error] = I18n.t(:enterprise_shop_show_error)
redirect_to shops_path
Expand Down
31 changes: 15 additions & 16 deletions app/services/order_cart_reset.rb
Original file line number Diff line number Diff line change
Expand Up @@ -2,25 +2,12 @@

# Resets an order by verifying it's state and fixing any issues
class OrderCartReset
def initialize(order, distributor_id, current_user, current_customer)
def initialize(order, distributor_id)
@order = order
@distributor ||= Enterprise.is_distributor.find_by_permalink(distributor_id) ||
Enterprise.is_distributor.find(distributor_id)
@current_user = current_user
@current_customer = current_customer
end

def call
reset_distributor
reset_user_and_customer if current_user
reset_order_cycle
order.save!
end

private

attr_reader :order, :distributor, :current_user, :current_customer

def reset_distributor
if order.distributor && order.distributor != distributor
order.empty!
Expand All @@ -29,12 +16,24 @@ def reset_distributor
order.distributor = distributor
end

def reset_user_and_customer
def reset_other!(current_user, current_customer)
reset_user_and_customer(current_user)
reset_order_cycle(current_customer)
order.save!
end

private

attr_reader :order, :distributor, :current_user

def reset_user_and_customer(current_user)
return unless current_user

order.associate_user!(current_user) if order.user.blank? || order.email.blank?
order.__send__(:associate_customer) if order.customer.nil? # Only associates existing customers
end

def reset_order_cycle
def reset_order_cycle(current_customer)
listed_order_cycles = Shop::OrderCyclesList.new(distributor, current_customer).call

if order_cycle_not_listed?(order.order_cycle, listed_order_cycles)
Expand Down
15 changes: 14 additions & 1 deletion spec/controllers/enterprises_controller_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@

describe EnterprisesController, type: :controller do
describe "shopping for a distributor" do
let(:user) { create(:user) }
let(:order) { controller.current_order(true) }
let(:line_item) { create(:line_item) }
let!(:current_distributor) { create(:distributor_enterprise, with_payment_and_shipping: true) }
Expand All @@ -17,10 +18,23 @@
it "sets the shop as the distributor on the order when shopping for the distributor" do
spree_get :shop, id: distributor

expect(controller.current_distributor).to eq(distributor)
expect(controller.current_order.distributor).to eq(distributor)
expect(controller.current_order.order_cycle).to be_nil
end

context "when user is logged in" do
before { allow(controller).to receive(:spree_current_user) { user } }

it "sets the shop as the distributor on the order when shopping for the distributor" do
spree_get :shop, id: distributor

expect(controller.current_distributor).to eq(distributor)
expect(controller.current_order.distributor).to eq(distributor)
expect(controller.current_order.order_cycle).to be_nil
end
end

it "sorts order cycles by the distributor's preferred ordering attr" do
distributor.update_attribute(:preferred_shopfront_order_cycle_order, 'orders_close_at')
spree_get :shop, id: distributor
Expand All @@ -32,7 +46,6 @@
end

context "using FilterOrderCycles tag rules" do
let(:user) { create(:user) }
let!(:order_cycle3) { create(:simple_order_cycle, distributors: [distributor], orders_open_at: 3.days.ago, orders_close_at: 4.days.from_now) }
let!(:oc3_exchange) { order_cycle3.exchanges.outgoing.to_enterprise(distributor).first }
let(:customer) { create(:customer, user: user, enterprise: distributor) }
Expand Down
6 changes: 3 additions & 3 deletions spec/services/order_cart_reset_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,7 @@
let(:new_distributor) { create(:distributor_enterprise) }

it "empties order" do
OrderCartReset.new(order, new_distributor.id.to_s, nil, nil).call
OrderCartReset.new(order, new_distributor.id.to_s).reset_distributor

expect(order.line_items).to be_empty
end
Expand All @@ -28,7 +28,7 @@
it "empties order and makes order cycle nil" do
expect(order_cycle_list).to receive(:call).and_return([])

OrderCartReset.new(order, distributor.id.to_s, nil, nil).call
OrderCartReset.new(order, distributor.id.to_s).reset_other!(nil, nil)

expect(order.line_items).to be_empty
expect(order.order_cycle).to be_nil
Expand All @@ -38,7 +38,7 @@
other_order_cycle = create(:simple_order_cycle, distributors: [distributor])
expect(order_cycle_list).to receive(:call).and_return([other_order_cycle])

OrderCartReset.new(order, distributor.id.to_s, nil, nil).call
OrderCartReset.new(order, distributor.id.to_s).reset_other!(nil, nil)

expect(order.order_cycle).to eq other_order_cycle
end
Expand Down