mirror of
https://github.com/openfoodfoundation/openfoodnetwork
synced 2026-02-26 01:33:22 +00:00
Add endpoint to add enterprise to a group
plus documentaion
This commit is contained in:
@@ -0,0 +1,33 @@
|
||||
# frozen_string_literal: true
|
||||
|
||||
module DfcProvider
|
||||
module EnterpriseGroups
|
||||
class AffiliatedByController < DfcProvider::ApplicationController
|
||||
def create
|
||||
group = EnterpriseGroup.find(params[:enterprise_group_id])
|
||||
|
||||
authorize! :update, group
|
||||
|
||||
enterprise_uri = RDF::URI.new(params[:@id])
|
||||
|
||||
return head :bad_request unless enterprise_uri.valid?
|
||||
|
||||
enterprise_id = ofn_id_from_uri(enterprise_uri)
|
||||
enterprise = Enterprise.find(enterprise_id)
|
||||
|
||||
group.enterprises << enterprise
|
||||
|
||||
return head :unprocessable_entity unless group.save
|
||||
|
||||
head :created
|
||||
end
|
||||
|
||||
private
|
||||
|
||||
def ofn_id_from_uri(uri)
|
||||
# enterprise uri follow this format http://test.host/api/dfc/enterprises/{ofn_enterprise_id}
|
||||
uri.path.split("/").last
|
||||
end
|
||||
end
|
||||
end
|
||||
end
|
||||
@@ -7,6 +7,8 @@ DfcProvider::Engine.routes.draw do
|
||||
resources :supplied_products, only: [:create, :show, :update]
|
||||
resources :social_medias, only: [:show]
|
||||
end
|
||||
resources :enterprise_groups, only: [:index, :show]
|
||||
resources :enterprise_groups, only: [:index, :show] do
|
||||
resources :affiliated_by, only: [:create], module: 'enterprise_groups'
|
||||
end
|
||||
resources :persons, only: [:show]
|
||||
end
|
||||
|
||||
@@ -0,0 +1,73 @@
|
||||
# frozen_string_literal: true
|
||||
|
||||
require_relative "../../swagger_helper"
|
||||
|
||||
describe "EnterpriseGroups::AffiliatedBy", type: :request, swagger_doc: "dfc.yaml",
|
||||
rswag_autodoc: true do
|
||||
let(:user) { create(:oidc_user, id: 12_345) }
|
||||
let(:group) {
|
||||
create(
|
||||
:enterprise_group,
|
||||
id: 60_000, owner: user, name: "Sustainable Farmers", address:,
|
||||
enterprises: [enterprise],
|
||||
)
|
||||
}
|
||||
let(:address) { create(:address, id: 40_000, address1: "8 Acres Drive") }
|
||||
let(:enterprise) { create(:enterprise, id: 10_000) }
|
||||
let!(:enterprise2) { create(:enterprise, id: 10_001) }
|
||||
|
||||
before { login_as user }
|
||||
|
||||
path "/api/dfc/enterprise_groups/{enterprise_group_id}/affiliated_by" do
|
||||
post "Add enterprise to group" do
|
||||
consumes "application/json"
|
||||
|
||||
parameter name: :enterprise_group_id, in: :path, type: :string
|
||||
parameter name: :enterprise_id, in: :body, schema: {
|
||||
example: {
|
||||
'@id': "http://test.host/api/dfc/enterprises/10001"
|
||||
}
|
||||
}
|
||||
|
||||
let(:enterprise_group_id) { group.id }
|
||||
let(:enterprise_id) do |example|
|
||||
example.metadata[:operation][:parameters].second[:schema][:example]
|
||||
end
|
||||
|
||||
response "201", "created" do
|
||||
run_test! do
|
||||
expect(group.enterprises.reload).to include(enterprise2)
|
||||
end
|
||||
end
|
||||
|
||||
response "400", "bad request" do
|
||||
describe "with missing request body" do
|
||||
around do |example|
|
||||
# Rswag expects all required parameters to be supplied with `let`
|
||||
# but we want to send a request without the request body parameter.
|
||||
parameters = example.metadata[:operation][:parameters]
|
||||
example.metadata[:operation][:parameters] = [parameters.first]
|
||||
example.run
|
||||
example.metadata[:operation][:parameters] = parameters
|
||||
end
|
||||
|
||||
run_test!
|
||||
end
|
||||
|
||||
describe "with non valid enterprise uri" do
|
||||
let(:enterprise_id) { { '@id': "http://test.host/%api/dfc/enterprises/10001" } }
|
||||
|
||||
run_test!
|
||||
end
|
||||
end
|
||||
|
||||
response "401", "unauthorized" do
|
||||
let(:non_group_owner) { create(:oidc_user, id: 12_346) }
|
||||
|
||||
before { login_as non_group_owner }
|
||||
|
||||
run_test!
|
||||
end
|
||||
end
|
||||
end
|
||||
end
|
||||
@@ -236,6 +236,30 @@ paths:
|
||||
"@type": "@id"
|
||||
dfc-b:stockLimitation: '3'
|
||||
dfc-b:sku: new-sku
|
||||
"/api/dfc/enterprise_groups/{enterprise_group_id}/affiliated_by":
|
||||
post:
|
||||
summary: Add enterprise to group
|
||||
parameters:
|
||||
- name: enterprise_group_id
|
||||
in: path
|
||||
required: true
|
||||
schema:
|
||||
type: string
|
||||
tags:
|
||||
- EnterpriseGroups::AffiliatedBy
|
||||
responses:
|
||||
'201':
|
||||
description: created
|
||||
'400':
|
||||
description: bad request
|
||||
'401':
|
||||
description: unauthorized
|
||||
requestBody:
|
||||
content:
|
||||
application/json:
|
||||
schema:
|
||||
example:
|
||||
"@id": http://test.host/api/dfc/enterprises/10001
|
||||
"/api/dfc/enterprise_groups":
|
||||
get:
|
||||
summary: List groups
|
||||
|
||||
Reference in New Issue
Block a user