mirror of
https://github.com/openfoodfoundation/openfoodnetwork
synced 2026-02-09 23:06:06 +00:00
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:
@@ -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)
|
||||
|
||||
30
app/services/customer_syncer.rb
Normal file
30
app/services/customer_syncer.rb
Normal 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
|
||||
Reference in New Issue
Block a user