Add dfc anonymous orders endpoint

This commit is contained in:
Ana Nunes da Silva
2024-06-14 15:38:58 +01:00
committed by David Cook
parent 8ab1cbe600
commit e8663c3fea
3 changed files with 91 additions and 0 deletions

View File

@@ -0,0 +1,66 @@
# frozen_string_literal: true
module DfcProvider
class AnonymousOrdersController < DfcProvider::ApplicationController
def index
orders = anonymous_orders.map do |order|
OrderBuilder.build_anonymous(order)
end
render json: DfcIo.export(*orders)
end
private
def anonymous_orders
Spree::LineItem
.joins(Arel.sql(joins_conditions))
.select(Arel.sql(select_fields))
.where(where_conditions)
.group(Arel.sql(group_fields))
.order(Arel.sql(order_fields))
end
def joins_conditions
[
"JOIN spree_orders ON spree_orders.id = spree_line_items.order_id",
"JOIN spree_variants ON spree_variants.id = spree_line_items.variant_id",
"JOIN spree_products ON spree_products.id = spree_variants.product_id",
"JOIN enterprises AS enterprise1 ON spree_orders.distributor_id = enterprise1.id",
"JOIN enterprises AS enterprise2 ON spree_products.supplier_id = enterprise2.id",
"JOIN spree_addresses AS distributors ON enterprise1.address_id = distributors.id",
"JOIN spree_addresses AS producers ON enterprise2.address_id = producers.id"
].join(' ')
end
def select_fields
"spree_products.name AS product_name,
spree_variants.display_name AS unit_name,
spree_products.variant_unit AS unit_type,
spree_variants.unit_value AS units,
spree_variants.unit_presentation,
SUM(spree_line_items.quantity) AS quantity_sold,
spree_line_items.price,
distributors.zipcode AS distributor_postcode,
producers.zipcode AS producer_postcode"
end
def where_conditions
{ spree_orders: { state: 'complete' } }
end
def group_fields
'spree_products.name,
spree_variants.display_name,
spree_variants.unit_value,
spree_variants.unit_presentation,
spree_products.variant_unit,
spree_line_items.price,
distributors.zipcode,
producers.zipcode'
end
def order_fields
'spree_products.name'
end
end
end

View File

@@ -0,0 +1,24 @@
# frozen_string_literal: true
class OrderBuilder < DfcBuilder
def self.build_anonymous(order)
id = urls.anonymous_orders_url
DataFoodConsortium::Connector::Order.new(
id,
orderStatus: 'complete'
).tap do |e|
add_ofn_property(e, "ofn:producer_postcode", order.producer_postcode)
add_ofn_property(e, "ofn:distributor_postcode", order.distributor_postcode)
add_ofn_property(e, "ofn:variant_unit_name", order.unit_name)
add_ofn_property(e, "ofn:variant_unit_type", order.unit_type)
add_ofn_property(e, "ofn:variant_units", order.units)
add_ofn_property(e, "ofn:price", order.price.to_f)
add_ofn_property(e, "ofn:quantity_sold", order.quantity_sold)
end
end
def self.add_ofn_property(dfc_enterprise, property_name, value)
dfc_enterprise.registerSemanticProperty(property_name) { value }
end
end

View File

@@ -12,4 +12,5 @@ DfcProvider::Engine.routes.draw do
resources :affiliated_by, only: [:create, :destroy], module: 'enterprise_groups'
end
resources :persons, only: [:show]
resources :anonymous_orders, only: [:index]
end