List only current taxons for active enterprises

This commit is contained in:
Matt-Yorkley
2019-05-10 01:10:19 +01:00
parent ed97400a23
commit 698d3672a6
3 changed files with 31 additions and 1 deletions

View File

@@ -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?

View File

@@ -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

View File

@@ -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]