Do not return master variant in api/ocs/products

`product.master` seems to always be `null` on
/api/v0/order_cycles/:id/products JSON response. That's because the
first of the underlying AR relation,
`order_cycle.variants_distributed_by(distributor)`, does not include
master variants. They are not in OC exchanges.
This commit is contained in:
Pau Perez
2021-05-28 13:16:26 +02:00
parent e7166ba5d4
commit dfe83e5c51
3 changed files with 6 additions and 75 deletions

View File

@@ -6,7 +6,6 @@ class Api::ProductSerializer < ActiveModel::Serializer
attributes :properties_with_values
has_many :variants, serializer: Api::VariantSerializer
has_one :master, serializer: Api::VariantSerializer
has_one :primary_taxon, serializer: Api::TaxonSerializer
has_many :taxons, serializer: Api::IdSerializer
@@ -32,10 +31,6 @@ class Api::ProductSerializer < ActiveModel::Serializer
options[:variants][object.id] || []
end
def master
options[:master_variants][object.id].andand.first
end
private
def sanitizer

View File

@@ -19,7 +19,6 @@ class ProductsRenderer
current_order_cycle: order_cycle,
current_distributor: distributor,
variants: variants_for_shop_by_id,
master_variants: master_variants_for_shop_by_id,
enterprise_fee_calculator: enterprise_fee_calculator).to_json
end
@@ -84,10 +83,6 @@ class ProductsRenderer
index_by_product_id variants_for_shop.reject(&:is_master)
end
def master_variants_for_shop_by_id
index_by_product_id variants_for_shop.select(&:is_master)
end
def index_by_product_id(variants)
variants.each_with_object({}) do |v, vs|
vs[v.product_id] ||= []

View File

@@ -13,25 +13,24 @@ describe Api::ProductSerializer do
let!(:property) { create(:property) }
let!(:product) { create(:product, primary_taxon: taxon, properties: [property], price: 20.00) }
let(:variant1) { create(:variant, product: product) }
let(:variant2) { create(:variant, product: product) }
let(:master_variant) { product.master }
let(:serializer) {
described_class.new(product,
variants: [variant1, variant2],
master_variants: [master_variant],
variants: [variant1],
current_distributor: distributor,
current_order_cycle: order_cycle)
}
before do
add_variant_to_order_cycle(exchange, master_variant)
add_variant_to_order_cycle(exchange, variant1)
add_variant_to_order_cycle(exchange, variant2)
end
it "serializes various attributes" do
expect(serializer.serializable_hash.keys).to eq serialized_attributes
expect(serializer.serializable_hash.keys).to eq [
:id, :name, :permalink, :meta_keywords, :group_buy, :notes, :description, :description_html,
:properties_with_values, :variants, :primary_taxon, :taxons, :images, :supplier
]
end
it "serializes product properties" do
@@ -43,62 +42,4 @@ describe Api::ProductSerializer do
it "serializes taxons" do
expect(serializer.serializable_hash[:taxons]).to eq [id: taxon.id]
end
describe "serializing price" do
context "without enterprise fees" do
it "returns the regular product price" do
product_price = serializer.serializable_hash[:price]
expect(product_price).to eq product.master.price
end
end
context "with enterprise fees" do
let(:simple_fee) { create(:enterprise_fee, enterprise: distributor, amount: 1000) }
before { exchange.enterprise_fees << simple_fee }
it "includes enterprise fees in the product price" do
product_price = serializer.serializable_hash[:price]
expect(product_price).to eq product.master.price + 1000
end
end
context "when a specific calculator is used in fees" do
let(:enterprise_fee_calculator) {
OpenFoodNetwork::EnterpriseFeeCalculator.new distributor, order_cycle
}
let(:serializer) {
described_class.new(product,
variants: [variant1, variant2],
master_variants: [master_variant],
current_distributor: distributor,
current_order_cycle: order_cycle,
enterprise_fee_calculator: enterprise_fee_calculator)
}
let!(:fee_with_calculator) {
create(:enterprise_fee,
amount: 20,
fee_type: "admin",
calculator: ::Calculator::FlatPercentPerItem.
new(preferred_flat_percent: 20))
}
before { exchange.enterprise_fees << fee_with_calculator }
it "applies the correct calculated fee in the product price" do
product_price = serializer.serializable_hash[:price]
expect(product_price).to eq product.master.price + (product.master.price / 100 * 20)
end
end
end
private
def serialized_attributes
[
:id, :name, :permalink, :meta_keywords, :group_buy, :notes, :description, :description_html,
:properties_with_values, :price, :variants, :master, :primary_taxon, :taxons, :images,
:supplier
]
end
end