mirror of
https://github.com/openfoodfoundation/openfoodnetwork
synced 2026-01-24 20:36:49 +00:00
74 lines
1.9 KiB
Ruby
74 lines
1.9 KiB
Ruby
module Api
|
|
class OrderCyclesController < BaseController
|
|
include EnterprisesHelper
|
|
respond_to :json
|
|
|
|
skip_authorization_check
|
|
|
|
def products
|
|
products = ProductsRenderer.new(
|
|
distributor,
|
|
order_cycle,
|
|
customer,
|
|
search_params
|
|
).products_json
|
|
|
|
render json: products
|
|
rescue ProductsRenderer::NoProducts
|
|
render status: :not_found, json: ''
|
|
end
|
|
|
|
def taxons
|
|
taxons = Spree::Taxon.
|
|
joins(:products).
|
|
where(spree_products: { id: distributed_products(distributor, order_cycle, customer) }).
|
|
select('DISTINCT spree_taxons.*')
|
|
|
|
render json: ActiveModel::ArraySerializer.new(taxons, each_serializer: Api::TaxonSerializer)
|
|
end
|
|
|
|
def properties
|
|
properties = Spree::Property.
|
|
joins(:products).
|
|
where(spree_products: { id: distributed_products(distributor, order_cycle, customer) }).
|
|
select('DISTINCT spree_properties.*')
|
|
|
|
render json: ActiveModel::ArraySerializer.new(
|
|
properties, each_serializer: Api::PropertySerializer
|
|
)
|
|
end
|
|
|
|
private
|
|
|
|
def search_params
|
|
permitted_search_params = params.slice :q, :page, :per_page
|
|
|
|
if permitted_search_params.key? :q
|
|
permitted_search_params[:q].slice!(*permitted_ransack_params)
|
|
end
|
|
|
|
permitted_search_params
|
|
end
|
|
|
|
def permitted_ransack_params
|
|
[:name_or_meta_keywords_or_supplier_name_cont, :properites_in_any, :primary_taxon_id_in_any]
|
|
end
|
|
|
|
def distributor
|
|
Enterprise.find_by_id(params[:distributor])
|
|
end
|
|
|
|
def order_cycle
|
|
OrderCycle.find_by_id(params[:id])
|
|
end
|
|
|
|
def customer
|
|
@current_api_user.andand.customer_of(distributor) || nil
|
|
end
|
|
|
|
def distributed_products(distributor, order_cycle, customer)
|
|
OrderCycleDistributedProducts.new(distributor, order_cycle, customer).products_relation
|
|
end
|
|
end
|
|
end
|