From dfe83e5c5163991d3fa3afba95974c4c629caae1 Mon Sep 17 00:00:00 2001 From: Pau Perez Date: Fri, 28 May 2021 13:16:26 +0200 Subject: [PATCH] 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. --- app/serializers/api/product_serializer.rb | 5 -- app/services/products_renderer.rb | 5 -- .../api/product_serializer_spec.rb | 71 ++----------------- 3 files changed, 6 insertions(+), 75 deletions(-) diff --git a/app/serializers/api/product_serializer.rb b/app/serializers/api/product_serializer.rb index f3ae97b56e..994df3bd52 100644 --- a/app/serializers/api/product_serializer.rb +++ b/app/serializers/api/product_serializer.rb @@ -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 diff --git a/app/services/products_renderer.rb b/app/services/products_renderer.rb index 83bbae5790..de81f8d9b2 100644 --- a/app/services/products_renderer.rb +++ b/app/services/products_renderer.rb @@ -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] ||= [] diff --git a/spec/serializers/api/product_serializer_spec.rb b/spec/serializers/api/product_serializer_spec.rb index 55790009b8..417b35fa27 100644 --- a/spec/serializers/api/product_serializer_spec.rb +++ b/spec/serializers/api/product_serializer_spec.rb @@ -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