/*! * 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, }, customFields: { type: 'ref', }, }, 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', ]); if (inputs.customFields) { values.customFields = inputs.customFields; } 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, }; }, };