1
0
Fork 0
mirror of https://github.com/plankanban/planka.git synced 2025-07-19 21:29:43 +02:00
planka/server/api/helpers/cards/update-one.js

282 lines
6.8 KiB
JavaScript
Raw Normal View History

2019-08-31 04:07:25 +05:00
module.exports = {
inputs: {
record: {
type: 'ref',
required: true,
2019-08-31 04:07:25 +05:00
},
values: {
type: 'json',
custom: (value) => {
if (!_.isPlainObject(value)) {
return false;
}
if (!_.isUndefined(value.position) && !_.isFinite(value.position)) {
return false;
}
return true;
},
defaultsTo: {},
},
nextBoard: {
2019-08-31 04:07:25 +05:00
type: 'ref',
},
nextList: {
type: 'ref',
},
2019-08-31 04:07:25 +05:00
user: {
type: 'ref',
},
board: {
type: 'ref',
},
list: {
type: 'ref',
},
2019-08-31 04:07:25 +05:00
request: {
type: 'ref',
},
2019-08-31 04:07:25 +05:00
},
2020-04-23 03:02:53 +05:00
exits: {
boardMustBePresent: {},
listMustBePresent: {},
nextListMustBelongToBoard: {},
nextListMustBePresent: {},
positionMustBeInValues: {},
userMustBePresent: {},
2020-04-23 03:02:53 +05:00
},
async fn(inputs) {
2019-08-31 04:07:25 +05:00
const { isSubscribed, ...values } = inputs.values;
if (inputs.nextBoard || inputs.nextList || !_.isUndefined(values.position)) {
if (!inputs.board) {
throw 'boardMustBePresent';
2020-04-23 03:02:53 +05:00
}
2019-08-31 04:07:25 +05:00
if (inputs.nextBoard) {
if (inputs.nextBoard.id === inputs.board.id) {
delete inputs.nextBoard; // eslint-disable-line no-param-reassign
} else {
values.boardId = inputs.nextBoard.id;
}
}
const board = inputs.nextBoard || inputs.board;
if (inputs.nextList) {
if (inputs.board.type === Board.Types.KANBAN && !inputs.list) {
throw 'listMustBePresent';
}
if (inputs.nextList.boardId !== board.id) {
throw 'nextListMustBelongToBoard';
}
if (
board.type === Board.Types.COLLECTION ||
(inputs.board.type === Board.Types.KANBAN && inputs.nextList.id === inputs.list.id)
) {
delete inputs.nextList; // eslint-disable-line no-param-reassign
} else {
values.listId = inputs.nextList.id;
}
}
if (inputs.nextList) {
if (_.isUndefined(values.position)) {
throw 'positionMustBeInValues';
}
} else if (inputs.nextBoard) {
if (inputs.nextBoard.type === Board.Types.KANBAN) {
throw 'nextListMustBePresent';
}
if (inputs.board.type === Board.Types.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
}
if ((!_.isUndefined(isSubscribed) || inputs.nextBoard || inputs.nextList) && !inputs.user) {
throw 'userMustBePresent';
2019-08-31 04:07:25 +05:00
}
if (!_.isNil(values.position)) {
const boardId = values.boardId || inputs.record.boardId;
const listId = values.listId || inputs.record.listId;
2019-08-31 04:07:25 +05:00
const cards = await sails.helpers.lists.getCards(listId, inputs.record.id);
const { position, repositions } = sails.helpers.utils.insertToPositionables(
values.position,
cards,
);
2019-08-31 04:07:25 +05:00
repositions.forEach(async ({ id, position: nextPosition }) => {
2019-08-31 04:07:25 +05:00
await Card.update({
id,
listId,
2019-08-31 04:07:25 +05:00
}).set({
position: nextPosition,
2019-08-31 04:07:25 +05:00
});
sails.sockets.broadcast(`board:${boardId}`, 'cardUpdate', {
item: {
id,
position: nextPosition,
},
});
2019-08-31 04:07:25 +05:00
});
values.position = position;
2019-08-31 04:07:25 +05:00
}
let card;
if (!_.isEmpty(values)) {
let prevLabels;
if (inputs.nextBoard) {
if (inputs.nextBoard.projectId !== inputs.board.projectId) {
const memberUserIds = await sails.helpers.boards.getMemberUserIds(inputs.nextBoard.id);
await CardSubscription.destroy({
cardId: inputs.record.id,
userId: {
'!=': memberUserIds,
},
});
await CardMembership.destroy({
cardId: inputs.record.id,
userId: {
'!=': memberUserIds,
},
});
}
prevLabels = await sails.helpers.cards.getLabels(inputs.record.id);
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 card;
2019-08-31 04:07:25 +05:00
}
if (inputs.nextBoard) {
const labels = await sails.helpers.boards.getLabels(card.boardId);
const labelByNameMap = _.keyBy(labels, 'name');
const labelIds = await Promise.all(
prevLabels.map(async (prevLabel) => {
if (labelByNameMap[prevLabel.name]) {
return labelByNameMap[prevLabel.name].id;
}
const { id } = await sails.helpers.labels.createOne(
_.omit(prevLabel, ['id', 'boardId']),
inputs.nextBoard,
);
return id;
}),
);
await Promise.all(
labelIds.map(async (labelId) =>
CardLabel.create({
labelId,
cardId: card.id,
})
.tolerate('E_UNIQUE')
.fetch(),
),
);
sails.sockets.broadcast(`board:${card.boardId}`, 'cardUpdate', {
item: card,
});
const subscriptionUserIds = await sails.helpers.cards.getSubscriptionUserIds(card.id);
subscriptionUserIds.forEach((userId) => {
sails.sockets.broadcast(`user:${userId}`, 'cardUpdate', {
item: {
id: card.id,
isSubscribed: true,
},
});
});
} else {
sails.sockets.broadcast(
`board:${card.boardId}`,
'cardUpdate',
{
item: card,
},
inputs.request,
);
}
2019-08-31 04:07:25 +05:00
if (!inputs.nextBoard && inputs.nextList) {
2020-05-05 01:30:06 +05:00
// TODO: add transfer action
await sails.helpers.actions.createOne(
{
type: Action.Types.MOVE_CARD,
data: {
fromList: _.pick(inputs.list, ['id', 'name']),
toList: _.pick(inputs.nextList, ['id', 'name']),
},
},
inputs.user,
card,
);
2019-08-31 04:07:25 +05:00
}
} else {
card = inputs.record;
}
if (!_.isUndefined(isSubscribed)) {
const cardSubscription = await CardSubscription.findOne({
cardId: card.id,
userId: inputs.user.id,
2019-08-31 04:07:25 +05:00
});
if (isSubscribed !== !!cardSubscription) {
if (isSubscribed) {
await CardSubscription.create({
cardId: card.id,
userId: inputs.user.id,
2019-08-31 04:07:25 +05:00
}).tolerate('E_UNIQUE');
} else {
await CardSubscription.destroyOne({
cardId: card.id,
userId: inputs.user.id,
2019-08-31 04:07:25 +05:00
});
}
sails.sockets.broadcast(
`user:${inputs.user.id}`,
'cardUpdate',
{
item: {
isSubscribed,
id: card.id,
},
2019-08-31 04:07:25 +05:00
},
inputs.request,
2019-08-31 04:07:25 +05:00
);
}
}
return card;
},
2019-08-31 04:07:25 +05:00
};