List catalog items with DFC Connector

This commit is contained in:
Maikel Linke
2023-03-10 15:14:02 +11:00
parent 7f8ce08d4e
commit c60d622818
3 changed files with 44 additions and 5 deletions

View File

@@ -7,11 +7,14 @@ module DfcProvider
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
person = PersonBuilder.person(current_user)
render json: DfcLoader.connector.export(
person,
*person.affiliatedOrganizations,
*person.affiliatedOrganizations.flat_map(&:catalogItems),
*person.affiliatedOrganizations.flat_map(&:catalogItems).map(&:product),
*person.affiliatedOrganizations.flat_map(&:catalogItems).flat_map(&:offers),
)
end
def show

View File

@@ -0,0 +1,18 @@
# frozen_string_literal: true
class EnterpriseBuilder < DfcBuilder
def self.enterprise(enterprise)
DataFoodConsortium::Connector::Enterprise.new(
enterprise.name
).tap do |e|
e.semanticId = urls.enterprise_url(enterprise.id)
e.catalogItems = catalog_items(enterprise)
end
end
def self.catalog_items(enterprise)
VariantFetcher.new(enterprise).scope.to_a.map do |variant|
catalog_item(variant)
end
end
end

View File

@@ -0,0 +1,18 @@
# frozen_string_literal: true
class PersonBuilder < DfcBuilder
def self.person(user)
DataFoodConsortium::Connector::Person.new(
urls.person_url(user.id),
firstName: user.bill_address&.firstname,
lastName: user.bill_address&.lastname,
affiliatedOrganizations: enterprises(user),
)
end
def self.enterprises(user)
user.enterprises.map do |enterprise|
EnterpriseBuilder.enterprise(enterprise)
end
end
end