Class: Workarea::SaveUserOrderDetails

Inherits:
Object
  • Object
show all
Includes:
Sidekiq::CallbacksWorker, Sidekiq::Worker
Defined in:
app/workers/workarea/save_user_order_details.rb

Instance Method Summary collapse

Instance Method Details

#perform(order_id) ⇒ Object



8
9
10
11
12
13
14
15
16
# File 'app/workers/workarea/save_user_order_details.rb', line 8

def perform(order_id)
  order = Order.find(order_id)
  return if order.user_id.blank?
  user = User.find(order.user_id)
  return unless user.email == order.email

  save_payment_details(order, user)
  save_shipping_details(order, user)
end

#save_payment_details(order, user) ⇒ Object



18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
# File 'app/workers/workarea/save_user_order_details.rb', line 18

def save_payment_details(order, user)
  payment = Payment.find_or_initialize_by(id: order.id)
  payment_profile = Payment::Profile.lookup(PaymentReference.new(user))
  billing_address = extract_address_attributes(payment.address)

  if billing_address.present?
    user.auto_save_billing_address(billing_address)

    if user.public_info.blank?
      user.update_attributes!(
        first_name: billing_address[:first_name],
        last_name: billing_address[:last_name]
      )
    end
  end

  if payment.credit_card? && !payment.credit_card.saved?
    payment_profile.credit_cards.create(
      first_name: payment.credit_card.first_name,
      last_name: payment.credit_card.last_name,
      display_number: payment.credit_card.display_number,
      issuer: payment.credit_card.issuer,
      month: payment.credit_card.month,
      year: payment.credit_card.year,
      token: payment.credit_card.token,
      default: payment_profile.credit_cards.none?
    )
  end
end

#save_shipping_details(order, user) ⇒ Object



48
49
50
51
52
53
54
55
56
57
58
59
# File 'app/workers/workarea/save_user_order_details.rb', line 48

def save_shipping_details(order, user)
  return unless order.requires_shipping?

  shippings = Shipping.where(order_id: order.id).to_a
  addresses = shippings.map do |shipping|
    extract_address_attributes(shipping.address)
  end

  addresses.each do |shipping_address|
    user.auto_save_shipping_address(shipping_address)
  end
end