diff --git a/engines/dfc_provider/app/controllers/dfc_provider/api/products_controller.rb b/engines/dfc_provider/app/controllers/dfc_provider/api/products_controller.rb index c35da75286..4cd256ef9f 100644 --- a/engines/dfc_provider/app/controllers/dfc_provider/api/products_controller.rb +++ b/engines/dfc_provider/app/controllers/dfc_provider/api/products_controller.rb @@ -14,15 +14,7 @@ module DfcProvider respond_to :json def index - products = @enterprise. - supplied_products. - includes(variants: :product) - - serialized_data = ::DfcProvider::ProductSerializer. - new(products, base_url). - serialized_data - - render json: serialized_data + render json: serialized_data_for(@user) end private @@ -54,10 +46,6 @@ module DfcProvider head :unauthorized end - def base_url - "#{root_url}api/dfc_provider" - end - def access_token request.headers['Authorization'].to_s.split(' ').last end @@ -65,6 +53,16 @@ module DfcProvider def authorization_control DfcProvider::AuthorizationControl.new(access_token) end + + def serialized_data_for(user) + { + "@context" => + { + "dfc" => "http://datafoodconsortium.org/ontologies/DFC_FullModel.owl#", + "@base" => "#{root_url}api/dfc_provider" + } + }.merge(DfcProvider::PersonSerializer.new(user).serialized_data) + end end end end diff --git a/engines/dfc_provider/app/serializers/dfc_provider/catalog_item_serializer.rb b/engines/dfc_provider/app/serializers/dfc_provider/catalog_item_serializer.rb new file mode 100644 index 0000000000..47be0dd5b4 --- /dev/null +++ b/engines/dfc_provider/app/serializers/dfc_provider/catalog_item_serializer.rb @@ -0,0 +1,33 @@ +# frozen_string_literal: true + +# Serializer used to render a DFC CatalogItemt from an OFN Product +# into JSON-LD format based on DFC ontology +module DfcProvider + class CatalogItemSerializer + def initialize(variant) + @variant = variant + end + + def serialized_data + { + "@id" => "/catalog_items/#{@variant.id}", + "@type" => "dfc:CatalogItem", + "dfc:references" => { + "@type" => "@id", + "@id" => "/supplied_products/#{@variant.product_id}" + }, + "dfc:sku" => @variant.sku, + "dfc:stockLimitation" => nil, + "dfc:offeredThrough" => serialized_offers + } + end + + private + + def serialized_offers + [ + OfferSerializer.new(@variant).serialized_data + ] + end + end +end diff --git a/engines/dfc_provider/app/serializers/dfc_provider/enterprise_serializer.rb b/engines/dfc_provider/app/serializers/dfc_provider/enterprise_serializer.rb new file mode 100644 index 0000000000..93b5a7f732 --- /dev/null +++ b/engines/dfc_provider/app/serializers/dfc_provider/enterprise_serializer.rb @@ -0,0 +1,44 @@ +# frozen_string_literal: true + +# Serializer used to render a DFC Enterprise from an OFN Enterprise +# into JSON-LD format based on DFC ontology +module DfcProvider + class EnterpriseSerializer + def initialize(enterprise) + @enterprise = enterprise + end + + def serialized_data + { + "@id" => "/entrepriseId", + "@type" => "dfc:Entreprise", + "dfc:VATnumber" => nil, + "dfc:defines" => [], + "dfc:supplies" => serialized_supplied_products, + "dfc:manages" => serialized_catalog_items + } + end + + private + + def products + @products ||= @enterprise. + supplied_products. + includes(variants: :product) + end + + def serialized_supplied_products + products.map do |product| + SuppliedProductSerializer.new(product).serialized_data + end + end + + def serialized_catalog_items + @products.map do |product| + product.variants.map do |variant| + CatalogItemSerializer.new(variant).serialized_data + end + end.flatten + end + end +end diff --git a/engines/dfc_provider/app/serializers/dfc_provider/offer_serializer.rb b/engines/dfc_provider/app/serializers/dfc_provider/offer_serializer.rb new file mode 100644 index 0000000000..008df0ec19 --- /dev/null +++ b/engines/dfc_provider/app/serializers/dfc_provider/offer_serializer.rb @@ -0,0 +1,24 @@ +# frozen_string_literal: true + +# Serializer used to render the DFC Offer from an OFN Product +# into JSON-LD format based on DFC ontology +module DfcProvider + class OfferSerializer + def initialize(variant) + @variant = variant + end + + def serialized_data + { + "@id" => "offers/#{@variant.id}", + "@type" => "dfc:Offer", + "dfc:offeresTo" => { + "@type" => "@id", + "@id" => "/customerCategoryId1" + }, + "dfc:price" => @variant.price, + "dfc:stockLimitation" => @variant.total_on_hand, + } + end + end +end diff --git a/engines/dfc_provider/app/serializers/dfc_provider/person_serializer.rb b/engines/dfc_provider/app/serializers/dfc_provider/person_serializer.rb new file mode 100644 index 0000000000..9f4f8f3737 --- /dev/null +++ b/engines/dfc_provider/app/serializers/dfc_provider/person_serializer.rb @@ -0,0 +1,36 @@ +# frozen_string_literal: true + +# Serializer used to render the DFC Person from an OFN User +# into JSON-LD format based on DFC ontology +module DfcProvider + class PersonSerializer + def initialize(user) + @user = user + end + + def serialized_data + { + "@id" => "/personId/#{@user.id}", + "@type" => "dfc:Person", + "dfc:familyName" => @user.login, + "dfc:firtsName" => @user.email, + "dfc:hasAdress" => { + "@type" => "dfc:Address", + "dfc:city" => "", + "dfc:country" => "", + "dfc:postcode" =>"", + "dfc:street" => "" + }, + "dfc:affiliates" => affiliates_serialized_data + } + end + + private + + def affiliates_serialized_data + @user.enterprises.each do |enterprise| + EnterpriseSerializer.new(enterprise).serialized_data + end + end + end +end diff --git a/engines/dfc_provider/app/serializers/dfc_provider/product_serializer.rb b/engines/dfc_provider/app/serializers/dfc_provider/product_serializer.rb deleted file mode 100644 index d88a415268..0000000000 --- a/engines/dfc_provider/app/serializers/dfc_provider/product_serializer.rb +++ /dev/null @@ -1,97 +0,0 @@ -# frozen_string_literal: true - -# Serializer used to render the products passed -# into JSON-LD format based on DFC ontology -module DfcProvider - class ProductSerializer - def initialize(products, base_url) - @products = products - @base_url = base_url - end - - def serialized_data - { - "@context" => - { - "dfc" => "http://datafoodconsortium.org/ontologies/DFC_FullModel.owl#", - "@base" => @base_url - }, - "@id" => "/enterprise/default/products", - "@id" => "/personId", - "@type"=> "dfc:Person", - "dfc:familyName" => "Doe", - "dfc:firtsName" => "Jhon", - "dfc:hasAdress" => { - "@type" => "dfc:Address", - "dfc:city" =>"", - "dfc:country" =>"", - "dfc:postcode" => "", - "dfc:street" => "" - }, - "dfc:affiliates" => [ - { - "@id" => "/entrepriseId", - "@type" => "dfc:Entreprise", - "dfc:VATnumber" => "", - "dfc:defines" => [], - "dfc:supplies" => supplied_products, - "dfc:manages" => managed_products - } - ] - } - end - - private - - def supplied_products - @products.map do |product| - variant = product.variants.first - { - "@id" => "/products/#{product.id}", - "dfc:hasUnit":{ - "@id" => "/unit/#{variant.unit_description.presence || 'piece'}", - "rdfs:label" => "#{variant.unit_description.presence || 'piece'}" - }, - "dfc:quantity" => "99.99", - "dfc:description" => product.name, - "dfc:totalTheoriticalStock" => nil, - "dfc:brand" => '', - "dfc:claim" => '', - "dfc:image" => product.images.first.try(:attahcement, :url), - "lifeTime" => "supply lifeTime", - "dfc:physicalCharacterisctics" => "supply physical characterisctics", - "dfc:quantity" => "supply quantity" - } - end - end - - def managed_products - @products.map do |product| - product.variants.map do |variant| - { - "@id" => "/catalogItemId1", - "@type" => "dfc:CatalogItem", - "dfc:references" => { - "@type" => "@id", - "@id" => "/suppliedProduct/item3" - }, - "dfc:sku" => product.sku, - "dfc:stockLimitation" => nil, - "dfc:offeredThrough" => [ - { - "@id" => "offerId1", - "@type" => "dfc:Offer", - "dfc:offeresTo" => { - "@type" => "@id", - "@id" => "/customerCategoryId1" - }, - "dfc:price" => variant.price, - "dfc:stockLimitation" => 0, - } - ] - } - end - end.flatten - end - end -end diff --git a/engines/dfc_provider/app/serializers/dfc_provider/supplied_product_serializer.rb b/engines/dfc_provider/app/serializers/dfc_provider/supplied_product_serializer.rb new file mode 100644 index 0000000000..1d051874c1 --- /dev/null +++ b/engines/dfc_provider/app/serializers/dfc_provider/supplied_product_serializer.rb @@ -0,0 +1,29 @@ +# frozen_string_literal: true + +# Serializer used to render a DFC SuppliedProduct from an OFN Product +# into JSON-LD format based on DFC ontology +module DfcProvider + class SuppliedProductSerializer + def initialize(product) + @product = product + end + + def serialized_data + { + "@id" => "/products/#{@product.id}", + "dfc:hasUnit" => { + "@id" => "/unit/#{@product.unit_description.presence || 'piece'}", + "rdfs:label" => "#{@product.unit_description.presence || 'piece'}" + }, + "dfc:quantity" => @product.total_on_hand, + "dfc:description" => @product.name, + "dfc:totalTheoriticalStock" => nil, + "dfc:brand" => nil, + "dfc:claim" => nil, + "dfc:image" => @product.images.first.try(:attachment, :url), + "lifeTime" => nil, + "dfc:physicalCharacterisctics" => nil + } + end + end +end