1
0
Fork 0
mirror of https://github.com/plankanban/planka.git synced 2025-07-19 13:19:44 +02:00
planka/server/api/controllers/cards/create.js
Maksim Eltyshev 2ee1166747 feat: Version 2
Closes #627, closes #1047
2025-05-10 02:09:06 +02:00

116 lines
2.4 KiB
JavaScript
Executable file

/*!
* Copyright (c) 2024 PLANKA Software GmbH
* Licensed under the Fair Use License: https://github.com/plankanban/planka/blob/master/LICENSE.md
*/
const { isDueDate, isStopwatch } = require('../../../utils/validators');
const { idInput } = require('../../../utils/inputs');
const Errors = {
NOT_ENOUGH_RIGHTS: {
notEnoughRights: 'Not enough rights',
},
LIST_NOT_FOUND: {
listNotFound: 'List not found',
},
POSITION_MUST_BE_PRESENT: {
positionMustBePresent: 'Position must be present',
},
};
module.exports = {
inputs: {
listId: {
...idInput,
required: true,
},
type: {
type: 'string',
isIn: Object.values(Card.Types),
required: true,
},
position: {
type: 'number',
min: 0,
allowNull: true,
},
name: {
type: 'string',
maxLength: 1024,
required: true,
},
description: {
type: 'string',
isNotEmptyString: true,
maxLength: 1048576,
allowNull: true,
},
dueDate: {
type: 'string',
custom: isDueDate,
},
stopwatch: {
type: 'json',
custom: isStopwatch,
},
},
exits: {
notEnoughRights: {
responseType: 'forbidden',
},
listNotFound: {
responseType: 'notFound',
},
positionMustBePresent: {
responseType: 'unprocessableEntity',
},
},
async fn(inputs) {
const { currentUser } = this.req;
const { list, board, project } = await sails.helpers.lists
.getPathToProjectById(inputs.listId)
.intercept('pathNotFound', () => Errors.LIST_NOT_FOUND);
const boardMembership = await BoardMembership.qm.getOneByBoardIdAndUserId(
board.id,
currentUser.id,
);
if (!boardMembership) {
throw Errors.LIST_NOT_FOUND; // Forbidden
}
if (boardMembership.role !== BoardMembership.Roles.EDITOR) {
throw Errors.NOT_ENOUGH_RIGHTS;
}
const values = _.pick(inputs, [
'type',
'position',
'name',
'description',
'dueDate',
'stopwatch',
]);
const card = await sails.helpers.cards.createOne
.with({
project,
values: {
...values,
board,
list,
creatorUser: currentUser,
},
request: this.req,
})
.intercept('positionMustBeInValues', () => Errors.POSITION_MUST_BE_PRESENT);
return {
item: card,
};
},
};