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
This commit is contained in:
Gaetan Craig-Riou
2024-05-28 12:07:21 +10:00
parent 3704b18952
commit a52401107a
3 changed files with 26 additions and 18 deletions

View File

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

View File

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

View File

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