Skip to content

Commit

Permalink
[E] Improve collaborator endpoints
Browse files Browse the repository at this point in the history
  • Loading branch information
1aurend committed Jan 31, 2025
1 parent cbc2bab commit 273825a
Show file tree
Hide file tree
Showing 7 changed files with 70 additions and 23 deletions.
35 changes: 30 additions & 5 deletions api/app/controllers/api/v1/collaborators_controller.rb
Original file line number Diff line number Diff line change
Expand Up @@ -2,12 +2,14 @@ module API
module V1
class CollaboratorsController < ApplicationController

authority_actions create_from_roles: "create"

resourceful! Collaborator, authorize_options: { except: [:roles] } do
Collaborator.filtered(collaborator_filter_params)
end

def create
@collaborators = collaborators_from_list(collaborator_list_params)
def create_from_roles
@collaborators = collaborators_from_roles(collaborators_from_roles_params)
render_multiple_resources(@collaborators, serializer: ::V1::CollaboratorSerializer)
end

Expand All @@ -19,9 +21,32 @@ def roles

private

def collaborators_from_list(params)
params[:data].each_with_object([]) do |c, arr|
@collaborator = ::Updaters::Collaborator.new({ data: c }).update(Collaborator.new)
def collaborators_from_roles_params
params.require(:data)
attributes = { roles: [] }
relationships = {
maker: { data: [:type, :id] },
collaboratable: { data: [:type, :id] }
}
param_config = { data: { attributes: attributes, relationships: relationships } }

params.permit(param_config)
end

def collaborators_from_roles(params)
raw_params = params.to_unsafe_h

roles = raw_params.dig(:data, :attributes, :roles)
relationships = raw_params.dig(:data, :relationships)

roles.each_with_object([]) do |role, arr|
collaborator_params = {
data: {
attributes: { role: role },
relationships: relationships
}
}
@collaborator = ::Updaters::Collaborator.new(collaborator_params).update(Collaborator.new)
@collaborator.save
arr << @collaborator
end
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -24,6 +24,8 @@ def show
end

def destroy
return render_no_maker_error unless maker_filter_present?

@collaborators = load_collaborators
@collaborators.destroy_all
end
Expand All @@ -33,6 +35,18 @@ def destroy
def load_collaborator
@project.collaborators.find(params[:id])
end

def maker_filter_present?
maker_id = collaborator_filter_params[:maker]

maker_id.present?
end

def render_no_maker_error
message = "Maker filter is required"

render json: { errors: [message] }, status: :unprocessable_entity
end
end
end
end
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -25,6 +25,8 @@ def show
end

def destroy
return render_no_maker_error unless maker_filter_present?

@collaborators = load_collaborators
@collaborators.destroy_all
end
Expand All @@ -38,6 +40,18 @@ def set_text
def load_collaborator
@text.collaborators.find(params[:id])
end

def maker_filter_present?
maker_id = collaborator_filter_params[:maker]

maker_id.present?
end

def render_no_maker_error
message = "Maker filter is required"

render json: { errors: [message] }, status: :unprocessable_entity
end
end
end
end
Expand Down
10 changes: 1 addition & 9 deletions api/app/controllers/concerns/validation.rb
Original file line number Diff line number Diff line change
Expand Up @@ -149,20 +149,12 @@ def twitter_query_params

def collaborator_params
params.require(:data)
attributes = [:role, :position]
attributes = [:role]
relationships = [:maker, :collaboratable]
param_config = structure_params(attributes: attributes, relationships: relationships)
params.permit(param_config)
end

def collaborator_list_params
params.require(:data)
attributes = [:role, :position]
relationships = [:maker, :collaboratable]
param_config = structure_params(attributes: attributes, relationships: relationships)
params.permit([param_config])
end

def resource_collection_params
params.require(:data)
attributes = [:title, :description, attachment(:thumbnail), :remove_thumbnail,
Expand Down
3 changes: 2 additions & 1 deletion api/config/routes.rb
Original file line number Diff line number Diff line change
Expand Up @@ -112,9 +112,10 @@
resources :journal_volumes, except: [:create, :index]
resources :ingestion_sources, except: [:create, :index]
resources :annotations, only: [:index, :show]
resources :collaborators, only: [:create] do
resources :collaborators do
collection do
get :roles
post :create_from_roles
end
end

Expand Down
6 changes: 3 additions & 3 deletions client/src/api/resources/collaborators.js
Original file line number Diff line number Diff line change
Expand Up @@ -18,17 +18,17 @@ export default {

create(collaborators) {
return {
endpoint: `/api/v1/collaborators`,
endpoint: `/api/v1/collaborators/create_from_roles`,
method: "POST",
options: {
body: JSON.stringify({ type: "collaborators", data: collaborators })
body: JSON.stringify({ data: collaborators })
}
};
},

show(entityType, entityId, id) {
return {
endpoint: `/api/v1/${entityType}/${entityId}/relationships/permissions/${id}`,
endpoint: `/api/v1/${entityType}/${entityId}/relationships/collaborators/${id}`,
method: "GET"
};
},
Expand Down
11 changes: 6 additions & 5 deletions client/src/backend/components/collaborator/AddForm/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -42,16 +42,17 @@ export default function AddCollaboratorForm({

const onSubmit = async e => {
e.preventDefault();
const contributions = roles.map(role => ({

const data = {
attributes: {
role
roles
},
relationships: {
maker: { data: { id: makerId.id } },
maker: { data: { id: makerId.id, type: "maker" } },
collaboratable: { data: { id: entityId, type: entityType } }
}
}));
const { errors } = await createCollaborator(contributions);
};
const { errors } = await createCollaborator(data);

if (!errors) {
if (refresh) refresh();
Expand Down

0 comments on commit 273825a

Please sign in to comment.