mirror of
https://github.com/openfoodfoundation/openfoodnetwork
synced 2026-04-03 06:59:14 +00:00
Make simple create and edit OC load exchange products on init
Simple create makes a new type of call to exchange products with no exchange_id and no prder_cycle_id, it simply lists supplied products for a given enterprise
This commit is contained in:
@@ -1,23 +1,33 @@
|
||||
angular.module('admin.orderCycles').controller "AdminSimpleCreateOrderCycleCtrl", ($scope, $controller, $window, OrderCycle, Enterprise, EnterpriseFee, StatusMessage, Schedules, RequestMonitor, ocInstance) ->
|
||||
angular.module('admin.orderCycles').controller "AdminSimpleCreateOrderCycleCtrl", ($scope, $controller, $window, OrderCycle, Enterprise, EnterpriseFee, Product, StatusMessage, Schedules, RequestMonitor, ocInstance) ->
|
||||
$controller('AdminOrderCycleBasicCtrl', {$scope: $scope, ocInstance: ocInstance})
|
||||
|
||||
$scope.order_cycle = OrderCycle.new {coordinator_id: ocInstance.coordinator_id}, =>
|
||||
# TODO: make this a get method, which only fetches one enterprise
|
||||
$scope.enterprises = Enterprise.index {coordinator_id: ocInstance.coordinator_id}, (enterprises) =>
|
||||
$scope.init(enterprises)
|
||||
$scope.enterprise_fees = EnterpriseFee.index(coordinator_id: ocInstance.coordinator_id)
|
||||
|
||||
$scope.init = (enterprises) ->
|
||||
enterprise = enterprises[Object.keys(enterprises)[0]]
|
||||
OrderCycle.addSupplier enterprise.id
|
||||
OrderCycle.addDistributor enterprise.id
|
||||
OrderCycle.order_cycle.coordinator_id = enterprise.id
|
||||
|
||||
OrderCycle.addDistributor enterprise.id, $scope.setOutgoingExchange
|
||||
OrderCycle.addSupplier enterprise.id, $scope.loadExchangeProducts
|
||||
|
||||
$scope.setOutgoingExchange = ->
|
||||
$scope.outgoing_exchange = OrderCycle.order_cycle.outgoing_exchanges[0]
|
||||
|
||||
# All variants start as checked
|
||||
OrderCycle.setExchangeVariants(OrderCycle.order_cycle.incoming_exchanges[0],
|
||||
Enterprise.suppliedVariants(enterprise.id), true)
|
||||
$scope.loadExchangeProducts = ->
|
||||
$scope.incoming_exchange = OrderCycle.order_cycle.incoming_exchanges[0]
|
||||
|
||||
OrderCycle.order_cycle.coordinator_id = enterprise.id
|
||||
params = { enterprise_id: $scope.incoming_exchange.enterprise_id, incoming: true }
|
||||
Product.index params, $scope.storeProductsAndSelectAllVariants
|
||||
|
||||
$scope.storeProductsAndSelectAllVariants = (products) ->
|
||||
$scope.enterprises[$scope.incoming_exchange.enterprise_id].supplied_products = products
|
||||
|
||||
# All variants start as checked
|
||||
OrderCycle.setExchangeVariants($scope.incoming_exchange,
|
||||
Enterprise.suppliedVariants($scope.incoming_exchange.enterprise_id), true)
|
||||
|
||||
$scope.removeDistributionOfVariant = angular.noop
|
||||
|
||||
|
||||
@@ -1,4 +1,4 @@
|
||||
angular.module('admin.orderCycles').controller "AdminSimpleEditOrderCycleCtrl", ($scope, $controller, $location, $window, OrderCycle, Enterprise, EnterpriseFee, Schedules, RequestMonitor, StatusMessage, ocInstance) ->
|
||||
angular.module('admin.orderCycles').controller "AdminSimpleEditOrderCycleCtrl", ($scope, $controller, $location, $window, OrderCycle, Enterprise, EnterpriseFee, Product, Schedules, RequestMonitor, StatusMessage, ocInstance) ->
|
||||
$controller('AdminOrderCycleBasicCtrl', {$scope: $scope, ocInstance: ocInstance})
|
||||
|
||||
$scope.orderCycleId = ->
|
||||
@@ -11,6 +11,12 @@ angular.module('admin.orderCycles').controller "AdminSimpleEditOrderCycleCtrl",
|
||||
|
||||
$scope.init = ->
|
||||
$scope.outgoing_exchange = OrderCycle.order_cycle.outgoing_exchanges[0]
|
||||
$scope.loadExchangeProducts()
|
||||
|
||||
$scope.loadExchangeProducts = ->
|
||||
exchange = OrderCycle.order_cycle.incoming_exchanges[0]
|
||||
Product.index { exchange_id: exchange.id }, (products) ->
|
||||
$scope.enterprises[exchange.enterprise_id].supplied_products = products
|
||||
|
||||
$scope.removeDistributionOfVariant = angular.noop
|
||||
|
||||
|
||||
@@ -47,12 +47,12 @@ angular.module('admin.orderCycles').factory 'OrderCycle', ($resource, $window, $
|
||||
addSupplier: (new_supplier_id, callback) ->
|
||||
this.order_cycle.incoming_exchanges.push({enterprise_id: new_supplier_id, incoming: true, active: true, variants: {}, enterprise_fees: []})
|
||||
$timeout ->
|
||||
callback()
|
||||
(callback || angular.noop)()
|
||||
|
||||
addDistributor: (new_distributor_id, callback) ->
|
||||
this.order_cycle.outgoing_exchanges.push({ enterprise_id: new_distributor_id, incoming: false, active: true, variants: {}, enterprise_fees: [] })
|
||||
$timeout ->
|
||||
callback()
|
||||
(callback || angular.noop)()
|
||||
|
||||
removeExchange: (exchange) ->
|
||||
if exchange.incoming
|
||||
|
||||
@@ -4,7 +4,12 @@ module Api
|
||||
|
||||
def show
|
||||
enterprise = Enterprise.find_by_id(params[:enterprise_id])
|
||||
@order_cycle = OrderCycle.find_by_id(params[:order_cycle_id])
|
||||
|
||||
if params[:order_cycle_id]
|
||||
@order_cycle = OrderCycle.find_by_id(params[:order_cycle_id])
|
||||
elsif !params[:incoming]
|
||||
raise "order_cycle_id is required to list products for new outgoing exchange"
|
||||
end
|
||||
|
||||
render json: exchange_products(params[:incoming], enterprise),
|
||||
each_serializer: Api::Admin::ForOrderCycle::SuppliedProductSerializer,
|
||||
@@ -33,12 +38,12 @@ module Api
|
||||
end
|
||||
|
||||
def products_for_incoming_exchange(enterprise)
|
||||
supplied_products(@order_cycle, enterprise)
|
||||
supplied_products(enterprise)
|
||||
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)
|
||||
def supplied_products(enterprise)
|
||||
if @order_cycle.present? && @order_cycle.prefers_product_selection_from_coordinator_inventory_only?
|
||||
enterprise.supplied_products.visible_for(@order_cycle.coordinator)
|
||||
else
|
||||
enterprise.supplied_products
|
||||
end
|
||||
@@ -47,7 +52,7 @@ module Api
|
||||
def products_for_outgoing_exchange
|
||||
products = []
|
||||
enterprises_for_outgoing_exchange.each do |enterprise|
|
||||
products.push( *supplied_products(@order_cycle, enterprise).to_a )
|
||||
products.push( *supplied_products(enterprise).to_a )
|
||||
|
||||
products.each do |product|
|
||||
unless product_supplied_to_order_cycle?(product)
|
||||
|
||||
@@ -14,7 +14,7 @@ class Api::Admin::ForOrderCycle::SuppliedProductSerializer < ActiveModel::Serial
|
||||
end
|
||||
|
||||
def variants
|
||||
variants = if order_cycle.prefers_product_selection_from_coordinator_inventory_only?
|
||||
variants = if order_cycle.present? && order_cycle.prefers_product_selection_from_coordinator_inventory_only?
|
||||
object.variants.visible_for(order_cycle.coordinator)
|
||||
else
|
||||
object.variants
|
||||
|
||||
@@ -1,8 +1,9 @@
|
||||
describe "AdminSimpleCreateOrderCycleCtrl", ->
|
||||
ctrl = null
|
||||
scope = {}
|
||||
scope = null
|
||||
OrderCycle = {}
|
||||
Enterprise = {}
|
||||
Product = {}
|
||||
EnterpriseFee = {}
|
||||
incoming_exchange = {}
|
||||
outgoing_exchange = {}
|
||||
@@ -10,27 +11,32 @@ describe "AdminSimpleCreateOrderCycleCtrl", ->
|
||||
beforeEach ->
|
||||
scope =
|
||||
$watch: jasmine.createSpy('$watch')
|
||||
setOutgoingExchange: jasmine.createSpy('setOutgoingExchange')
|
||||
loadExchangeProducts: jasmine.createSpy('loadExchangeProducts')
|
||||
storeProductsAndSelectAllVariants: jasmine.createSpy('storeProductsAndSelectAllVariants')
|
||||
order_cycle =
|
||||
coordinator_id: 123
|
||||
incoming_exchanges: [incoming_exchange]
|
||||
outgoing_exchanges: [outgoing_exchange]
|
||||
OrderCycle =
|
||||
order_cycle: order_cycle
|
||||
addSupplier: jasmine.createSpy()
|
||||
addDistributor: jasmine.createSpy()
|
||||
setExchangeVariants: jasmine.createSpy()
|
||||
addSupplier: jasmine.createSpy('addSupplier')
|
||||
addDistributor: jasmine.createSpy('addDistributor')
|
||||
setExchangeVariants: jasmine.createSpy('setExchangeVariants')
|
||||
new: jasmine.createSpy().and.returnValue order_cycle
|
||||
Enterprise =
|
||||
get: jasmine.createSpy().and.returnValue {id: 123}
|
||||
index: jasmine.createSpy()
|
||||
suppliedVariants: jasmine.createSpy().and.returnValue('supplied variants')
|
||||
Product =
|
||||
index: jasmine.createSpy()
|
||||
EnterpriseFee =
|
||||
index: jasmine.createSpy()
|
||||
ocInstance = {}
|
||||
|
||||
module('admin.orderCycles')
|
||||
inject ($controller) ->
|
||||
ctrl = $controller 'AdminSimpleCreateOrderCycleCtrl', {$scope: scope, OrderCycle: OrderCycle, Enterprise: Enterprise, EnterpriseFee: EnterpriseFee, ocInstance: ocInstance}
|
||||
ctrl = $controller 'AdminSimpleCreateOrderCycleCtrl', {$scope: scope, OrderCycle: OrderCycle, Enterprise: Enterprise, EnterpriseFee: EnterpriseFee, Product: Product, ocInstance: ocInstance}
|
||||
|
||||
describe "initialisation", ->
|
||||
enterprise = {id: 123}
|
||||
@@ -39,15 +45,26 @@ describe "AdminSimpleCreateOrderCycleCtrl", ->
|
||||
beforeEach ->
|
||||
scope.init(enterprises)
|
||||
|
||||
it "sets up an incoming and outgoing exchange", ->
|
||||
expect(OrderCycle.addSupplier).toHaveBeenCalledWith(enterprise.id)
|
||||
expect(OrderCycle.addDistributor).toHaveBeenCalledWith(enterprise.id)
|
||||
expect(scope.outgoing_exchange).toEqual outgoing_exchange
|
||||
it "adds enterprise as both the supplier and the distributor", ->
|
||||
expect(OrderCycle.addSupplier).toHaveBeenCalledWith(enterprise.id, scope.loadExchangeProducts)
|
||||
expect(OrderCycle.addDistributor).toHaveBeenCalledWith(enterprise.id, scope.setOutgoingExchange)
|
||||
|
||||
it "loads exchange products", ->
|
||||
incoming_exchange.enterprise_id = enterprise.id
|
||||
|
||||
scope.loadExchangeProducts()
|
||||
|
||||
expect(Product.index).toHaveBeenCalledWith({ enterprise_id: enterprise.id, incoming: true }, scope.storeProductsAndSelectAllVariants)
|
||||
|
||||
it "stores products and selects all variants", ->
|
||||
scope.incoming_exchange = incoming_exchange
|
||||
incoming_exchange.enterprise_id = enterprise.id
|
||||
scope.enterprises = { "#{enterprise.id}": {}}
|
||||
|
||||
scope.storeProductsAndSelectAllVariants()
|
||||
|
||||
it "selects all variants", ->
|
||||
expect(Enterprise.suppliedVariants).
|
||||
toHaveBeenCalledWith(enterprise.id)
|
||||
|
||||
expect(OrderCycle.setExchangeVariants).
|
||||
toHaveBeenCalledWith(incoming_exchange, 'supplied variants', true)
|
||||
|
||||
|
||||
Reference in New Issue
Block a user