2019-08-31 04:07:25 +05:00
|
|
|
const moment = require('moment');
|
|
|
|
|
|
|
|
const Errors = {
|
|
|
|
LIST_NOT_FOUND: {
|
2019-11-05 18:01:42 +05:00
|
|
|
notFound: 'List is not found',
|
|
|
|
},
|
2019-08-31 04:07:25 +05:00
|
|
|
};
|
|
|
|
|
|
|
|
module.exports = {
|
|
|
|
inputs: {
|
|
|
|
listId: {
|
2019-10-10 02:51:54 +05:00
|
|
|
type: 'string',
|
|
|
|
regex: /^[0-9]+$/,
|
2019-11-05 18:01:42 +05:00
|
|
|
required: true,
|
2019-08-31 04:07:25 +05:00
|
|
|
},
|
|
|
|
position: {
|
|
|
|
type: 'number',
|
2019-11-05 18:01:42 +05:00
|
|
|
required: true,
|
2019-08-31 04:07:25 +05:00
|
|
|
},
|
|
|
|
name: {
|
|
|
|
type: 'string',
|
2019-11-05 18:01:42 +05:00
|
|
|
required: true,
|
2019-08-31 04:07:25 +05:00
|
|
|
},
|
|
|
|
description: {
|
|
|
|
type: 'string',
|
|
|
|
isNotEmptyString: true,
|
2019-11-05 18:01:42 +05:00
|
|
|
allowNull: true,
|
2019-08-31 04:07:25 +05:00
|
|
|
},
|
2019-10-05 06:12:36 +05:00
|
|
|
dueDate: {
|
2019-08-31 04:07:25 +05:00
|
|
|
type: 'string',
|
2019-11-05 18:01:42 +05:00
|
|
|
custom: (value) => moment(value, moment.ISO_8601, true).isValid(),
|
2019-08-31 04:07:25 +05:00
|
|
|
},
|
|
|
|
timer: {
|
|
|
|
type: 'json',
|
2019-11-05 18:01:42 +05:00
|
|
|
custom: (value) => _.isPlainObject(value)
|
|
|
|
&& _.size(value) === 2
|
|
|
|
&& (_.isNull(value.startedAt) || moment(value.startedAt, moment.ISO_8601, true).isValid())
|
|
|
|
&& _.isFinite(value.total),
|
|
|
|
},
|
2019-08-31 04:07:25 +05:00
|
|
|
},
|
|
|
|
|
|
|
|
exits: {
|
|
|
|
notFound: {
|
2019-11-05 18:01:42 +05:00
|
|
|
responseType: 'notFound',
|
|
|
|
},
|
2019-08-31 04:07:25 +05:00
|
|
|
},
|
|
|
|
|
2019-11-05 18:01:42 +05:00
|
|
|
async fn(inputs, exits) {
|
2019-08-31 04:07:25 +05:00
|
|
|
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,
|
2019-11-05 18:01:42 +05:00
|
|
|
currentUser.id,
|
2019-08-31 04:07:25 +05:00
|
|
|
);
|
|
|
|
|
|
|
|
if (!isUserMemberForProject) {
|
|
|
|
throw Errors.LIST_NOT_FOUND; // Forbidden
|
|
|
|
}
|
|
|
|
|
2019-11-05 18:01:42 +05:00
|
|
|
const values = _.pick(inputs, ['position', 'name', 'description', 'dueDate', 'timer']);
|
2019-08-31 04:07:25 +05:00
|
|
|
|
2019-11-05 18:01:42 +05:00
|
|
|
const card = await sails.helpers.createCard(list, values, currentUser, this.req);
|
2019-08-31 04:07:25 +05:00
|
|
|
|
|
|
|
return exits.success({
|
2019-11-05 18:01:42 +05:00
|
|
|
item: card,
|
2019-08-31 04:07:25 +05:00
|
|
|
});
|
2019-11-05 18:01:42 +05:00
|
|
|
},
|
2019-08-31 04:07:25 +05:00
|
|
|
};
|