2019-08-31 04:07:25 +05:00
|
|
|
module.exports = {
|
|
|
|
inputs: {
|
|
|
|
list: {
|
|
|
|
type: 'ref',
|
2019-11-05 18:01:42 +05:00
|
|
|
required: true,
|
2019-08-31 04:07:25 +05:00
|
|
|
},
|
|
|
|
values: {
|
|
|
|
type: 'json',
|
2020-02-03 18:42:31 +05:00
|
|
|
custom: value => _.isPlainObject(value) && _.isFinite(value.position),
|
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
|
|
|
},
|
|
|
|
|
2019-11-05 18:01:42 +05:00
|
|
|
async fn(inputs, exits) {
|
2019-08-31 04:07:25 +05:00
|
|
|
const cards = await sails.helpers.getCardsForList(inputs.list.id);
|
|
|
|
|
|
|
|
const { position, repositions } = sails.helpers.insertToPositionables(
|
|
|
|
inputs.values.position,
|
2019-11-05 18:01:42 +05:00
|
|
|
cards,
|
2019-08-31 04:07:25 +05:00
|
|
|
);
|
|
|
|
|
2019-11-05 18:01:42 +05:00
|
|
|
repositions.forEach(async ({ id, position: nextPosition }) => {
|
2019-08-31 04:07:25 +05:00
|
|
|
await Card.update({
|
|
|
|
id,
|
2019-11-05 18:01:42 +05:00
|
|
|
listId: inputs.list.id,
|
2019-08-31 04:07:25 +05:00
|
|
|
}).set({
|
2019-11-05 18:01:42 +05:00
|
|
|
position: nextPosition,
|
2019-08-31 04:07:25 +05:00
|
|
|
});
|
|
|
|
|
|
|
|
sails.sockets.broadcast(`board:${list.boardId}`, 'cardUpdate', {
|
|
|
|
item: {
|
|
|
|
id,
|
2019-11-05 18:01:42 +05:00
|
|
|
position: nextPosition,
|
|
|
|
},
|
2019-08-31 04:07:25 +05:00
|
|
|
});
|
|
|
|
});
|
|
|
|
|
|
|
|
const card = await Card.create({
|
|
|
|
...inputs.values,
|
|
|
|
position,
|
|
|
|
listId: inputs.list.id,
|
2019-11-05 18:01:42 +05:00
|
|
|
boardId: inputs.list.boardId,
|
2019-08-31 04:07:25 +05:00
|
|
|
}).fetch();
|
|
|
|
|
|
|
|
sails.sockets.broadcast(
|
|
|
|
`board:${card.boardId}`,
|
|
|
|
'cardCreate',
|
|
|
|
{
|
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
|
|
|
inputs.request,
|
2019-08-31 04:07:25 +05:00
|
|
|
);
|
|
|
|
|
|
|
|
const values = {
|
|
|
|
type: 'createCard',
|
|
|
|
data: {
|
2019-11-05 18:01:42 +05:00
|
|
|
list: _.pick(inputs.list, ['id', 'name']),
|
|
|
|
},
|
2019-08-31 04:07:25 +05:00
|
|
|
};
|
|
|
|
|
|
|
|
await sails.helpers.createAction(card, inputs.user, values);
|
|
|
|
|
|
|
|
return exits.success(card);
|
2019-11-05 18:01:42 +05:00
|
|
|
},
|
2019-08-31 04:07:25 +05:00
|
|
|
};
|