From 4083da5b1676b5ff6aad27ff459be915815f7c7b Mon Sep 17 00:00:00 2001 From: Luis Ramos Date: Tue, 5 May 2020 22:27:27 +0100 Subject: [PATCH] Add sample order to sample data --- lib/tasks/sample_data.rake | 3 ++ lib/tasks/sample_data/order_factory.rb | 62 ++++++++++++++++++++++++++ 2 files changed, 65 insertions(+) create mode 100644 lib/tasks/sample_data/order_factory.rb diff --git a/lib/tasks/sample_data.rake b/lib/tasks/sample_data.rake index bf929281b6..1dcac757c9 100644 --- a/lib/tasks/sample_data.rake +++ b/lib/tasks/sample_data.rake @@ -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? diff --git a/lib/tasks/sample_data/order_factory.rb b/lib/tasks/sample_data/order_factory.rb new file mode 100644 index 0000000000..cbd7059dab --- /dev/null +++ b/lib/tasks/sample_data/order_factory.rb @@ -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