Move customer update logic to own service file

The Order class is too big already. We can move more code in the next
commits.
This commit is contained in:
Maikel Linke
2022-09-27 17:53:04 +10:00
committed by Konrad
parent cec81a3ae6
commit a0260c4ff7
2 changed files with 31 additions and 17 deletions

View File

@@ -732,23 +732,7 @@ module Spree
self.customer ||= find_customer
self.customer ||= create_customer if require_customer?
synchronise_customer_email
end
# Update the customer record if the user changed their email address.
def synchronise_customer_email
if user && customer && user.email != customer.email
duplicate = Customer.find_by(email: user.email, enterprise: distributor)
if duplicate.present?
Spree::Order.where(customer_id: duplicate.id).update_all(customer_id: customer.id)
Subscription.where(customer_id: duplicate.id).update_all(customer_id: customer.id)
duplicate.destroy
end
customer.update(email: user.email)
end
CustomerSyncer.new(self).synchronise_customer_email
end
def update_adjustment!(adjustment)

View File

@@ -0,0 +1,30 @@
# frozen_string_literal: true
# Create, find and update customer records.
#
# P.S.: I almost couldn't resist to call this CustomerService.
class CustomerSyncer
attr_reader :customer, :distributor, :user
def initialize(order)
@customer = order.customer
@distributor = order.distributor
@user = order.user
end
# Update the customer record if the user changed their email address.
def synchronise_customer_email
if user && customer && user.email != customer.email
duplicate = Customer.find_by(email: user.email, enterprise: distributor)
if duplicate.present?
Spree::Order.where(customer_id: duplicate.id).update_all(customer_id: customer.id)
Subscription.where(customer_id: duplicate.id).update_all(customer_id: customer.id)
duplicate.destroy
end
customer.update(email: user.email)
end
end
end