1
0
Fork 0
mirror of https://github.com/plankanban/planka.git synced 2025-07-18 20:59:44 +02:00
planka/server/api/controllers/cards/create.js

73 lines
1.6 KiB
JavaScript
Executable file

const moment = require('moment');
const Errors = {
LIST_NOT_FOUND: {
notFound: 'List is not found',
},
};
module.exports = {
inputs: {
listId: {
type: 'string',
regex: /^[0-9]+$/,
required: true,
},
position: {
type: 'number',
required: true,
},
name: {
type: 'string',
required: true,
},
description: {
type: 'string',
isNotEmptyString: true,
allowNull: true,
},
dueDate: {
type: 'string',
custom: value => moment(value, moment.ISO_8601, true).isValid(),
},
timer: {
type: 'json',
custom: value =>
_.isPlainObject(value) &&
_.size(value) === 2 &&
(_.isNull(value.startedAt) || moment(value.startedAt, moment.ISO_8601, true).isValid()) &&
_.isFinite(value.total),
},
},
exits: {
notFound: {
responseType: 'notFound',
},
},
async fn(inputs, exits) {
const { currentUser } = this.req;
const { list, project } = await sails.helpers
.getListToProjectPath(inputs.listId)
.intercept('notFound', () => Errors.LIST_NOT_FOUND);
const isUserMemberForProject = await sails.helpers.isUserMemberForProject(
project.id,
currentUser.id,
);
if (!isUserMemberForProject) {
throw Errors.LIST_NOT_FOUND; // Forbidden
}
const values = _.pick(inputs, ['position', 'name', 'description', 'dueDate', 'timer']);
const card = await sails.helpers.createCard(list, values, currentUser, this.req);
return exits.success({
item: card,
});
},
};