1
0
Fork 0
mirror of https://github.com/plankanban/planka.git synced 2025-07-19 05:09:43 +02:00
planka/server/api/controllers/board-memberships/create.js

106 lines
2.3 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
*/
const { idInput } = require('../../../utils/inputs');
const Errors = {
NOT_ENOUGH_RIGHTS: {
notEnoughRights: 'Not enough rights',
},
BOARD_NOT_FOUND: {
boardNotFound: 'Board not found',
},
USER_NOT_FOUND: {
userNotFound: 'User not found',
},
USER_ALREADY_BOARD_MEMBER: {
userAlreadyBoardMember: 'User already board member',
},
};
module.exports = {
inputs: {
boardId: {
...idInput,
required: true,
},
userId: {
...idInput,
required: true,
},
role: {
type: 'string',
isIn: Object.values(BoardMembership.Roles),
required: true,
},
canComment: {
type: 'boolean',
2024-02-08 16:42:10 +01:00
allowNull: true,
},
},
exits: {
notEnoughRights: {
responseType: 'forbidden',
},
boardNotFound: {
responseType: 'notFound',
},
userNotFound: {
responseType: 'notFound',
},
userAlreadyBoardMember: {
responseType: 'conflict',
},
},
async fn(inputs) {
const { currentUser } = this.req;
const { board, project } = await sails.helpers.boards
.getPathToProjectById(inputs.boardId)
.intercept('pathNotFound', () => Errors.BOARD_NOT_FOUND);
const isProjectManager = await sails.helpers.users.isProjectManager(currentUser.id, project.id);
if (!isProjectManager) {
throw Errors.BOARD_NOT_FOUND; // Forbidden
}
if (!sails.helpers.users.isAdminOrProjectOwner(currentUser)) {
if (inputs.userId !== currentUser.id) {
throw Errors.NOT_ENOUGH_RIGHTS;
}
}
const user = await User.qm.getOneById(inputs.userId, {
withDeactivated: false,
});
if (!user) {
throw Errors.USER_NOT_FOUND;
}
const values = _.pick(inputs, ['role', 'canComment']);
2022-12-26 21:10:50 +01:00
const boardMembership = await sails.helpers.boardMemberships.createOne
.with({
project,
2022-12-26 21:10:50 +01:00
values: {
...values,
board,
user,
},
actorUser: currentUser,
2022-12-26 21:10:50 +01:00
request: this.req,
})
.intercept('userAlreadyBoardMember', () => Errors.USER_ALREADY_BOARD_MEMBER);
return {
item: boardMembership,
};
},
};