2019-08-31 04:07:25 +05:00
|
|
|
module.exports = {
|
|
|
|
inputs: {
|
2020-08-04 01:32:46 +05:00
|
|
|
board: {
|
2019-08-31 04:07:25 +05:00
|
|
|
type: 'ref',
|
2019-11-05 18:01:42 +05:00
|
|
|
required: true,
|
2019-08-31 04:07:25 +05:00
|
|
|
},
|
2020-08-04 01:32:46 +05:00
|
|
|
list: {
|
|
|
|
type: 'ref',
|
|
|
|
},
|
2019-08-31 04:07:25 +05:00
|
|
|
values: {
|
|
|
|
type: 'json',
|
2020-08-04 01:32:46 +05:00
|
|
|
custom: (value) => {
|
|
|
|
if (!_.isPlainObject(value)) {
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
|
|
|
|
if (!_.isUndefined(value.position) && !_.isFinite(value.position)) {
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
|
|
|
|
return true;
|
|
|
|
},
|
2019-11-05 18:01:42 +05:00
|
|
|
required: true,
|
2019-08-31 04:07:25 +05:00
|
|
|
},
|
|
|
|
user: {
|
|
|
|
type: 'ref',
|
2019-11-05 18:01:42 +05:00
|
|
|
required: true,
|
2019-08-31 04:07:25 +05:00
|
|
|
},
|
|
|
|
request: {
|
2019-11-05 18:01:42 +05:00
|
|
|
type: 'ref',
|
|
|
|
},
|
2019-08-31 04:07:25 +05:00
|
|
|
},
|
|
|
|
|
2020-08-04 01:32:46 +05:00
|
|
|
exits: {
|
|
|
|
listMustBePresent: {},
|
|
|
|
listMustBelongToBoard: {},
|
|
|
|
positionMustBeInValues: {},
|
|
|
|
},
|
|
|
|
|
2019-11-05 18:01:42 +05:00
|
|
|
async fn(inputs, exits) {
|
2020-08-04 01:32:46 +05:00
|
|
|
const { values } = inputs;
|
2019-08-31 04:07:25 +05:00
|
|
|
|
2020-08-04 01:32:46 +05:00
|
|
|
values.boardId = inputs.board.id;
|
2019-08-31 04:07:25 +05:00
|
|
|
|
2020-08-04 01:32:46 +05:00
|
|
|
if (inputs.board.type === 'kanban') {
|
|
|
|
if (!inputs.list) {
|
|
|
|
throw 'listMustBePresent';
|
|
|
|
}
|
|
|
|
|
|
|
|
if (inputs.list.boardId !== inputs.board.id) {
|
|
|
|
throw 'listMustBelongToBoard';
|
|
|
|
}
|
2019-08-31 04:07:25 +05:00
|
|
|
|
2020-08-04 01:32:46 +05:00
|
|
|
values.listId = inputs.list.id;
|
|
|
|
|
|
|
|
if (_.isUndefined(values.position)) {
|
|
|
|
throw 'positionMustBeInValues';
|
|
|
|
}
|
|
|
|
|
|
|
|
const cards = await sails.helpers.getCardsForList(inputs.list.id);
|
|
|
|
|
|
|
|
const { position, repositions } = sails.helpers.insertToPositionables(
|
|
|
|
inputs.values.position,
|
|
|
|
cards,
|
|
|
|
);
|
|
|
|
|
|
|
|
repositions.forEach(async ({ id, position: nextPosition }) => {
|
|
|
|
await Card.update({
|
2019-08-31 04:07:25 +05:00
|
|
|
id,
|
2020-08-04 01:32:46 +05:00
|
|
|
listId: inputs.list.id,
|
|
|
|
}).set({
|
2019-11-05 18:01:42 +05:00
|
|
|
position: nextPosition,
|
2020-08-04 01:32:46 +05:00
|
|
|
});
|
|
|
|
|
|
|
|
sails.sockets.broadcast(`board:${inputs.board.id}`, 'cardUpdate', {
|
|
|
|
item: {
|
|
|
|
id,
|
|
|
|
position: nextPosition,
|
|
|
|
},
|
|
|
|
});
|
2019-08-31 04:07:25 +05:00
|
|
|
});
|
|
|
|
|
2020-08-04 01:32:46 +05:00
|
|
|
values.position = position;
|
|
|
|
} else if (inputs.board.type === 'collection') {
|
|
|
|
delete values.position;
|
|
|
|
}
|
|
|
|
|
|
|
|
const card = await Card.create(values).fetch();
|
2019-08-31 04:07:25 +05:00
|
|
|
|
2020-04-10 00:11:34 +05:00
|
|
|
if (inputs.user.subscribeToOwnCards) {
|
|
|
|
await CardSubscription.create({
|
|
|
|
cardId: card.id,
|
|
|
|
userId: inputs.user.id,
|
|
|
|
}).tolerate('E_UNIQUE');
|
|
|
|
|
|
|
|
card.isSubscribed = true;
|
|
|
|
} else {
|
|
|
|
card.isSubscribed = false;
|
|
|
|
}
|
|
|
|
|
2020-08-04 01:32:46 +05:00
|
|
|
// FIXME: broadcast subscription separately
|
2019-08-31 04:07:25 +05:00
|
|
|
sails.sockets.broadcast(
|
|
|
|
`board:${card.boardId}`,
|
|
|
|
'cardCreate',
|
|
|
|
{
|
2019-11-05 18:01:42 +05:00
|
|
|
item: card,
|
2020-05-09 05:30:52 +05:00
|
|
|
included: {
|
|
|
|
cardMemberships: [],
|
|
|
|
cardLabels: [],
|
|
|
|
tasks: [],
|
|
|
|
attachments: [],
|
|
|
|
},
|
2019-08-31 04:07:25 +05:00
|
|
|
},
|
2019-11-05 18:01:42 +05:00
|
|
|
inputs.request,
|
2019-08-31 04:07:25 +05:00
|
|
|
);
|
|
|
|
|
2020-08-04 01:32:46 +05:00
|
|
|
await sails.helpers.createAction(card, inputs.user, {
|
2019-08-31 04:07:25 +05:00
|
|
|
type: 'createCard',
|
|
|
|
data: {
|
2019-11-05 18:01:42 +05:00
|
|
|
list: _.pick(inputs.list, ['id', 'name']),
|
|
|
|
},
|
2020-08-04 01:32:46 +05:00
|
|
|
});
|
2019-08-31 04:07:25 +05:00
|
|
|
|
|
|
|
return exits.success(card);
|
2019-11-05 18:01:42 +05:00
|
|
|
},
|
2019-08-31 04:07:25 +05:00
|
|
|
};
|