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

140 lines
3.2 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/{userId}:
* delete:
* summary: Remove user from card
* description: Removes a user from a card. Requires board editor permissions.
* tags:
* - Card Memberships
* parameters:
* - name: cardId
* in: path
* required: true
* description: ID of the card to remove the user from
* schema:
* type: string
* example: 1357158568008091264
* - name: userId
* in: path
* required: true
* description: ID of the user to remove from the card
* schema:
* type: string
* example: 1357158568008091265
* responses:
* 200:
* description: User removed from 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'
*/
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
},
2020-04-03 00:35:25 +05:00
USER_NOT_CARD_MEMBER: {
userNotCardMember: 'User not 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',
},
userNotCardMember: {
responseType: 'notFound',
},
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;
}
let cardMembership = await CardMembership.qm.getOneByCardIdAndUserId(
inputs.cardId,
inputs.userId,
);
2019-08-31 04:07:25 +05:00
if (!cardMembership) {
2020-04-03 00:35:25 +05:00
throw Errors.USER_NOT_CARD_MEMBER;
2019-08-31 04:07:25 +05:00
}
const user = await User.qm.getOneById(cardMembership.userId);
2022-12-26 21:10:50 +01:00
cardMembership = await sails.helpers.cardMemberships.deleteOne.with({
user,
project,
2022-12-26 21:10:50 +01:00
board,
list,
card,
2022-12-26 21:10:50 +01:00
record: cardMembership,
actorUser: currentUser,
2022-12-26 21:10:50 +01:00
request: this.req,
});
2019-08-31 04:07:25 +05:00
if (!cardMembership) {
2020-04-03 00:35:25 +05:00
throw Errors.USER_NOT_CARD_MEMBER;
2019-08-31 04:07:25 +05:00
}
return {
item: cardMembership,
};
},
2019-08-31 04:07:25 +05:00
};