mirror of
https://github.com/openfoodfoundation/openfoodnetwork
synced 2026-01-24 20:36:49 +00:00
Enterprise service splits enterprises into hubs and producers
This commit is contained in:
@@ -10,6 +10,8 @@ angular.module('admin.orderCycles').factory('Enterprise', ($resource) ->
|
||||
{
|
||||
Enterprise: Enterprise
|
||||
enterprises: {}
|
||||
producer_enterprises: []
|
||||
hub_enterprises: []
|
||||
supplied_products: []
|
||||
loaded: false
|
||||
|
||||
@@ -17,6 +19,8 @@ angular.module('admin.orderCycles').factory('Enterprise', ($resource) ->
|
||||
Enterprise.index params, (data) =>
|
||||
for enterprise in data
|
||||
@enterprises[enterprise.id] = enterprise
|
||||
@producer_enterprises.push(enterprise) if enterprise.is_primary_producer
|
||||
@hub_enterprises.push(enterprise) if enterprise.sells == 'any'
|
||||
|
||||
for product in enterprise.supplied_products
|
||||
@supplied_products.push(product)
|
||||
|
||||
@@ -1,5 +1,6 @@
|
||||
class Api::Admin::ForOrderCycle::EnterpriseSerializer < ActiveModel::Serializer
|
||||
attributes :id, :name, :managed, :supplied_products
|
||||
attributes :is_primary_producer, :is_distributor, :sells
|
||||
|
||||
def managed
|
||||
Enterprise.managed_by(options[:spree_current_user]).include? object
|
||||
@@ -8,6 +9,6 @@ class Api::Admin::ForOrderCycle::EnterpriseSerializer < ActiveModel::Serializer
|
||||
def supplied_products
|
||||
objects = object.supplied_products.not_deleted
|
||||
serializer = Api::Admin::ForOrderCycle::SuppliedProductSerializer
|
||||
ActiveModel::ArraySerializer.new(objects, each_serializer: serializer )
|
||||
ActiveModel::ArraySerializer.new(objects, each_serializer: serializer)
|
||||
end
|
||||
end
|
||||
|
||||
@@ -333,18 +333,18 @@ describe 'OrderCycle services', ->
|
||||
Enterprise = $injector.get('Enterprise')
|
||||
$httpBackend = _$httpBackend_
|
||||
$httpBackend.whenGET('/admin/enterprises/for_order_cycle.json?').respond [
|
||||
{id: 1, name: 'One', supplied_products: [1, 2]}
|
||||
{id: 1, name: 'One', supplied_products: [1, 2], is_primary_producer: true}
|
||||
{id: 2, name: 'Two', supplied_products: [3, 4]}
|
||||
{id: 3, name: 'Three', supplied_products: [5, 6]}
|
||||
{id: 3, name: 'Three', supplied_products: [5, 6], sells: 'any'}
|
||||
]
|
||||
|
||||
it 'loads enterprises as a hash', ->
|
||||
enterprises = Enterprise.index()
|
||||
$httpBackend.flush()
|
||||
expect(enterprises).toEqual
|
||||
1: new Enterprise.Enterprise({id: 1, name: 'One', supplied_products: [1, 2]})
|
||||
1: new Enterprise.Enterprise({id: 1, name: 'One', supplied_products: [1, 2], is_primary_producer: true})
|
||||
2: new Enterprise.Enterprise({id: 2, name: 'Two', supplied_products: [3, 4]})
|
||||
3: new Enterprise.Enterprise({id: 3, name: 'Three', supplied_products: [5, 6]})
|
||||
3: new Enterprise.Enterprise({id: 3, name: 'Three', supplied_products: [5, 6], sells: 'any'})
|
||||
|
||||
it 'reports its loadedness', ->
|
||||
expect(Enterprise.loaded).toBe(false)
|
||||
@@ -352,6 +352,16 @@ describe 'OrderCycle services', ->
|
||||
$httpBackend.flush()
|
||||
expect(Enterprise.loaded).toBe(true)
|
||||
|
||||
it 'loads producers as an array', ->
|
||||
Enterprise.index()
|
||||
$httpBackend.flush()
|
||||
expect(Enterprise.producer_enterprises).toEqual [new Enterprise.Enterprise({id: 1, name: 'One', supplied_products: [1, 2], is_primary_producer: true})]
|
||||
|
||||
it 'loads hubs as an array', ->
|
||||
Enterprise.index()
|
||||
$httpBackend.flush()
|
||||
expect(Enterprise.hub_enterprises).toEqual [new Enterprise.Enterprise({id: 3, name: 'Three', supplied_products: [5, 6], sells: 'any'})]
|
||||
|
||||
it 'collates all supplied products', ->
|
||||
enterprises = Enterprise.index()
|
||||
$httpBackend.flush()
|
||||
|
||||
Reference in New Issue
Block a user