Skip to content

Commit

Permalink
Merge pull request #9943 from mkllnk/dfc-provider-test
Browse files Browse the repository at this point in the history
DFC 1.6 prototype
  • Loading branch information
RachL authored Dec 9, 2022
2 parents ffba628 + 7b96571 commit 5ed6e55
Show file tree
Hide file tree
Showing 31 changed files with 458 additions and 273 deletions.
2 changes: 1 addition & 1 deletion .rubocop_todo.yml
Original file line number Diff line number Diff line change
Expand Up @@ -681,7 +681,7 @@ Rails/ActiveRecordOverride:
# This cop supports unsafe autocorrection (--autocorrect-all).
Rails/ApplicationController:
Exclude:
- 'engines/dfc_provider/app/controllers/dfc_provider/api/base_controller.rb'
- 'engines/dfc_provider/app/controllers/dfc_provider/base_controller.rb'

# Offense count: 6
# This cop supports unsafe autocorrection (--autocorrect-all).
Expand Down
5 changes: 0 additions & 5 deletions config/routes.rb
Original file line number Diff line number Diff line change
Expand Up @@ -117,11 +117,6 @@

get 'sitemap.xml', to: 'sitemap#index', defaults: { format: 'xml' }

constraints FeatureToggleConstraint.new(:dfc_provider) do
# Mount DFC API endpoints
mount DfcProvider::Engine, at: '/'
end

# Mount Spree's routes
mount Spree::Core::Engine, :at => '/'

Expand Down
6 changes: 6 additions & 0 deletions config/routes/api.rb
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,12 @@
end

namespace :api do

constraints FeatureToggleConstraint.new(:dfc_provider) do
# Mount DFC API endpoints
mount DfcProvider::Engine, at: '/dfc-v1.6/'
end

namespace :v0 do
resources :products do
collection do
Expand Down

This file was deleted.

This file was deleted.

This file was deleted.

This file was deleted.

This file was deleted.

Original file line number Diff line number Diff line change
@@ -0,0 +1,52 @@
# frozen_string_literal: true

# Controller used to provide the API products for the DFC application
module DfcProvider
class BaseController < ActionController::Base
protect_from_forgery with: :null_session

rescue_from ActiveRecord::RecordNotFound, with: :not_found

before_action :check_authorization

respond_to :json

private

def check_authorization
head :unauthorized if current_user.nil?
end

def check_enterprise
return if current_enterprise.present?

not_found
end

def current_enterprise
@current_enterprise ||=
case params[enterprise_id_param_name]
when 'default'
current_user.enterprises.first!
else
current_user.enterprises.find(params[enterprise_id_param_name])
end
end

def enterprise_id_param_name
:enterprise_id
end

def current_user
@current_user ||= authorization_control.user
end

def authorization_control
DfcProvider::AuthorizationControl.new(request)
end

def not_found
head :not_found
end
end
end
Original file line number Diff line number Diff line change
@@ -0,0 +1,28 @@
# 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
class CatalogItemsController < DfcProvider::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

def show
render json: variant, serializer: DfcProvider::CatalogItemSerializer
end

private

def variant
@variant ||=
DfcProvider::VariantFetcher.new(current_enterprise).scope.find(params[:id])
end
end
end
Original file line number Diff line number Diff line change
@@ -0,0 +1,18 @@
# frozen_string_literal: true

# Controller used to provide the CatalogItem API for the DFC application
module DfcProvider
class EnterprisesController < DfcProvider::BaseController
before_action :check_enterprise

def show
render json: current_enterprise, serializer: DfcProvider::EnterpriseSerializer
end

private

def enterprise_id_param_name
:id
end
end
end
Original file line number Diff line number Diff line change
@@ -0,0 +1,24 @@
# frozen_string_literal: true

# Controller used to provide the Persons API for the DFC application
module DfcProvider
class PersonsController < DfcProvider::BaseController
before_action :check_user_accessibility

def show
render json: user, serializer: DfcProvider::PersonSerializer
end

private

def user
@user ||= Spree::User.find(params[:id])
end

def check_user_accessibility
return if current_user == user

not_found
end
end
end
Original file line number Diff line number Diff line change
@@ -0,0 +1,27 @@
# 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
class SuppliedProductsController < DfcProvider::BaseController
before_action :check_enterprise

def show
render json: variant, serializer: DfcProvider::SuppliedProductSerializer
end

def update
dfc_request = JSON.parse(request.body.read)
return unless dfc_request.key?("dfc-b:description")

variant.product.update!(name: dfc_request["dfc-b:description"])
end

private

def variant
@variant ||=
DfcProvider::VariantFetcher.new(current_enterprise).scope.find(params[:id])
end
end
end
Original file line number Diff line number Diff line change
Expand Up @@ -6,23 +6,23 @@ module DfcProvider
class CatalogItemSerializer < BaseSerializer
attribute :id, key: '@id'
attribute :type, key: '@type'
attribute :references, key: 'dfc:references'
attribute :sku, key: 'dfc:sku'
attribute :stock_limitation, key: 'dfc:stockLimitation'
attribute :references, key: 'dfc-b:references'
attribute :sku, key: 'dfc-b:sku'
attribute :stock_limitation, key: 'dfc-b:stockLimitation'
has_many :offered_through,
serializer: DfcProvider::OfferSerializer,
key: 'dfc:offeredThrough'
key: 'dfc-b:offeredThrough'

def id
dfc_provider_routes.api_dfc_provider_enterprise_catalog_item_url(
dfc_provider_routes.enterprise_catalog_item_url(
enterprise_id: object.product.supplier_id,
id: object.id,
host: host
)
end

def type
'dfc:CatalogItem'
'dfc-b:CatalogItem'
end

def references
Expand All @@ -41,7 +41,7 @@ def offered_through
private

def reference_id
dfc_provider_routes.api_dfc_provider_enterprise_supplied_product_url(
dfc_provider_routes.enterprise_supplied_product_url(
enterprise_id: object.product.supplier_id,
id: object.id,
host: host
Expand Down
Loading

0 comments on commit 5ed6e55

Please sign in to comment.