Move exchange products endpoint to api namespace and make it work for outgoing exchanges

This commit is contained in:
luisramos0
2019-11-13 13:51:25 +00:00
parent 9adbdc377d
commit 89628c27f3
6 changed files with 138 additions and 37 deletions

View File

@@ -1,5 +1,5 @@
angular.module('admin.orderCycles').factory('Product', ($resource) ->
Product = $resource('/admin/exchanges/:exchange_id/products.json', {}, {
Product = $resource('/api/exchanges/:exchange_id/products.json', {}, {
'index':
method: 'GET'
isArray: true

View File

@@ -1,32 +0,0 @@
module Admin
class ExchangesProductsController < Spree::Admin::BaseController
def index
@exchange = Exchange.find_by_id(params[:exchange_id])
respond_to do |format|
format.json do
render json: exchange_products,
each_serializer: Api::Admin::ForOrderCycle::SuppliedProductSerializer,
order_cycle: @exchange.order_cycle
end
end
end
private
# So far, products for incoming exchanges only
def exchange_products
return [] unless @exchange.incoming
products_for_incoming_exchange
end
def products_for_incoming_exchange
if @exchange.order_cycle.prefers_product_selection_from_coordinator_inventory_only?
@exchange.sender.supplied_products.visible_for(@order_cycle.coordinator)
else
@exchange.sender.supplied_products
end
end
end
end

View File

@@ -0,0 +1,97 @@
module Api
class ExchangesProductsController < Api::BaseController
skip_authorization_check only: [:index]
def index
@exchange = Exchange.find_by_id(params[:exchange_id])
render json: exchange_products,
each_serializer: Api::Admin::ForOrderCycle::SuppliedProductSerializer,
order_cycle: @exchange.order_cycle,
status: :ok
end
private
def exchange_products
if @exchange.incoming
products_for_incoming_exchange
else
products_for_outgoing_exchange
end
end
def products_for_incoming_exchange
supplied_products(@exchange.order_cycle, @exchange.sender)
end
def supplied_products(order_cycle, enterprise)
if order_cycle.prefers_product_selection_from_coordinator_inventory_only?
enterprise.supplied_products.visible_for(order_cycle.coordinator)
else
enterprise.supplied_products
end
end
def products_for_outgoing_exchange
products = []
enterprises_for_outgoing_exchange.each do |enterprise|
products.push( *supplied_products(@exchange.order_cycle, enterprise).to_a )
products.each do |product|
unless product_supplied_to_order_cycle?(product)
products.delete(product)
end
end
end
products
end
def product_supplied_to_order_cycle?(product)
(product.variants.map(&:id) & incoming_exchanges_variants).any?
end
def incoming_exchanges_variants
return @incoming_exchanges_variants if @incoming_exchanges_variants.present?
scoped_exchanges =
OpenFoodNetwork::OrderCyclePermissions.
new(spree_current_user, @exchange.order_cycle).
visible_exchanges.
by_enterprise_name.
incoming
@incoming_exchanges_variants = []
scoped_exchanges.each do |exchange|
@incoming_exchanges_variants.push(
*exchange.variants.merge(visible_incoming_variants(exchange)).map(&:id).to_a
)
end
@incoming_exchanges_variants
end
def visible_incoming_variants(exchange)
if exchange.order_cycle.prefers_product_selection_from_coordinator_inventory_only?
permitted_incoming_variants(exchange).visible_for(exchange.order_cycle.coordinator)
else
permitted_incoming_variants(exchange)
end
end
def permitted_incoming_variants(exchange)
OpenFoodNetwork::OrderCyclePermissions.new(spree_current_user, exchange.order_cycle).
visible_variants_for_incoming_exchanges_from(exchange.sender)
end
def enterprises_for_outgoing_exchange
enterprises = OpenFoodNetwork::OrderCyclePermissions.
new(spree_current_user, @exchange.order_cycle)
.visible_enterprises
return enterprises if enterprises.empty?
enterprises.includes(
supplied_products: [:supplier, :variants, master: [:images]]
)
end
end
end

View File

@@ -18,10 +18,6 @@ Openfoodnetwork::Application.routes.draw do
end
end
resources :exchanges do
resources :products, controller: 'exchanges_products'
end
resources :enterprises do
collection do
get :for_order_cycle

View File

@@ -43,6 +43,10 @@ Openfoodnetwork::Application.routes.draw do
get :properties, on: :member
end
resources :exchanges do
resources :products, controller: 'exchanges_products'
end
resource :status do
get :job_queue
end

View File

@@ -0,0 +1,36 @@
require 'spec_helper'
module Api
describe ExchangesProductsController, type: :controller do
include AuthenticationWorkflow
let!(:order_cycle) { create(:order_cycle) }
let!(:coordinator) { order_cycle.coordinator }
before do
allow(controller).to receive_messages spree_current_user: coordinator.owner
end
describe "#index" do
describe "for incoming exchanges" do
it "loads data" do
exchange = order_cycle.exchanges.incoming.first
spree_get :index, exchange_id: exchange.id
expect(json_response.first["supplier_name"]).to eq exchange.variants.first.product.supplier.name
end
end
describe "for outgoing exchanges" do
it "loads data" do
exchange = order_cycle.exchanges.outgoing.first
spree_get :index, exchange_id: exchange.id
suppliers = [exchange.variants[0].product.supplier.name, exchange.variants[1].product.supplier.name]
expect(suppliers).to include json_response.first["supplier_name"]
expect(suppliers).to include json_response.second["supplier_name"]
end
end
end
end
end