Compare commits

...

5 Commits

5 changed files with 41 additions and 6 deletions

View File

@@ -63,8 +63,7 @@ Spree::Product.class_eval do
scope :visible_for, lambda { |enterprise|
joins('LEFT OUTER JOIN spree_variants AS o_spree_variants ON (o_spree_variants.product_id = spree_products.id)').
joins('LEFT OUTER JOIN inventory_items AS o_inventory_items ON (o_spree_variants.id = o_inventory_items.variant_id)').
where('o_inventory_items.enterprise_id = (?) AND visible = (?)', enterprise, true).
select('DISTINCT spree_products.*')
where('o_inventory_items.enterprise_id = (?) AND visible = (?)', enterprise, true)
}
# -- Scopes

View File

@@ -30,9 +30,11 @@ class Api::Admin::ForOrderCycle::EnterpriseSerializer < ActiveModel::Serializer
def products_scope
products_relation = object.supplied_products
if order_cycle.prefers_product_selection_from_coordinator_inventory_only?
products_relation = products_relation.visible_for(order_cycle.coordinator)
products_relation = products_relation.
visible_for(order_cycle.coordinator).
select('DISTINCT spree_products.*')
end
products_relation.order(:name)
products_relation
end
def products

View File

@@ -11,6 +11,7 @@ module PermittedAttributes
@params.require(:order_cycle).permit(
:name, :orders_open_at, :orders_close_at, :coordinator_id,
:preferred_product_selection_from_coordinator_inventory_only,
incoming_exchanges: permitted_exchange_attributes,
outgoing_exchanges: permitted_exchange_attributes,
schedule_ids: [], coordinator_fee_ids: []

View File

@@ -178,10 +178,22 @@ module Admin
it "returns an error message" do
spree_put :update, params
json_response = JSON.parse(response.body)
expect(json_response['errors']).to be
end
end
it "can update preference product_selection_from_coordinator_inventory_only" do
expect(OrderCycleForm).to receive(:new).
with(order_cycle,
{ "preferred_product_selection_from_coordinator_inventory_only" => true },
anything) { form_mock }
allow(form_mock).to receive(:save) { true }
spree_put :update, params.
merge(order_cycle: { preferred_product_selection_from_coordinator_inventory_only: true })
end
end
end

View File

@@ -7,8 +7,9 @@ describe ExchangeProductsRenderer do
describe "#exchange_products" do
describe "for an incoming exchange" do
let(:exchange) { order_cycle.exchanges.incoming.first }
it "loads products" do
exchange = order_cycle.exchanges.incoming.first
products = renderer.exchange_products(true, exchange.sender)
expect(products.first.supplier.name).to eq exchange.variants.first.product.supplier.name
@@ -16,14 +17,34 @@ describe ExchangeProductsRenderer do
end
describe "for an outgoing exchange" do
let(:exchange) { order_cycle.exchanges.outgoing.first }
it "loads products" do
exchange = order_cycle.exchanges.outgoing.first
products = renderer.exchange_products(false, exchange.receiver)
suppliers = [exchange.variants[0].product.supplier.name, exchange.variants[1].product.supplier.name]
expect(suppliers).to include products.first.supplier.name
expect(suppliers).to include products.second.supplier.name
end
context "showing products from coordinator inventory only" do
before { order_cycle.update prefers_product_selection_from_coordinator_inventory_only: true }
it "loads no products if there are no products from the coordinator inventory" do
products = renderer.exchange_products(false, exchange.receiver)
expect(products).to be_empty
end
it "loads products from the coordinator inventory" do
# Add variant already in the exchange to the coordinator's inventory
exchange.variants.first.inventory_items = [create(:inventory_item, enterprise: order_cycle.coordinator)]
products = renderer.exchange_products(false, exchange.receiver)
expect(products).to eq [exchange.variants.first.product]
end
end
end
end