Skip to content

Commit

Permalink
Merge pull request #7800 from pacodelaluna/improve-dfc-logic
Browse files Browse the repository at this point in the history
Improve DFC Provider logic to use existing business logic
  • Loading branch information
andrewpbrett authored Jul 16, 2021
2 parents fd8766e + 39bb25e commit c6743d8
Show file tree
Hide file tree
Showing 20 changed files with 179 additions and 90 deletions.
2 changes: 2 additions & 0 deletions engines/dfc_provider/README.md
Original file line number Diff line number Diff line change
Expand Up @@ -8,3 +8,5 @@ Basically, it allows an OFN user linked to an enterprise:
* to be authenticated thanks to an Access Token from DFC Authorization server (using an OIDC implementation)

The API endpoint for the catalog is `/api/dfc_provider/enterprise/prodcuts.json` and you need to pass the token inside an authentication header (`Authentication: Bearer 123mytoken456`).

This feature is still under active development.
Original file line number Diff line number Diff line change
Expand Up @@ -7,8 +7,7 @@ class BaseController < ActionController::Base
rescue_from ActiveRecord::RecordNotFound, with: :not_found

before_action :check_authorization,
:check_user,
:check_enterprise
:check_user

respond_to :json

Expand All @@ -27,12 +26,15 @@ def check_user
end

def check_enterprise
current_enterprise
return if current_enterprise.present?

not_found
end

def current_enterprise
@current_enterprise ||=
if params[enterprise_id_param_name] == 'default'
case params[enterprise_id_param_name]
when 'default'
current_user.enterprises.first!
else
current_user.enterprises.find(params[enterprise_id_param_name])
Expand Down
Original file line number Diff line number Diff line change
@@ -1,10 +1,17 @@
# frozen_string_literal: true

# Controller used to provide the API products for the DFC application
# CatalogItems are items that are being sold by the entreprise.
module DfcProvider
module Api
class CatalogItemsController < DfcProvider::Api::BaseController
before_action :check_enterprise

def index
# CatalogItem is nested into an entreprise which is also nested into
# an user on the DFC specifications, as defined here:
# https://datafoodconsortium.gitbook.io/dfc-standard-documentation
# /technical-specification/api-examples
render json: current_user, serializer: DfcProvider::PersonSerializer
end

Expand All @@ -16,10 +23,7 @@ def show

def variant
@variant ||=
Spree::Variant.
joins(product: :supplier).
where('enterprises.id' => current_enterprise.id).
find(params[:id])
DfcProvider::VariantFetcher.new(current_enterprise).scope.find(params[:id])
end
end
end
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,8 @@
module DfcProvider
module Api
class EnterprisesController < DfcProvider::Api::BaseController
before_action :check_enterprise

def show
render json: current_enterprise, serializer: DfcProvider::EnterpriseSerializer
end
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -4,8 +4,6 @@
module DfcProvider
module Api
class PersonsController < DfcProvider::Api::BaseController
skip_before_action :check_enterprise

before_action :check_user_accessibility

def show
Expand Down
Original file line number Diff line number Diff line change
@@ -1,9 +1,12 @@
# frozen_string_literal: true

# Controller used to provide the SuppliedProducts API for the DFC application
# SuppliedProducts are products that are managed by an enterprise.
module DfcProvider
module Api
class SuppliedProductsController < DfcProvider::Api::BaseController
before_action :check_enterprise

def show
render json: variant, serializer: DfcProvider::SuppliedProductSerializer
end
Expand All @@ -12,10 +15,7 @@ def show

def variant
@variant ||=
Spree::Variant.
joins(product: :supplier).
where('enterprises.id' => current_enterprise.id).
find(params[:id])
DfcProvider::VariantFetcher.new(current_enterprise).scope.find(params[:id])
end
end
end
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@
# Serializer used to render the DFC Address from an OFN User
# into JSON-LD format based on DFC ontology
module DfcProvider
class AddressSerializer < ActiveModel::Serializer
class AddressSerializer < BaseSerializer
attribute :type, key: '@type'
attribute :city, key: 'dfc:city'
attribute :country, key: 'dfc:country'
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,17 @@
# frozen_string_literal: true

# Serializer used to render the DFC Address from an OFN User
# into JSON-LD format based on DFC ontology
module DfcProvider
class BaseSerializer < ActiveModel::Serializer
private

def host
Rails.application.routes.default_url_options[:host]
end

def dfc_provider_routes
DfcProvider::Engine.routes.url_helpers
end
end
end
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@
# Serializer used to render a DFC CatalogItem from an OFN Product
# into JSON-LD format based on DFC ontology
module DfcProvider
class CatalogItemSerializer < ActiveModel::Serializer
class CatalogItemSerializer < BaseSerializer
attribute :id, key: '@id'
attribute :type, key: '@type'
attribute :references, key: 'dfc:references'
Expand All @@ -17,7 +17,7 @@ def id
dfc_provider_routes.api_dfc_provider_enterprise_catalog_item_url(
enterprise_id: object.product.supplier_id,
id: object.id,
host: root_url
host: host
)
end

Expand All @@ -28,7 +28,7 @@ def type
def references
{
'@type' => '@id',
'@id' => "/supplied_products/#{object.product_id}"
'@id' => reference_id
}
end

Expand All @@ -43,13 +43,9 @@ def offered_through
def reference_id
dfc_provider_routes.api_dfc_provider_enterprise_supplied_product_url(
enterprise_id: object.product.supplier_id,
id: object.product_id,
host: root_url
id: object.id,
host: host
)
end

def dfc_provider_routes
DfcProvider::Engine.routes.url_helpers
end
end
end
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@
# Serializer used to render a DFC Enterprise from an OFN Enterprise
# into JSON-LD format based on DFC ontology
module DfcProvider
class EnterpriseSerializer < ActiveModel::Serializer
class EnterpriseSerializer < BaseSerializer
attribute :id, key: '@id'
attribute :type, key: '@type'
attribute :vat_number, key: 'dfc:VATnumber'
Expand All @@ -18,7 +18,7 @@ class EnterpriseSerializer < ActiveModel::Serializer
def id
dfc_provider_routes.api_dfc_provider_enterprise_url(
id: object.id,
host: root_url
host: host
)
end

Expand All @@ -33,21 +33,11 @@ def defines
end

def supplies
Spree::Variant.
joins(product: :supplier).
where('enterprises.id' => object.id)
DfcProvider::VariantFetcher.new(object).scope
end

def manages
Spree::Variant.
joins(product: :supplier).
where('enterprises.id' => object.id)
end

private

def dfc_provider_routes
DfcProvider::Engine.routes.url_helpers
DfcProvider::VariantFetcher.new(object).scope
end
end
end
Original file line number Diff line number Diff line change
Expand Up @@ -3,10 +3,10 @@
# Serializer used to render the DFC Offer from an OFN Product
# into JSON-LD format based on DFC ontology
module DfcProvider
class OfferSerializer < ActiveModel::Serializer
class OfferSerializer < BaseSerializer
attribute :id, key: '@id'
attribute :type, key: '@type'
attribute :offeres_to, key: 'dfc:offeres_to'
attribute :offers_to, key: 'dfc:offers_to'
attribute :price, key: 'dfc:price'
attribute :stock_limitation, key: 'dfc:stockLimitation'

Expand All @@ -18,7 +18,7 @@ def type
'dfc:Offer'
end

def offeres_to
def offers_to
{
'@type' => '@id',
'@id' => nil
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@
# Serializer used to render the DFC Person from an OFN User
# into JSON-LD format based on DFC ontology
module DfcProvider
class PersonSerializer < ActiveModel::Serializer
class PersonSerializer < BaseSerializer
attribute :context, key: '@context'
attribute :id, key: '@id'
attribute :type, key: '@type'
Expand All @@ -28,7 +28,7 @@ def context
def id
dfc_provider_routes.api_dfc_provider_person_url(
id: object.id,
host: root_url
host: host
)
end

Expand All @@ -45,11 +45,5 @@ def address; end
def affiliates
object.enterprises
end

private

def dfc_provider_routes
DfcProvider::Engine.routes.url_helpers
end
end
end
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@
# Serializer used to render a DFC SuppliedProduct from an OFN Variant
# into JSON-LD format based on DFC ontology
module DfcProvider
class SuppliedProductSerializer < ActiveModel::Serializer
class SuppliedProductSerializer < BaseSerializer
attribute :id, key: '@id'
attribute :type, key: '@type'
attribute :unit, key: 'dfc:hasUnit'
Expand All @@ -20,7 +20,7 @@ def id
dfc_provider_routes.api_dfc_provider_enterprise_supplied_product_url(
enterprise_id: object.product.supplier_id,
id: object.id,
host: root_url
host: host
)
end

Expand Down Expand Up @@ -64,9 +64,5 @@ def physical_characteristics
def unit_name
object.unit_description.presence || 'piece'
end

def dfc_provider_routes
DfcProvider::Engine.routes.url_helpers
end
end
end
18 changes: 18 additions & 0 deletions engines/dfc_provider/app/services/dfc_provider/variant_fetcher.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,18 @@
# frozen_string_literal: true

# Service used to fetch variants related to an entreprise.
# It improves maintenance as it is the central point requesting
# Spree::Varaint inside the DfcProvider engine.
module DfcProvider
class VariantFetcher
def initialize(enterprise)
@enterprise = enterprise
end

def scope
Spree::Variant.
joins(product: :supplier).
where('enterprises.id' => @enterprise.id)
end
end
end
Loading

0 comments on commit c6743d8

Please sign in to comment.