Class: Workarea::OrdersSeeds

Inherits:
Object
  • Object
show all
Includes:
ActiveSupport::Testing::TimeHelpers
Defined in:
app/seeds/workarea/orders_seeds.rb

Instance Method Summary collapse

Instance Method Details

#create_orderObject



25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
# File 'app/seeds/workarea/orders_seeds.rb', line 25

def create_order
  user = User.sample

  if user.first_name.blank? || user.last_name.blank?
    user.update_attributes!(
      first_name: Faker::Name.first_name,
      last_name: Faker::Name.last_name
    )
  end

  unless user.addresses.count > 0
    user.addresses.create!(
      first_name: user.first_name,
      last_name: user.last_name,
      street: Faker::Address.street_address,
      city: Faker::Address.city,
      postal_code: Faker::Address.zip_code,
      region: Faker::Address.state_abbr,
      country: 'US',
      phone_number: Faker::PhoneNumber.cell_phone,
      phone_extension: Faker::PhoneNumber.extension
    )
  end

  payment_profile = Payment::Profile.lookup(PaymentReference.new(user))

  payment_profile.update(store_credit: [0, 10].sample.to_m)

  saved_card = payment_profile.credit_cards.first

  saved_card ||= payment_profile.credit_cards.create!(
    first_name: user.first_name,
    last_name: user.last_name,
    number: '1',
    month: 1,
    year: Time.current.year + 5,
    cvv: '999'
  )

  quantity = rand(2) == 0 ? 1 : 2
  order = Order.find_current(user_id: user.id)

  promo_code = try_promo_code
  order.add_promo_code(promo_code) if promo_code.present?

  2.times do
    sku = find_purchasable_sku
    order.add_item(
      OrderItemDetails.find(sku).to_h.merge(
        sku: sku,
        quantity: rand(2 * quantity) + 1,
        via: find_via
      )
    )
  end

  checkout = Checkout.new(order)
  checkout.start_as(user)
  checkout.update(payment: saved_card.id)
  checkout.place_order

  Payment::Capture
    .new(payment: checkout.payment)
    .tap { |c| c.allocate_amounts!(total: order.reload.total_price) }
    .complete!

  Fulfillment.find(order.id).ship_items(
    '1Z' + rand(99_999_999).to_s,
    order.items.map { |i| { id: i.id, quantity: i.quantity } }
  )
end

#find_purchasable_skuObject



106
107
108
# File 'app/seeds/workarea/orders_seeds.rb', line 106

def find_purchasable_sku
  Inventory::Sku.where(:available.gt => 8).sample.id
end

#find_viaObject



97
98
99
100
101
102
103
104
# File 'app/seeds/workarea/orders_seeds.rb', line 97

def find_via
  if rand(2) == 0
    Catalog::Category.sample.to_gid_param
  else
    query = Catalog::Product.sample.name.split(' ').sample
    Navigation::SearchResults.new(q: query).to_gid_param
  end
end

#performObject



7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
# File 'app/seeds/workarea/orders_seeds.rb', line 7

def perform
  puts 'Adding orders...'

  12.times do |i|
    travel_to((i + 1).weeks.ago)
    create_order
    travel_back
  end

  8.times do
    travel_to rand(80).days.ago
    create_order
    travel_back
  end

  create_order
end

#try_promo_codeObject



110
111
112
113
114
# File 'app/seeds/workarea/orders_seeds.rb', line 110

def try_promo_code
  if [true, false].sample
    Pricing::Discount.pluck(:promo_codes).flatten.sample
  end
end