mirror of
https://github.com/openfoodfoundation/openfoodnetwork
synced 2026-02-27 01:43:22 +00:00
List catalog items with DFC Connector
This commit is contained in:
@@ -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
|
||||
|
||||
18
engines/dfc_provider/app/services/enterprise_builder.rb
Normal file
18
engines/dfc_provider/app/services/enterprise_builder.rb
Normal 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
|
||||
18
engines/dfc_provider/app/services/person_builder.rb
Normal file
18
engines/dfc_provider/app/services/person_builder.rb
Normal 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
|
||||
Reference in New Issue
Block a user