Add new admin/exchange/products endpoint that replaces Enterprise/for_order_cycle#supplied_products

This commit is contained in:
luisramos0
2019-11-12 18:16:29 +00:00
parent 883cd81058
commit 9adbdc377d
4 changed files with 62 additions and 4 deletions

View File

@@ -1,5 +1,5 @@
angular.module('admin.orderCycles')
.controller 'AdminOrderCycleExchangesCtrl', ($scope, $controller, $filter, $window, $location, $timeout, OrderCycle, Enterprise, EnterpriseFee, Schedules, RequestMonitor, ocInstance, StatusMessage) ->
.controller 'AdminOrderCycleExchangesCtrl', ($scope, $controller, $filter, $window, $location, $timeout, OrderCycle, Product, Enterprise, EnterpriseFee, Schedules, RequestMonitor, ocInstance, StatusMessage) ->
$controller('AdminEditOrderCycleCtrl', {$scope: $scope, ocInstance: ocInstance, $location: $location})
$scope.supplier_enterprises = Enterprise.producer_enterprises
@@ -46,9 +46,9 @@ angular.module('admin.orderCycles')
OrderCycle.removeDistributionOfVariant(variant_id)
# Load exchange data
initPanel = (scope) ->
# TEMP Insert some dummy data into enterprise.supplied_products so that the UI keeps on working
scope.enterprises[scope.exchange.enterprise_id].supplied_products = [{id: 2, name: "asdasd" + scope.exchange.id }]
initPanel = (scope) ->
Product.index {exchange_id: scope.exchange.id, enterprise_id: scope.exchange.enterprise_id}, (products) ->
scope.enterprises[scope.exchange.enterprise_id].supplied_products = products
# Register listener to capture first toggle open of the products panel
exchangeProdutsInitialized = []

View File

@@ -0,0 +1,22 @@
angular.module('admin.orderCycles').factory('Product', ($resource) ->
Product = $resource('/admin/exchanges/:exchange_id/products.json', {}, {
'index':
method: 'GET'
isArray: true
})
{
Product: Product
products: {}
loaded: false
index: (params={}, callback=null) ->
Product.index params, (data) =>
@products[params.enterprise_id] = []
for product in data
@products[params.enterprise_id].push(product)
@loaded = true
(callback || angular.noop)(@products[params.enterprise_id])
this.products
})

View File

@@ -0,0 +1,32 @@
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

@@ -18,6 +18,10 @@ 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