List enterprise groups on DFC API

This commit is contained in:
Maikel Linke
2023-09-07 13:28:12 +10:00
parent a03fb01a8c
commit 95d3987752
4 changed files with 63 additions and 3 deletions

View File

@@ -3,6 +3,16 @@
# OFN EnterpriseGroup is represented as DFC Enterprise.
module DfcProvider
class EnterpriseGroupsController < DfcProvider::ApplicationController
def index
person = PersonBuilder.person(current_user)
groups = current_user.owned_groups
enterprises = groups.map do |group|
EnterpriseBuilder.enterprise_group(group)
end
person.affiliatedOrganizations = enterprises
render json: DfcIo.export(person, *enterprises)
end
def show
group = EnterpriseGroup.find(params[:id])
enterprise = EnterpriseBuilder.enterprise_group(group)

View File

@@ -6,6 +6,6 @@ DfcProvider::Engine.routes.draw do
resources :catalog_items, only: [:index, :show, :update]
resources :supplied_products, only: [:create, :show, :update]
end
resources :enterprise_groups, only: [:show]
resources :enterprise_groups, only: [:index, :show]
resources :persons, only: [:show]
end

View File

@@ -3,17 +3,43 @@
require_relative "../swagger_helper"
describe "EnterpriseGroups", type: :request, swagger_doc: "dfc.yaml", rswag_autodoc: true do
let(:user) { create(:oidc_user) }
let(:user) { create(:oidc_user, id: 12_345) }
let(:group) {
create(
:enterprise_group,
id: 60_000, name: "Sustainable Farmers", enterprises: [enterprise],
id: 60_000, owner: user, name: "Sustainable Farmers",
enterprises: [enterprise],
)
}
let(:enterprise) { create(:enterprise, id: 10_000) }
before { login_as user }
path "/api/dfc/enterprise_groups" do
get "List groups" do
produces "application/json"
response "200", "successful" do
let!(:groups) { [group] }
run_test! do
graph = json_response["@graph"]
expect(graph[0]["@type"]).to eq "dfc-b:Person"
expect(graph[0]).to include(
"dfc-b:affiliates" => "http://test.host/api/dfc/enterprise_groups/60000",
)
expect(graph[1]["@type"]).to eq "dfc-b:Enterprise"
expect(graph[1]).to include(
"dfc-b:hasName" => "Sustainable Farmers",
"dfc-b:affiliatedBy" => "http://test.host/api/dfc/enterprises/10000",
)
end
end
end
end
path "/api/dfc/enterprise_groups/{id}" do
get "Show groups" do
parameter name: :id, in: :path, type: :string

View File

@@ -234,6 +234,30 @@ paths:
"@type": "@id"
dfc-b:stockLimitation: '3'
dfc-b:sku: new-sku
"/api/dfc/enterprise_groups":
get:
summary: List groups
tags:
- EnterpriseGroups
responses:
'200':
description: successful
content:
application/json:
examples:
test_example:
value:
"@context": https://www.datafoodconsortium.org
"@graph":
- "@id": http://test.host/api/dfc/persons/12345
"@type": dfc-b:Person
dfc-b:affiliates: http://test.host/api/dfc/enterprise_groups/60000
- "@id": http://test.host/api/dfc/enterprise_groups/60000
"@type": dfc-b:Enterprise
dfc-b:hasName: Sustainable Farmers
dfc-b:hasDescription: this is a group
dfc-b:VATnumber: ''
dfc-b:affiliatedBy: http://test.host/api/dfc/enterprises/10000
"/api/dfc/enterprise_groups/{id}":
get:
summary: Show groups