mirror of
https://github.com/openfoodfoundation/openfoodnetwork
synced 2026-01-24 20:36:49 +00:00
Adding 'new' method to admin order cycle service, for loading data from serializer for new OCs
This commit is contained in:
@@ -4,8 +4,7 @@ angular.module('admin.order_cycles', ['ngResource'])
|
||||
$scope.supplied_products = Enterprise.supplied_products
|
||||
$scope.enterprise_fees = EnterpriseFee.index()
|
||||
|
||||
$scope.order_cycle = OrderCycle.order_cycle
|
||||
$scope.order_cycle.coordinator_id = ocInstance.coordinator_id
|
||||
$scope.order_cycle = OrderCycle.new({ coordinator_id: ocInstance.coordinator_id})
|
||||
|
||||
$scope.loaded = ->
|
||||
Enterprise.loaded && EnterpriseFee.loaded
|
||||
|
||||
@@ -1,14 +1,12 @@
|
||||
angular.module('admin.order_cycles').factory('OrderCycle', ($resource, $window) ->
|
||||
OrderCycle = $resource '/admin/order_cycles/:order_cycle_id.json', {}, {
|
||||
OrderCycle = $resource '/admin/order_cycles/:action_name/:order_cycle_id.json', {}, {
|
||||
'index': { method: 'GET', isArray: true}
|
||||
'new' : { method: 'GET', params: { action_name: "new" } }
|
||||
'create': { method: 'POST'}
|
||||
'update': { method: 'PUT'}}
|
||||
|
||||
{
|
||||
order_cycle:
|
||||
incoming_exchanges: []
|
||||
outgoing_exchanges: []
|
||||
coordinator_fees: []
|
||||
order_cycle: {}
|
||||
|
||||
loaded: false
|
||||
|
||||
@@ -84,6 +82,20 @@ angular.module('admin.order_cycles').factory('OrderCycle', ($resource, $window)
|
||||
for exchange in this.order_cycle.outgoing_exchanges
|
||||
exchange.variants[variant_id] = false
|
||||
|
||||
new: (params, callback=null) ->
|
||||
OrderCycle.new params, (oc) =>
|
||||
delete oc.$promise
|
||||
delete oc.$resolved
|
||||
angular.extend(@order_cycle, oc)
|
||||
@order_cycle.incoming_exchanges = []
|
||||
@order_cycle.outgoing_exchanges = []
|
||||
delete(@order_cycle.exchanges)
|
||||
@loaded = true
|
||||
|
||||
(callback || angular.noop)(@order_cycle)
|
||||
|
||||
@order_cycle
|
||||
|
||||
load: (order_cycle_id, callback=null) ->
|
||||
service = this
|
||||
OrderCycle.get {order_cycle_id: order_cycle_id}, (oc) ->
|
||||
|
||||
@@ -22,7 +22,9 @@ module Admin
|
||||
def new
|
||||
respond_to do |format|
|
||||
format.html
|
||||
format.json
|
||||
format.json do
|
||||
render json: Api::Admin::OrderCycleSerializer.new(@order_cycle, current_user: spree_current_user).to_json
|
||||
end
|
||||
end
|
||||
end
|
||||
|
||||
|
||||
@@ -13,7 +13,6 @@ describe 'OrderCycle controllers', ->
|
||||
event =
|
||||
preventDefault: jasmine.createSpy('preventDefault')
|
||||
OrderCycle =
|
||||
order_cycle: 'my order cycle'
|
||||
exchangeSelectedVariants: jasmine.createSpy('exchangeSelectedVariants').andReturn('variants selected')
|
||||
productSuppliedToOrderCycle: jasmine.createSpy('productSuppliedToOrderCycle').andReturn('product supplied')
|
||||
variantSuppliedToOrderCycle: jasmine.createSpy('variantSuppliedToOrderCycle').andReturn('variant supplied')
|
||||
@@ -29,6 +28,7 @@ describe 'OrderCycle controllers', ->
|
||||
removeExchangeFee: jasmine.createSpy('removeExchangeFee')
|
||||
removeDistributionOfVariant: jasmine.createSpy('removeDistributionOfVariant')
|
||||
create: jasmine.createSpy('create')
|
||||
new: jasmine.createSpy('new').andReturn "my order cycle"
|
||||
Enterprise =
|
||||
index: jasmine.createSpy('index').andReturn('enterprises list')
|
||||
supplied_products: 'supplied products'
|
||||
@@ -450,12 +450,15 @@ describe 'OrderCycle services', ->
|
||||
{sender_id: 1, receiver_id: 456, incoming: true}
|
||||
{sender_id: 456, receiver_id: 2, incoming: false}
|
||||
]
|
||||
$httpBackend.whenGET('/admin/order_cycles/new.json').respond
|
||||
id: 123
|
||||
name: 'New Order Cycle'
|
||||
coordinator_id: 456
|
||||
coordinator_fees: []
|
||||
exchanges: []
|
||||
|
||||
it 'initialises order cycle', ->
|
||||
expect(OrderCycle.order_cycle).toEqual
|
||||
incoming_exchanges: []
|
||||
outgoing_exchanges: []
|
||||
coordinator_fees: []
|
||||
expect(OrderCycle.order_cycle).toEqual {}
|
||||
|
||||
it 'counts selected variants in an exchange', ->
|
||||
result = OrderCycle.exchangeSelectedVariants({variants: {1: true, 2: false, 3: true}})
|
||||
@@ -503,6 +506,11 @@ describe 'OrderCycle services', ->
|
||||
describe 'adding suppliers', ->
|
||||
exchange = null
|
||||
|
||||
beforeEach ->
|
||||
# Initialise OC
|
||||
OrderCycle.new()
|
||||
$httpBackend.flush()
|
||||
|
||||
it 'adds the supplier to incoming exchanges', ->
|
||||
OrderCycle.addSupplier('123')
|
||||
expect(OrderCycle.order_cycle.incoming_exchanges).toEqual [
|
||||
@@ -512,6 +520,11 @@ describe 'OrderCycle services', ->
|
||||
describe 'adding distributors', ->
|
||||
exchange = null
|
||||
|
||||
beforeEach ->
|
||||
# Initialise OC
|
||||
OrderCycle.new()
|
||||
$httpBackend.flush()
|
||||
|
||||
it 'adds the distributor to outgoing exchanges', ->
|
||||
OrderCycle.addDistributor('123')
|
||||
expect(OrderCycle.order_cycle.outgoing_exchanges).toEqual [
|
||||
@@ -559,6 +572,9 @@ describe 'OrderCycle services', ->
|
||||
expect(OrderCycle.removeDistributionOfVariant).not.toHaveBeenCalled()
|
||||
|
||||
it 'adds coordinator fees', ->
|
||||
# Initialise OC
|
||||
OrderCycle.new()
|
||||
$httpBackend.flush()
|
||||
OrderCycle.addCoordinatorFee()
|
||||
expect(OrderCycle.order_cycle.coordinator_fees).toEqual [{}]
|
||||
|
||||
@@ -680,7 +696,25 @@ describe 'OrderCycle services', ->
|
||||
$httpBackend.flush()
|
||||
expect(OrderCycle.loaded).toBe(true)
|
||||
|
||||
describe 'loading an order cycle', ->
|
||||
describe 'loading a new order cycle', ->
|
||||
beforeEach ->
|
||||
OrderCycle.new()
|
||||
$httpBackend.flush()
|
||||
|
||||
|
||||
it 'loads basic fields', ->
|
||||
expect(OrderCycle.order_cycle.id).toEqual(123)
|
||||
expect(OrderCycle.order_cycle.name).toEqual('New Order Cycle')
|
||||
expect(OrderCycle.order_cycle.coordinator_id).toEqual(456)
|
||||
|
||||
it 'initialises the incoming and outgoing exchanges', ->
|
||||
expect(OrderCycle.order_cycle.incoming_exchanges).toEqual []
|
||||
expect(OrderCycle.order_cycle.outgoing_exchanges).toEqual []
|
||||
|
||||
it 'removes the original exchanges array', ->
|
||||
expect(OrderCycle.order_cycle.exchanges).toBeUndefined()
|
||||
|
||||
describe 'loading an existing order cycle', ->
|
||||
beforeEach ->
|
||||
OrderCycle.load('123')
|
||||
$httpBackend.flush()
|
||||
@@ -705,8 +739,8 @@ describe 'OrderCycle services', ->
|
||||
active: true
|
||||
]
|
||||
|
||||
it 'removes original exchanges array', ->
|
||||
expect(OrderCycle.order_cycle.exchanges).toEqual(undefined)
|
||||
it 'removes the original exchanges array', ->
|
||||
expect(OrderCycle.order_cycle.exchanges).toBeUndefined()
|
||||
|
||||
describe 'creating an order cycle', ->
|
||||
it 'redirects to the order cycles page on success', ->
|
||||
|
||||
Reference in New Issue
Block a user