Ensuring that #items_bought_by_user doesn't return items from cancelled orders

This commit is contained in:
Rob Harrington
2017-04-27 14:39:36 +10:00
parent 68c8759af1
commit 314ccc2f27
2 changed files with 22 additions and 9 deletions

View File

@@ -251,16 +251,11 @@ class OrderCycle < ActiveRecord::Base
end
def items_bought_by_user(user, distributor)
orders = Spree::Order.complete.where(user_id: user, distributor_id: distributor, order_cycle_id: self)
items = []
orders.each do |o|
items += o.line_items
end
# The Spree::Order.complete scope only checks for completed_at date, does not ensure state is "complete"
orders = Spree::Order.complete.where(state: "complete", user_id: user, distributor_id: distributor, order_cycle_id: self)
items = orders.map(&:line_items).flatten
scoper = OpenFoodNetwork::ScopeVariantToHub.new(distributor)
items.each do |li|
scoper.scope(li.variant)
end
items
items.each { |li| scoper.scope(li.variant) }
end
private

View File

@@ -505,4 +505,22 @@ describe OrderCycle do
OrderCycle.earliest_closing_times[e2.id].should == time2
end
end
describe "finding all line items sold by to a user by a given shop" do
let(:shop) { create(:enterprise) }
let(:user) { create(:user) }
let(:oc) { create(:order_cycle) }
let!(:order1) { create(:completed_order_with_totals, distributor: shop, user: user, order_cycle: oc) }
let!(:order2) { create(:completed_order_with_totals, distributor: create(:enterprise), user: user, order_cycle: oc) }
let!(:order3) { create(:completed_order_with_totals, distributor: shop, user: create(:user), order_cycle: oc) }
let!(:order4) { create(:completed_order_with_totals, distributor: shop, user: user, order_cycle: create(:order_cycle)) }
let!(:order5) { create(:completed_order_with_totals, distributor: shop, user: user, order_cycle: oc) }
before { order5.cancel }
it "only returns items from non-cancelled orders in the OC, placed by the user at the shop" do
items = oc.items_bought_by_user(user, shop)
expect(items).to eq order1.reload.line_items
end
end
end