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
|
|
|
},
|
2020-08-04 01:32:46 +05:00
|
|
|
toBoard: {
|
|
|
|
type: 'ref',
|
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
|
|
|
},
|
2020-08-04 01:32:46 +05:00
|
|
|
values: {
|
|
|
|
type: 'json',
|
|
|
|
custom: (value) => {
|
|
|
|
if (!_.isPlainObject(value)) {
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
|
|
|
|
if (!_.isUndefined(value.position) && !_.isFinite(value.position)) {
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
|
|
|
|
return true;
|
|
|
|
},
|
|
|
|
required: true,
|
2020-05-09 05:30:52 +05:00
|
|
|
},
|
2020-08-04 01:32:46 +05:00
|
|
|
board: {
|
2019-08-31 04:07:25 +05:00
|
|
|
type: 'ref',
|
|
|
|
},
|
2020-08-04 01:32:46 +05:00
|
|
|
list: {
|
2020-05-09 05:30:52 +05:00
|
|
|
type: 'ref',
|
|
|
|
},
|
2019-08-31 04:07:25 +05:00
|
|
|
user: {
|
|
|
|
type: 'ref',
|
|
|
|
},
|
|
|
|
request: {
|
2019-11-05 18:01:42 +05:00
|
|
|
type: 'ref',
|
|
|
|
},
|
2019-08-31 04:07:25 +05:00
|
|
|
},
|
|
|
|
|
2020-04-23 03:02:53 +05:00
|
|
|
exits: {
|
2020-08-04 01:32:46 +05:00
|
|
|
boardMustBePresent: {},
|
|
|
|
listMustBePresent: {},
|
|
|
|
toListMustBelongToBoard: {},
|
|
|
|
toListMustBePresent: {},
|
|
|
|
positionMustBeInValues: {},
|
|
|
|
userMustBePresent: {},
|
2020-04-23 03:02:53 +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;
|
|
|
|
|
2020-08-04 01:32:46 +05:00
|
|
|
if (inputs.toBoard || inputs.toList || !_.isUndefined(values.position)) {
|
|
|
|
if (!inputs.board) {
|
|
|
|
throw 'boardMustBePresent';
|
2020-04-23 03:02:53 +05:00
|
|
|
}
|
2019-08-31 04:07:25 +05:00
|
|
|
|
2020-08-04 01:32:46 +05:00
|
|
|
if (inputs.toBoard) {
|
|
|
|
if (inputs.toBoard.id === inputs.board.id) {
|
|
|
|
delete inputs.toBoard; // eslint-disable-line no-param-reassign
|
|
|
|
} else {
|
|
|
|
values.boardId = inputs.toBoard.id;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
const board = inputs.toBoard || inputs.board;
|
|
|
|
|
|
|
|
if (inputs.toList) {
|
|
|
|
if (inputs.board.type === 'kanban' && !inputs.list) {
|
|
|
|
throw 'listMustBePresent';
|
|
|
|
}
|
|
|
|
|
|
|
|
if (inputs.toList.boardId !== board.id) {
|
|
|
|
throw 'toListMustBelongToBoard';
|
|
|
|
}
|
|
|
|
|
|
|
|
if (
|
|
|
|
board.type === 'collection' ||
|
|
|
|
(inputs.board.type === 'kanban' && inputs.toList.id === inputs.list.id)
|
|
|
|
) {
|
|
|
|
delete inputs.toList; // eslint-disable-line no-param-reassign
|
|
|
|
} else {
|
|
|
|
values.listId = inputs.toList.id;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
if (inputs.toList) {
|
|
|
|
if (_.isUndefined(values.position)) {
|
|
|
|
throw 'positionMustBeInValues';
|
|
|
|
}
|
|
|
|
} else if (inputs.toBoard) {
|
|
|
|
if (inputs.toBoard.type === 'kanban') {
|
|
|
|
throw 'toListMustBePresent';
|
|
|
|
}
|
|
|
|
|
|
|
|
if (inputs.board.type === 'kanban') {
|
|
|
|
values.listId = null;
|
|
|
|
values.position = null;
|
2020-05-05 01:30:06 +05:00
|
|
|
}
|
2019-08-31 04:07:25 +05:00
|
|
|
}
|
2020-04-23 03:02:53 +05:00
|
|
|
}
|
|
|
|
|
2020-08-04 01:32:46 +05:00
|
|
|
if ((!_.isUndefined(isSubscribed) || inputs.toBoard || inputs.toList) && !inputs.user) {
|
|
|
|
throw 'userMustBePresent';
|
2019-08-31 04:07:25 +05:00
|
|
|
}
|
|
|
|
|
2020-08-04 01:32:46 +05:00
|
|
|
if (!_.isNil(values.position)) {
|
2020-04-23 03:02:53 +05:00
|
|
|
const cards = await sails.helpers.getCardsForList(
|
|
|
|
values.listId || inputs.record.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
|
|
|
|
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
|
|
|
});
|
|
|
|
|
2020-08-04 01:32:46 +05:00
|
|
|
sails.sockets.broadcast(`board:${values.boardId || inputs.record.boardId}`, 'cardUpdate', {
|
2019-11-05 18:01:42 +05:00
|
|
|
item: {
|
|
|
|
id,
|
|
|
|
position: nextPosition,
|
|
|
|
},
|
|
|
|
});
|
2019-08-31 04:07:25 +05:00
|
|
|
});
|
2020-08-04 01:32:46 +05:00
|
|
|
|
|
|
|
values.position = position;
|
2019-08-31 04:07:25 +05:00
|
|
|
}
|
|
|
|
|
|
|
|
let card;
|
|
|
|
if (!_.isEmpty(values)) {
|
2020-05-09 05:30:52 +05:00
|
|
|
let prevLabels;
|
2020-08-04 01:32:46 +05:00
|
|
|
if (inputs.toBoard) {
|
2020-05-09 05:30:52 +05:00
|
|
|
if (inputs.toBoard.projectId !== inputs.board.projectId) {
|
|
|
|
const userIds = await sails.helpers.getMembershipUserIdsForProject(
|
|
|
|
inputs.toBoard.projectId,
|
|
|
|
);
|
|
|
|
|
|
|
|
await CardSubscription.destroy({
|
|
|
|
cardId: inputs.record.id,
|
|
|
|
userId: {
|
|
|
|
'!=': userIds,
|
|
|
|
},
|
|
|
|
});
|
|
|
|
|
|
|
|
await CardMembership.destroy({
|
|
|
|
cardId: inputs.record.id,
|
|
|
|
userId: {
|
|
|
|
'!=': userIds,
|
|
|
|
},
|
|
|
|
});
|
|
|
|
}
|
2020-05-05 03:33:29 +05:00
|
|
|
|
2020-05-09 05:30:52 +05:00
|
|
|
prevLabels = await sails.helpers.getLabelsForCard(inputs.record.id);
|
2020-05-05 03:33:29 +05:00
|
|
|
|
|
|
|
await CardLabel.destroy({
|
|
|
|
cardId: inputs.record.id,
|
|
|
|
});
|
|
|
|
}
|
|
|
|
|
2019-08-31 04:07:25 +05:00
|
|
|
card = await Card.updateOne(inputs.record.id).set(values);
|
|
|
|
|
|
|
|
if (!card) {
|
|
|
|
return exits.success(card);
|
|
|
|
}
|
|
|
|
|
2020-08-04 01:32:46 +05:00
|
|
|
if (inputs.toBoard) {
|
2020-05-09 05:30:52 +05:00
|
|
|
sails.sockets.broadcast(
|
|
|
|
`board:${inputs.board.id}`,
|
|
|
|
'cardDelete',
|
|
|
|
{
|
|
|
|
item: inputs.record,
|
|
|
|
},
|
|
|
|
inputs.request,
|
|
|
|
);
|
|
|
|
|
|
|
|
const labels = await sails.helpers.getLabelsForBoard(card.boardId);
|
|
|
|
const labelByNameMap = _.keyBy(labels, 'name');
|
|
|
|
|
|
|
|
const labelIds = await Promise.all(
|
2020-05-10 02:06:14 +05:00
|
|
|
prevLabels.map(async (prevLabel) => {
|
2020-05-09 05:30:52 +05:00
|
|
|
if (labelByNameMap[prevLabel.name]) {
|
|
|
|
return labelByNameMap[prevLabel.name].id;
|
|
|
|
}
|
|
|
|
|
|
|
|
const { id } = await sails.helpers.createLabel(
|
|
|
|
inputs.toBoard,
|
|
|
|
_.omit(prevLabel, ['id', 'boardId']),
|
|
|
|
);
|
|
|
|
|
|
|
|
return id;
|
|
|
|
}),
|
|
|
|
);
|
|
|
|
|
2020-05-10 02:06:14 +05:00
|
|
|
await Promise.all(
|
|
|
|
labelIds.map(async (labelId) => {
|
|
|
|
await CardLabel.create({
|
|
|
|
labelId,
|
|
|
|
cardId: card.id,
|
|
|
|
})
|
|
|
|
.tolerate('E_UNIQUE')
|
|
|
|
.fetch();
|
|
|
|
}),
|
|
|
|
);
|
2020-05-05 03:33:29 +05:00
|
|
|
|
2020-05-09 05:30:52 +05:00
|
|
|
const cardMemberships = await sails.helpers.getMembershipsForCard(card.id);
|
|
|
|
const cardLabels = await sails.helpers.getCardLabelsForCard(card.id);
|
|
|
|
const tasks = await sails.helpers.getTasksForCard(card.id);
|
|
|
|
const attachments = await sails.helpers.getAttachmentsForCard(card.id);
|
|
|
|
|
2020-05-10 02:06:14 +05:00
|
|
|
sails.sockets.broadcast(`board:${card.boardId}`, 'cardCreate', {
|
|
|
|
item: card,
|
|
|
|
included: {
|
|
|
|
cardMemberships,
|
|
|
|
cardLabels,
|
|
|
|
tasks,
|
|
|
|
attachments,
|
2020-05-09 05:30:52 +05:00
|
|
|
},
|
2020-05-10 02:06:14 +05:00
|
|
|
});
|
2020-05-09 05:30:52 +05:00
|
|
|
|
|
|
|
const userIds = await sails.helpers.getSubscriptionUserIdsForCard(card.id);
|
|
|
|
|
|
|
|
userIds.forEach((userId) => {
|
2020-05-10 02:06:14 +05:00
|
|
|
sails.sockets.broadcast(`user:${userId}`, 'cardUpdate', {
|
|
|
|
item: {
|
|
|
|
id: card.id,
|
|
|
|
isSubscribed: true,
|
2020-05-09 05:30:52 +05:00
|
|
|
},
|
2020-05-10 02:06:14 +05:00
|
|
|
});
|
2020-05-09 05:30:52 +05:00
|
|
|
});
|
|
|
|
} else {
|
|
|
|
sails.sockets.broadcast(
|
|
|
|
`board:${card.boardId}`,
|
|
|
|
'cardUpdate',
|
|
|
|
{
|
|
|
|
item: card,
|
|
|
|
},
|
|
|
|
inputs.request,
|
|
|
|
);
|
|
|
|
}
|
2019-08-31 04:07:25 +05:00
|
|
|
|
2020-08-04 01:32:46 +05:00
|
|
|
if (!inputs.toBoard && inputs.toList) {
|
2020-05-05 01:30:06 +05:00
|
|
|
// TODO: add transfer action
|
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
|
|
|
};
|