2019-08-31 04:07:25 +05:00
|
|
|
module.exports = {
|
|
|
|
inputs: {
|
|
|
|
record: {
|
|
|
|
type: 'ref',
|
2019-11-05 18:01:42 +05:00
|
|
|
required: true,
|
2019-08-31 04:07:25 +05:00
|
|
|
},
|
|
|
|
values: {
|
|
|
|
type: 'json',
|
2020-04-03 00:35:25 +05:00
|
|
|
custom: (value) =>
|
2020-02-03 18:42:31 +05:00
|
|
|
_.isPlainObject(value) && (_.isUndefined(value.position) || _.isFinite(value.position)),
|
2019-11-05 18:01:42 +05:00
|
|
|
required: true,
|
2019-08-31 04:07:25 +05:00
|
|
|
},
|
|
|
|
toList: {
|
2019-11-05 18:01:42 +05:00
|
|
|
type: 'ref',
|
2019-08-31 04:07:25 +05:00
|
|
|
},
|
|
|
|
list: {
|
|
|
|
type: 'ref',
|
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 { isSubscribed, ...values } = inputs.values;
|
|
|
|
|
|
|
|
let listId;
|
|
|
|
if (inputs.toList) {
|
|
|
|
listId = inputs.toList.id;
|
|
|
|
|
|
|
|
if (listId !== inputs.list.id) {
|
|
|
|
values.listId = listId;
|
|
|
|
} else {
|
2020-02-03 18:42:31 +05:00
|
|
|
delete inputs.toList; // eslint-disable-line no-param-reassign
|
2019-08-31 04:07:25 +05:00
|
|
|
}
|
|
|
|
} else {
|
|
|
|
listId = inputs.list.id;
|
|
|
|
}
|
|
|
|
|
|
|
|
if (!_.isUndefined(values.position)) {
|
2019-11-05 18:01:42 +05:00
|
|
|
const cards = await sails.helpers.getCardsForList(listId, inputs.record.id);
|
2019-08-31 04:07:25 +05:00
|
|
|
|
2019-11-05 18:01:42 +05:00
|
|
|
const { position, repositions } = sails.helpers.insertToPositionables(values.position, cards);
|
2019-08-31 04:07:25 +05:00
|
|
|
|
|
|
|
values.position = position;
|
|
|
|
|
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,
|
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
|
|
|
});
|
|
|
|
|
2019-11-05 18:01:42 +05:00
|
|
|
sails.sockets.broadcast(`board:${inputs.record.boardId}`, 'cardUpdate', {
|
|
|
|
item: {
|
|
|
|
id,
|
|
|
|
position: nextPosition,
|
|
|
|
},
|
|
|
|
});
|
2019-08-31 04:07:25 +05:00
|
|
|
});
|
|
|
|
}
|
|
|
|
|
|
|
|
let card;
|
|
|
|
if (!_.isEmpty(values)) {
|
|
|
|
card = await Card.updateOne(inputs.record.id).set(values);
|
|
|
|
|
|
|
|
if (!card) {
|
|
|
|
return exits.success(card);
|
|
|
|
}
|
|
|
|
|
|
|
|
sails.sockets.broadcast(
|
|
|
|
`board:${card.boardId}`,
|
|
|
|
'cardUpdate',
|
|
|
|
{
|
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
|
|
|
);
|
|
|
|
|
|
|
|
if (inputs.toList) {
|
2019-11-05 18:01:42 +05:00
|
|
|
await sails.helpers.createAction(card, inputs.user, {
|
2019-08-31 04:07:25 +05:00
|
|
|
type: 'moveCard',
|
|
|
|
data: {
|
|
|
|
fromList: _.pick(inputs.list, ['id', 'name']),
|
2019-11-05 18:01:42 +05:00
|
|
|
toList: _.pick(inputs.toList, ['id', 'name']),
|
|
|
|
},
|
|
|
|
});
|
2019-08-31 04:07:25 +05:00
|
|
|
}
|
|
|
|
} else {
|
|
|
|
card = inputs.record;
|
|
|
|
}
|
|
|
|
|
|
|
|
if (!_.isUndefined(isSubscribed)) {
|
|
|
|
const cardSubscription = await CardSubscription.findOne({
|
|
|
|
cardId: card.id,
|
2019-11-05 18:01:42 +05:00
|
|
|
userId: inputs.user.id,
|
2019-08-31 04:07:25 +05:00
|
|
|
});
|
|
|
|
|
|
|
|
if (isSubscribed !== !!cardSubscription) {
|
|
|
|
if (isSubscribed) {
|
|
|
|
await CardSubscription.create({
|
|
|
|
cardId: card.id,
|
2019-11-05 18:01:42 +05:00
|
|
|
userId: inputs.user.id,
|
2019-08-31 04:07:25 +05:00
|
|
|
}).tolerate('E_UNIQUE');
|
|
|
|
} else {
|
|
|
|
await CardSubscription.destroyOne({
|
|
|
|
cardId: card.id,
|
2019-11-05 18:01:42 +05:00
|
|
|
userId: inputs.user.id,
|
2019-08-31 04:07:25 +05:00
|
|
|
});
|
|
|
|
}
|
|
|
|
|
|
|
|
sails.sockets.broadcast(
|
|
|
|
`user:${inputs.user.id}`,
|
|
|
|
'cardUpdate',
|
|
|
|
{
|
|
|
|
item: {
|
|
|
|
isSubscribed,
|
2019-11-05 18:01:42 +05:00
|
|
|
id: card.id,
|
|
|
|
},
|
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
|
|
|
);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
return exits.success(card);
|
2019-11-05 18:01:42 +05:00
|
|
|
},
|
2019-08-31 04:07:25 +05:00
|
|
|
};
|