Add sample order to sample data

This commit is contained in:
Luis Ramos
2020-05-05 22:27:27 +01:00
parent e04b18c70e
commit 4083da5b16
2 changed files with 65 additions and 0 deletions

View File

@@ -10,6 +10,7 @@ require "tasks/sample_data/product_factory"
require "tasks/sample_data/shipping_method_factory"
require "tasks/sample_data/taxon_factory"
require "tasks/sample_data/user_factory"
require "tasks/sample_data/order_factory"
# The sample data generated by this task is supposed to save some time during
# manual testing. It is not meant to be complete, but we try to improve it
@@ -49,6 +50,8 @@ namespace :ofn do
CustomerFactory.new.create_samples(users)
GroupFactory.new.create_samples
OrderFactory.new.create_samples
end
def seeded?

View File

@@ -0,0 +1,62 @@
# frozen_string_literal: true
require "tasks/sample_data/logging"
require "tasks/sample_data/addressing"
class OrderFactory
include Logging
include Addressing
def create_samples
log "Creating a sample order"
order_cycle = OrderCycle.find_by(name: "Fredo's Farm Hub OC")
distributor = Enterprise.find_by(name: "Fredo's Farm Hub")
create_order(
"new.customer@example.org",
order_cycle,
distributor
)
end
private
def create_order(email, order_cycle, distributor)
order = Spree::Order.create!(
email: email,
order_cycle: order_cycle,
distributor: distributor,
bill_address: order_address,
ship_address: order_address
)
order.line_items.create( variant_id: variant(order_cycle).id, quantity: 5 )
order.payments.create(payment_method_id: payment_method_id(distributor))
place_order(order)
end
def variant(order_cycle)
# First variant on the first outgoing exchange of the OC
order_cycle.exchanges.outgoing.first.variants.first
end
def payment_method_id(distributor)
# First payment method of the distributor
distributor.payment_methods.first.id
end
def place_order(order)
order.save
AdvanceOrderService.new(order).call
order
end
def order_address
address = address("25 Myrtle Street, Bayswater, 3153")
address.firstname = "John"
address.lastname = "Mistery"
address.phone = "0987654321"
address
end
end