Refactor to (person) affiliate sales data

This commit is contained in:
Ana Nunes da Silva
2024-07-05 15:57:29 +01:00
committed by David Cook
parent e8663c3fea
commit 4d8f24f6d6
5 changed files with 78 additions and 28 deletions

View File

@@ -1,8 +1,8 @@
# frozen_string_literal: true
module DfcProvider
class AnonymousOrdersController < DfcProvider::ApplicationController
def index
class AffiliateSalesDataController < DfcProvider::ApplicationController
def show
orders = anonymous_orders.map do |order|
OrderBuilder.build_anonymous(order)
end

View File

@@ -10,6 +10,11 @@ module DfcProvider
render json: DfcIo.export(person)
end
def affiliate_sales_data
sales_data = AffiliateSalesDataBuilder.build(user)
render json: DfcIo.export(sales_data)
end
private
def user

View File

@@ -0,0 +1,66 @@
# frozen_string_literal: true
class AffiliateSalesDataBuilder < DfcBuilder
def self.build(user)
DataFoodConsortium::Connector::Person.new(urls.affiliate_sales_data_person_url(user.id))
end
def self.user_enterprise
# DataFoodConsortium::Connector::Enterprise.new(
# )
end
def self.sales_data
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 self.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 self.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 self.where_conditions
{ spree_orders: { state: 'complete' } }
end
def self.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 self.order_fields
'spree_products.name'
end
private_class_method :sales_data
end

View File

@@ -1,24 +0,0 @@
# 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

@@ -11,6 +11,9 @@ DfcProvider::Engine.routes.draw do
resources :enterprise_groups, only: [:index, :show] do
resources :affiliated_by, only: [:create, :destroy], module: 'enterprise_groups'
end
resources :persons, only: [:show]
resources :anonymous_orders, only: [:index]
resources :persons, only: [:show] do
member do
get :affiliate_sales_data
end
end
end