From a52401107ab03b1daa0adc1bfa65fa614a51bcf5 Mon Sep 17 00:00:00 2001 From: Gaetan Craig-Riou Date: Tue, 28 May 2024 12:07:21 +1000 Subject: [PATCH] Order cycle API, separate properties and producer_properties Due to moving the supplier to the variant, we had to add manual search for producer properties instead of using ransack. So we need a way for the frontend to diferenciate between product properties and producer properties. This is the first step towards that --- .../api/v0/order_cycles_controller.rb | 16 ++++++++--- config/routes/api.rb | 1 + .../api/v0/order_cycles_controller_spec.rb | 27 +++++++++---------- 3 files changed, 26 insertions(+), 18 deletions(-) diff --git a/app/controllers/api/v0/order_cycles_controller.rb b/app/controllers/api/v0/order_cycles_controller.rb index 1f195b490a..ccf6a24dd6 100644 --- a/app/controllers/api/v0/order_cycles_controller.rb +++ b/app/controllers/api/v0/order_cycles_controller.rb @@ -7,9 +7,11 @@ module Api include ApiActionCaching skip_authorization_check - skip_before_action :authenticate_user, :ensure_api_key, only: [:taxons, :properties] + skip_before_action :authenticate_user, :ensure_api_key, only: [ + :taxons, :properties, :producer_properties + ] - caches_action :taxons, :properties, + caches_action :taxons, :properties, :producer_properties, expires_in: CacheService::FILTERS_EXPIRY, cache_path: proc { |controller| controller.request.url } @@ -41,7 +43,13 @@ module Api def properties render plain: ActiveModel::ArraySerializer.new( - product_properties | producer_properties, each_serializer: Api::PropertySerializer + product_properties, each_serializer: Api::PropertySerializer + ).to_json + end + + def producer_properties + render plain: ActiveModel::ArraySerializer.new( + load_producer_properties, each_serializer: Api::PropertySerializer ).to_json end @@ -58,7 +66,7 @@ module Api select('DISTINCT spree_properties.*') end - def producer_properties + def load_producer_properties producers = Enterprise. joins(:supplied_products). where(spree_products: { id: distributed_products }) diff --git a/config/routes/api.rb b/config/routes/api.rb index a661c6f359..74f2ad3652 100644 --- a/config/routes/api.rb +++ b/config/routes/api.rb @@ -60,6 +60,7 @@ Openfoodnetwork::Application.routes.draw do get :products, on: :member get :taxons, on: :member get :properties, on: :member + get :producer_properties, on: :member end resources :exchanges, only: [:show], to: 'exchange_products#index' do diff --git a/spec/controllers/api/v0/order_cycles_controller_spec.rb b/spec/controllers/api/v0/order_cycles_controller_spec.rb index e3cbe2503d..225b6f22be 100644 --- a/spec/controllers/api/v0/order_cycles_controller_spec.rb +++ b/spec/controllers/api/v0/order_cycles_controller_spec.rb @@ -239,25 +239,24 @@ module Api expect(json_response.length).to be 2 expect(properties).to include property1.presentation, property2.presentation end + end - context "with producer properties" do - let!(:property4) { create(:property) } - let!(:supplier) { create(:supplier_enterprise) } - let!(:producer_property) { - create(:producer_property, producer_id: supplier.id, property: property4) - } + describe "#producer_properties" do + let!(:property4) { create(:property) } + let!(:supplier) { create(:supplier_enterprise) } + let!(:producer_property) { + create(:producer_property, producer_id: supplier.id, property: property4) + } - before { product1.variants.first.update(supplier: ) } + before { product1.variants.first.update(supplier: ) } - it "loads producer properties for distributed products in the order cycle" do - api_get :properties, id: order_cycle.id, distributor: distributor.id + it "loads producer properties for distributed products in the order cycle" do + api_get :producer_properties, id: order_cycle.id, distributor: distributor.id - properties = json_response.pluck(:name) + properties = json_response.pluck(:name) - expect(json_response.length).to be 3 - expect(properties).to include property1.presentation, property2.presentation, - producer_property.property.presentation - end + expect(json_response.length).to be 1 + expect(properties).to include producer_property.property.presentation end end