WIP, SO placement job: Finding orders to be placed

This commit is contained in:
Rob Harrington
2016-11-10 14:47:31 +11:00
parent 6f629e112a
commit 178aadb311
4 changed files with 43 additions and 3 deletions

View File

@@ -6,5 +6,10 @@ class StandingOrderPlacementJob
end
def perform
orders
end
def orders
Spree::Order.incomplete.where(order_cycle_id: order_cycle).joins(:standing_order)
end
end

View File

@@ -12,6 +12,8 @@ Spree::Order.class_eval do
belongs_to :distributor, class_name: 'Enterprise'
belongs_to :cart
belongs_to :customer
has_one :standing_order_order
has_one :standing_order, through: :standing_order_order
validates :customer, presence: true, if: :require_customer?
validate :products_available_from_new_distribution, :if => lambda { distributor_id_changed? || order_cycle_id_changed? }

View File

@@ -152,9 +152,11 @@ FactoryGirl.define do
factory :standing_order_with_items, parent: :standing_order do |standing_order|
standing_line_items { create_list(:standing_line_item, 3) }
before(:create) do |standing_order, proxy|
oc = standing_order.schedule.order_cycles.first
ex = create(:exchange, :order_cycle => oc, :sender => standing_order.shop, :receiver => standing_order.shop, :incoming => false, :pickup_time => 'time', :pickup_instructions => 'instructions')
standing_order.standing_line_items.each { |sli| ex.variants << sli.variant }
standing_order.schedule.order_cycles.each do |oc|
ex = oc.exchanges.outgoing.find_by_sender_id_and_receiver_id(standing_order.shop_id, standing_order.shop_id) ||
create(:exchange, :order_cycle => oc, :sender => standing_order.shop, :receiver => standing_order.shop, :incoming => false, :pickup_time => 'time', :pickup_instructions => 'instructions')
standing_order.standing_line_items.each { |sli| ex.variants << sli.variant }
end
end
end

View File

@@ -0,0 +1,31 @@
require 'spec_helper'
describe StandingOrderPlacementJob do
let(:shop) { create(:distributor_enterprise) }
let(:order_cycle1) { create(:simple_order_cycle, coordinator: shop) }
let(:order_cycle2) { create(:simple_order_cycle, coordinator: shop) }
let(:schedule1) { create(:schedule, order_cycles: [order_cycle1]) }
let(:schedule2) { create(:schedule, order_cycles: [order_cycle1, order_cycle2]) }
let(:standing_order1) { create(:standing_order_with_items, shop: shop, schedule: schedule1) }
let(:standing_order2) { create(:standing_order_with_items, shop: shop, schedule: schedule2) }
let!(:job) { StandingOrderPlacementJob.new(order_cycle1) }
describe "finding standing_order orders for the specified order cycle" do
let(:order1) { create(:order, order_cycle: order_cycle1, completed_at: 5.minutes.ago) } # Complete + Linked + OC Matches
let(:order2) { create(:order, order_cycle: order_cycle1) } # Incomplete + Linked + OC Matches
let(:order3) { create(:order, order_cycle: order_cycle1) } # Incomplete + Not-Linked + OC Matches
let(:order4) { create(:order, order_cycle: order_cycle2) } # Incomplete + Linked + OC Mismatch
before do
standing_order1.orders = [order1,order2]
standing_order2.orders = [order4]
end
it "only returns incomplete orders in the relevant order cycle that are linked to a standing order" do
orders = job.send(:orders)
expect(orders).to include order2
expect(orders).to_not include order1, order3, order4
end
end
end