From 698d3672a636665b9741d52001e601b083824503 Mon Sep 17 00:00:00 2001 From: Matt-Yorkley <9029026+Matt-Yorkley@users.noreply.github.com> Date: Fri, 10 May 2019 01:10:19 +0100 Subject: [PATCH] List only current taxons for active enterprises --- app/models/enterprise.rb | 16 ++++++++++++++++ .../api/enterprise_shopfront_serializer.rb | 4 +++- spec/models/enterprise_spec.rb | 12 ++++++++++++ 3 files changed, 31 insertions(+), 1 deletion(-) diff --git a/app/models/enterprise.rb b/app/models/enterprise.rb index 162031889b..d5e45a3ae5 100644 --- a/app/models/enterprise.rb +++ b/app/models/enterprise.rb @@ -285,6 +285,14 @@ class Enterprise < ActiveRecord::Base select('DISTINCT spree_taxons.*') end + def current_distributed_taxons + Spree::Taxon + .select("DISTINCT spree_taxons.*") + .joins(products: :variants_including_master) + .joins("INNER JOIN (#{current_exchange_variants.to_sql}) \ + AS exchange_variants ON spree_variants.id = exchange_variants.variant_id") + end + # Return all taxons for all supplied products def supplied_taxons Spree::Taxon. @@ -325,6 +333,14 @@ class Enterprise < ActiveRecord::Base private + def current_exchange_variants + ExchangeVariant.joins(exchange: :order_cycle) + .merge(Exchange.outgoing) + .select("DISTINCT exchange_variants.variant_id, exchanges.receiver_id AS enterprise_id") + .where("exchanges.receiver_id = ?", id) + .merge(OrderCycle.active.with_distributor(id)) + end + def name_is_unique dups = Enterprise.where(name: name) dups = dups.where('id != ?', id) unless new_record? diff --git a/app/serializers/api/enterprise_shopfront_serializer.rb b/app/serializers/api/enterprise_shopfront_serializer.rb index 6b800ffcd7..e7b645def6 100644 --- a/app/serializers/api/enterprise_shopfront_serializer.rb +++ b/app/serializers/api/enterprise_shopfront_serializer.rb @@ -62,8 +62,10 @@ module Api end def taxons + taxons = active ? enterprise.current_distributed_taxons : enterprise.distributed_taxons + ActiveModel::ArraySerializer.new( - enterprise.distributed_taxons, each_serializer: Api::TaxonSerializer + taxons, each_serializer: Api::TaxonSerializer ) end diff --git a/spec/models/enterprise_spec.rb b/spec/models/enterprise_spec.rb index c648031e88..cdee0610ab 100644 --- a/spec/models/enterprise_spec.rb +++ b/spec/models/enterprise_spec.rb @@ -482,14 +482,26 @@ describe Enterprise do let(:supplier) { create(:supplier_enterprise) } let(:taxon1) { create(:taxon) } let(:taxon2) { create(:taxon) } + let(:taxon3) { create(:taxon) } let(:product1) { create(:simple_product, primary_taxon: taxon1, taxons: [taxon1]) } let(:product2) { create(:simple_product, primary_taxon: taxon1, taxons: [taxon1, taxon2]) } + let(:product3) { create(:simple_product, primary_taxon: taxon3) } + let(:oc) { create(:order_cycle, distributors: [distributor]) } + let(:ex) { create(:exchange, order_cycle: oc, incoming: false, sender: supplier, receiver: distributor) } it "gets all taxons of all distributed products" do allow(Spree::Product).to receive(:in_distributor).and_return [product1, product2] expect(distributor.distributed_taxons).to match_array [taxon1, taxon2] end + it "gets all taxons of all distributed products in open order cycles" do + Spree::Product.stub(:in_distributor).and_return [product1, product2, product3] + ex.variants << product1.variants.first + ex.variants << product3.variants.first + + distributor.current_distributed_taxons.should match_array [taxon1, taxon3] + end + it "gets all taxons of all supplied products" do allow(Spree::Product).to receive(:in_supplier).and_return [product1, product2] expect(supplier.supplied_taxons).to match_array [taxon1, taxon2]