mirror of
https://github.com/openfoodfoundation/openfoodnetwork
synced 2026-02-03 22:06:07 +00:00
Remove supplied products from enterprise serializer and from UI side
This list of products s now loaded in a specific call to ExchangeProducts and for each specific exchange
This commit is contained in:
@@ -4,7 +4,6 @@ angular.module('admin.orderCycles')
|
||||
|
||||
$scope.supplier_enterprises = Enterprise.producer_enterprises
|
||||
$scope.distributor_enterprises = Enterprise.hub_enterprises
|
||||
$scope.supplied_products = Enterprise.supplied_products
|
||||
|
||||
$scope.exchangeSelectedVariants = (exchange) ->
|
||||
OrderCycle.exchangeSelectedVariants(exchange)
|
||||
|
||||
@@ -12,7 +12,6 @@ angular.module('admin.orderCycles').factory('Enterprise', ($resource) ->
|
||||
enterprises: {}
|
||||
producer_enterprises: []
|
||||
hub_enterprises: []
|
||||
supplied_products: []
|
||||
loaded: false
|
||||
|
||||
index: (params={}, callback=null) ->
|
||||
@@ -22,9 +21,6 @@ angular.module('admin.orderCycles').factory('Enterprise', ($resource) ->
|
||||
@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)
|
||||
|
||||
@loaded = true
|
||||
(callback || angular.noop)(@enterprises)
|
||||
|
||||
|
||||
@@ -1,7 +1,7 @@
|
||||
require 'open_food_network/enterprise_issue_validator'
|
||||
|
||||
class Api::Admin::ForOrderCycle::EnterpriseSerializer < ActiveModel::Serializer
|
||||
attributes :id, :name, :managed, :supplied_products,
|
||||
attributes :id, :name, :managed,
|
||||
:issues_summary_supplier, :issues_summary_distributor,
|
||||
:is_primary_producer, :is_distributor, :sells
|
||||
|
||||
@@ -25,12 +25,6 @@ class Api::Admin::ForOrderCycle::EnterpriseSerializer < ActiveModel::Serializer
|
||||
Enterprise.managed_by(options[:spree_current_user]).include? object
|
||||
end
|
||||
|
||||
def supplied_products
|
||||
serializer = Api::Admin::ForOrderCycle::SuppliedProductSerializer
|
||||
ActiveModel::ArraySerializer.new(products, each_serializer: serializer,
|
||||
order_cycle: order_cycle)
|
||||
end
|
||||
|
||||
private
|
||||
|
||||
def products_scope
|
||||
|
||||
@@ -25,8 +25,7 @@ describe 'AdminOrderCycleExchangesCtrl', ->
|
||||
addExchangeFee: jasmine.createSpy('addExchangeFee')
|
||||
removeExchangeFee: jasmine.createSpy('removeExchangeFee')
|
||||
removeDistributionOfVariant: jasmine.createSpy('removeDistributionOfVariant')
|
||||
Enterprise =
|
||||
supplied_products: 'supplied products'
|
||||
Enterprise = {}
|
||||
EnterpriseFee =
|
||||
forEnterprise: jasmine.createSpy('forEnterprise').and.returnValue('enterprise fees for enterprise')
|
||||
ocInstance = {}
|
||||
@@ -35,9 +34,6 @@ describe 'AdminOrderCycleExchangesCtrl', ->
|
||||
inject ($controller) ->
|
||||
ctrl = $controller 'AdminOrderCycleExchangesCtrl', {$scope: scope, $location: location, OrderCycle: OrderCycle, Enterprise: Enterprise, EnterpriseFee: EnterpriseFee, ocInstance: ocInstance}
|
||||
|
||||
it 'Loads supplied products', ->
|
||||
expect(scope.supplied_products).toEqual('supplied products')
|
||||
|
||||
it 'Delegates exchangeSelectedVariants to OrderCycle', ->
|
||||
expect(scope.exchangeSelectedVariants('exchange')).toEqual('variants selected')
|
||||
expect(OrderCycle.exchangeSelectedVariants).toHaveBeenCalledWith('exchange')
|
||||
|
||||
@@ -8,18 +8,18 @@ describe 'Enterprise service', ->
|
||||
Enterprise = $injector.get('Enterprise')
|
||||
$httpBackend = _$httpBackend_
|
||||
$httpBackend.whenGET('/admin/enterprises/for_order_cycle.json').respond [
|
||||
{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], sells: 'any'}
|
||||
{id: 1, name: 'One', is_primary_producer: true}
|
||||
{id: 2, name: 'Two'}
|
||||
{id: 3, name: 'Three', 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], 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], sells: 'any'})
|
||||
1: new Enterprise.Enterprise({id: 1, name: 'One', is_primary_producer: true})
|
||||
2: new Enterprise.Enterprise({id: 2, name: 'Two'})
|
||||
3: new Enterprise.Enterprise({id: 3, name: 'Three', sells: 'any'})
|
||||
|
||||
it 'reports its loadedness', ->
|
||||
expect(Enterprise.loaded).toBe(false)
|
||||
@@ -30,22 +30,18 @@ describe 'Enterprise service', ->
|
||||
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})]
|
||||
expect(Enterprise.producer_enterprises).toEqual [new Enterprise.Enterprise({id: 1, name: 'One', 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()
|
||||
expect(Enterprise.supplied_products).toEqual [1, 2, 3, 4, 5, 6]
|
||||
expect(Enterprise.hub_enterprises).toEqual [new Enterprise.Enterprise({id: 3, name: 'Three', sells: 'any'})]
|
||||
|
||||
it "finds supplied variants for an enterprise", ->
|
||||
spyOn(Enterprise, 'variantsOf').and.returnValue(10)
|
||||
Enterprise.index()
|
||||
enterprises = Enterprise.index()
|
||||
$httpBackend.flush()
|
||||
Enterprise.enterprises[1].supplied_products = [1, 2]
|
||||
expect(Enterprise.suppliedVariants(1)).toEqual [10, 10]
|
||||
|
||||
describe "finding the variants of a product", ->
|
||||
|
||||
@@ -1,57 +0,0 @@
|
||||
require "spec_helper"
|
||||
|
||||
describe Api::Admin::ForOrderCycle::EnterpriseSerializer do
|
||||
let(:coordinator) { create(:distributor_enterprise) }
|
||||
let(:order_cycle) { double(:order_cycle, coordinator: coordinator) }
|
||||
let(:enterprise) { create(:distributor_enterprise) }
|
||||
|
||||
let(:non_inventory_product) { create(:simple_product, supplier: enterprise) }
|
||||
let!(:non_inventory_variant) { non_inventory_product.variants.first }
|
||||
|
||||
let(:inventory_product) { create(:simple_product, supplier: enterprise) }
|
||||
let(:inventory_variant) { inventory_product.variants.first }
|
||||
|
||||
let(:deleted_product) { create(:product, supplier: enterprise, deleted_at: 24.hours.ago ) }
|
||||
let(:deleted_variant) { deleted_product.variants.first }
|
||||
|
||||
let(:serialized_enterprise) do
|
||||
Api::Admin::ForOrderCycle::EnterpriseSerializer.new(
|
||||
enterprise.reload, # load the products that were created after the enterprise
|
||||
order_cycle: order_cycle,
|
||||
spree_current_user: enterprise.owner
|
||||
).to_json
|
||||
end
|
||||
|
||||
let!(:inventory_item1) { create(:inventory_item, enterprise: coordinator, variant: inventory_variant, visible: true) }
|
||||
let!(:inventory_item2) { create(:inventory_item, enterprise: coordinator, variant: deleted_variant, visible: true) }
|
||||
|
||||
context "when order cycle shows only variants in the coordinator's inventory" do
|
||||
before do
|
||||
allow(order_cycle).to receive(:prefers_product_selection_from_coordinator_inventory_only?) { true }
|
||||
end
|
||||
|
||||
describe "supplied products" do
|
||||
it "renders only non-deleted variants that are in the coordinators inventory" do
|
||||
expect(serialized_enterprise).to have_json_size(1).at_path 'supplied_products'
|
||||
expect(serialized_enterprise).to have_json_size(1).at_path 'supplied_products/0/variants'
|
||||
expect(serialized_enterprise).to be_json_eql(inventory_variant.id).at_path 'supplied_products/0/variants/0/id'
|
||||
end
|
||||
end
|
||||
end
|
||||
|
||||
context "when order cycle shows all available products" do
|
||||
before do
|
||||
allow(order_cycle).to receive(:prefers_product_selection_from_coordinator_inventory_only?) { false }
|
||||
end
|
||||
|
||||
describe "supplied products" do
|
||||
it "renders variants that are not in the coordinators inventory but not variants of deleted products" do
|
||||
expect(serialized_enterprise).to have_json_size(2).at_path 'supplied_products'
|
||||
expect(serialized_enterprise).to have_json_size(1).at_path 'supplied_products/0/variants'
|
||||
expect(serialized_enterprise).to have_json_size(1).at_path 'supplied_products/1/variants'
|
||||
variant_ids = parse_json(serialized_enterprise)['supplied_products'].map{ |p| p['variants'].first['id'] }
|
||||
expect(variant_ids).to include non_inventory_variant.id, inventory_variant.id
|
||||
end
|
||||
end
|
||||
end
|
||||
end
|
||||
Reference in New Issue
Block a user