Files
planka/server/api/controllers/card-memberships/create.js

154 lines
3.6 KiB
JavaScript
Raw Normal View History

/*!
* Copyright (c) 2024 PLANKA Software GmbH
* Licensed under the Fair Use License: https://github.com/plankanban/planka/blob/master/LICENSE.md
*/
2025-09-08 16:20:27 +02:00
/**
* @swagger
* /api/cards/{cardId}/memberships:
* post:
* summary: Add user to card
* description: Adds a user to a card. Requires board editor permissions.
* tags:
* - Card Memberships
* parameters:
* - name: cardId
* in: path
* required: true
* description: ID of the card to add the user to
* schema:
* type: string
* example: 1357158568008091264
* requestBody:
* required: true
* content:
* application/json:
* schema:
* type: object
* required:
* - userId
* properties:
* userId:
* type: string
* description: ID of the card to add the user to
* example: 1357158568008091265
* responses:
* 200:
* description: User added to card successfully
* content:
* application/json:
* schema:
* type: object
* required:
* - item
* properties:
* item:
* $ref: '#/components/schemas/CardMembership'
* 400:
* $ref: '#/components/responses/ValidationError'
* 401:
* $ref: '#/components/responses/Unauthorized'
* 403:
* $ref: '#/components/responses/Forbidden'
* 404:
* $ref: '#/components/responses/NotFound'
* 409:
* $ref: '#/components/responses/Conflict'
*/
const { idInput } = require('../../../utils/inputs');
2019-08-31 04:07:25 +05:00
const Errors = {
NOT_ENOUGH_RIGHTS: {
notEnoughRights: 'Not enough rights',
},
2019-08-31 04:07:25 +05:00
CARD_NOT_FOUND: {
2020-04-03 00:35:25 +05:00
cardNotFound: 'Card not found',
2019-08-31 04:07:25 +05:00
},
USER_NOT_FOUND: {
2020-04-03 00:35:25 +05:00
userNotFound: 'User not found',
2019-08-31 04:07:25 +05:00
},
2020-04-03 00:35:25 +05:00
USER_ALREADY_CARD_MEMBER: {
userAlreadyCardMember: 'User already card member',
},
2019-08-31 04:07:25 +05:00
};
module.exports = {
inputs: {
cardId: {
...idInput,
required: true,
2019-08-31 04:07:25 +05:00
},
userId: {
...idInput,
required: true,
},
2019-08-31 04:07:25 +05:00
},
exits: {
notEnoughRights: {
responseType: 'forbidden',
},
2020-04-03 00:35:25 +05:00
cardNotFound: {
responseType: 'notFound',
2019-08-31 04:07:25 +05:00
},
2020-04-03 00:35:25 +05:00
userNotFound: {
responseType: 'notFound',
},
userAlreadyCardMember: {
responseType: 'conflict',
},
2019-08-31 04:07:25 +05:00
},
async fn(inputs) {
2019-08-31 04:07:25 +05:00
const { currentUser } = this.req;
const { card, list, board, project } = await sails.helpers.cards
.getPathToProjectById(inputs.cardId)
2020-04-03 00:35:25 +05:00
.intercept('pathNotFound', () => Errors.CARD_NOT_FOUND);
2019-08-31 04:07:25 +05:00
const boardMembership = await BoardMembership.qm.getOneByBoardIdAndUserId(
board.id,
currentUser.id,
);
2019-08-31 04:07:25 +05:00
if (!boardMembership) {
2019-08-31 04:07:25 +05:00
throw Errors.CARD_NOT_FOUND; // Forbidden
}
if (boardMembership.role !== BoardMembership.Roles.EDITOR) {
throw Errors.NOT_ENOUGH_RIGHTS;
}
const user = await User.qm.getOneById(inputs.userId);
if (!user) {
throw Errors.USER_NOT_FOUND;
}
const isBoardMember = await sails.helpers.users.isBoardMember(user.id, board.id);
2019-08-31 04:07:25 +05:00
if (!isBoardMember) {
throw Errors.USER_NOT_FOUND; // Forbidden
2019-08-31 04:07:25 +05:00
}
2022-12-26 21:10:50 +01:00
const cardMembership = await sails.helpers.cardMemberships.createOne
.with({
project,
board,
list,
2022-12-26 21:10:50 +01:00
values: {
card,
user,
2022-12-26 21:10:50 +01:00
},
actorUser: currentUser,
2022-12-26 21:10:50 +01:00
request: this.req,
})
2020-04-03 00:35:25 +05:00
.intercept('userAlreadyCardMember', () => Errors.USER_ALREADY_CARD_MEMBER);
2019-08-31 04:07:25 +05:00
return {
item: cardMembership,
};
},
2019-08-31 04:07:25 +05:00
};