reset_other! now reset the customer when present

This commit is contained in:
Gaetan Craig-Riou
2026-03-10 13:55:34 +11:00
parent 57a69f7fa3
commit fe00d1f813
2 changed files with 36 additions and 2 deletions

View File

@@ -19,8 +19,9 @@ module Orders
end
def reset_other!(current_user, current_customer)
reset_user_and_customer(current_user)
reset_user(current_user)
reset_order_cycle(current_customer)
order.customer = current_customer if current_customer.present?
order.save!
end
@@ -28,7 +29,7 @@ module Orders
attr_reader :order, :distributor, :current_user
def reset_user_and_customer(current_user)
def reset_user(current_user)
return unless current_user
order.associate_user!(current_user) if order.user.blank? || order.email.blank?

View File

@@ -53,6 +53,39 @@ RSpec.describe Orders::CartResetService do
end
end
describe "resetting the customer" do
let(:customer) { create(:customer) }
before do
order.customer = customer
order.save!
end
it "links the customer to the order" do
new_customer = create(:customer)
described_class.new(order, distributor.id.to_s).reset_other!(nil, new_customer)
expect(order.reload.customer).to eq(new_customer)
end
context "when customer is missing" do
it "does not reset the customer" do
expect do
described_class.new(order, distributor.id.to_s).reset_other!(nil, nil)
end.not_to change { order.customer }
end
end
context "with the same customer as the order's customer" do
it "does not reset the customer" do
expect do
described_class.new(order, distributor.id.to_s).reset_other!(nil, customer)
end.not_to change { order.customer }
end
end
end
context "if the order's order cycle is not in the list of visible order cycles" do
let(:order_cycle) { create(:simple_order_cycle, distributors: [distributor]) }
let(:order_cycle_list) { instance_double(Shop::OrderCyclesList) }