On User confirmation, link all already placed orders to this new user

if user.email == customer.email

Co-Authored-By: Maikel <maikel@email.org.au>
This commit is contained in:
Jean-Baptiste Bellet
2022-06-08 15:40:39 +02:00
parent 19b57aa74e
commit bc43028cb1
2 changed files with 12 additions and 1 deletions

View File

@@ -41,7 +41,7 @@ module Spree
accepts_nested_attributes_for :bill_address
accepts_nested_attributes_for :ship_address
after_create :associate_customers
after_create :associate_customers, :associate_orders
validate :limit_owned_enterprises
@@ -113,6 +113,12 @@ module Spree
self.customers = Customer.where(email: email)
end
def associate_orders
Spree::Order.where(customer: customers).find_each do |order|
order.associate_user!(self)
end
end
def can_own_more_enterprises?
owned_enterprises.reload.size < enterprise_limit
end

View File

@@ -100,6 +100,7 @@ describe Spree::User do
let(:enterprise2) { create(:enterprise) }
let!(:customer1) { create(:customer, user: nil, email: email, enterprise: enterprise1) }
let!(:customer2) { create(:customer, user: nil, email: email, enterprise: enterprise2) }
let!(:order) { create(:order, customer: customer1) }
let!(:user) { create(:user, email: email) }
it "should associate these customers with the created user" do
@@ -107,6 +108,10 @@ describe Spree::User do
expect(user.customer_of(enterprise1)).to be_truthy
expect(user.customer_of(enterprise2)).to be_truthy
end
it "should associate the orders passed by customer linked to the created user" do
expect(user.orders.reload).to include order
end
end
end