1
0
Fork 0
mirror of https://github.com/plankanban/planka.git synced 2025-07-18 20:59:44 +02:00
planka/server/api/helpers/cards/update-one.js

285 lines
6.6 KiB
JavaScript
Raw Normal View History

2022-12-26 21:10:50 +01:00
const valuesValidator = (value) => {
if (!_.isPlainObject(value)) {
return false;
}
if (!_.isUndefined(value.position) && !_.isFinite(value.position)) {
return false;
}
if (!_.isUndefined(value.board) && !_.isPlainObject(value.board)) {
return false;
}
if (!_.isUndefined(value.list) && !_.isPlainObject(value.list)) {
return false;
}
return true;
};
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: 'ref',
2022-12-26 21:10:50 +01:00
custom: valuesValidator,
required: true,
},
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: {
positionMustBeInValues: {},
2022-12-26 21:10:50 +01:00
listMustBeInValues: {},
listInValuesMustBelongToBoard: {},
userMustBePresent: {},
2022-12-26 21:10:50 +01:00
boardMustBePresent: {},
listMustBePresent: {},
2020-04-23 03:02:53 +05:00
},
async fn(inputs) {
2019-08-31 04:07:25 +05:00
const { isSubscribed, ...values } = inputs.values;
2022-12-26 21:10:50 +01:00
if (values.board || values.list || !_.isUndefined(values.position)) {
if (!inputs.board) {
throw 'boardMustBePresent';
2020-04-23 03:02:53 +05:00
}
2019-08-31 04:07:25 +05:00
2022-12-26 21:10:50 +01:00
if (values.board) {
if (values.board.id === inputs.board.id) {
delete values.board;
} else {
2022-12-26 21:10:50 +01:00
values.boardId = values.board.id;
}
}
2022-12-26 21:10:50 +01:00
const board = values.board || inputs.board;
2022-12-26 21:10:50 +01:00
if (values.list) {
if (!inputs.list) {
throw 'listMustBePresent';
}
2022-12-26 21:10:50 +01:00
if (values.list.boardId !== board.id) {
throw 'listInValuesMustBelongToBoard';
}
2022-12-26 21:10:50 +01:00
if (values.list.id === inputs.list.id) {
delete values.list;
} else {
2022-12-26 21:10:50 +01:00
values.listId = values.list.id;
}
}
2022-12-26 21:10:50 +01:00
if (values.list) {
if (_.isUndefined(values.position)) {
throw 'positionMustBeInValues';
}
2022-12-26 21:10:50 +01:00
} else if (values.board) {
throw 'listMustBeInValues';
2019-08-31 04:07:25 +05:00
}
2020-04-23 03:02:53 +05:00
}
2022-12-26 21:10:50 +01:00
if ((!_.isUndefined(isSubscribed) || values.board || values.list) && !inputs.user) {
throw 'userMustBePresent';
2019-08-31 04:07:25 +05:00
}
2023-01-08 22:10:41 +01:00
if (!_.isUndefined(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
2022-12-26 21:10:50 +01:00
values.position = position;
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
});
}
let card;
2022-12-26 21:10:50 +01:00
if (_.isEmpty(values)) {
card = inputs.record;
} else {
let prevLabels;
2022-12-26 21:10:50 +01:00
if (values.board) {
const boardMemberUserIds = await sails.helpers.boards.getMemberUserIds(values.board.id);
2022-12-26 21:10:50 +01:00
await CardSubscription.destroy({
cardId: inputs.record.id,
userId: {
'!=': boardMemberUserIds,
},
});
await CardMembership.destroy({
cardId: inputs.record.id,
userId: {
'!=': boardMemberUserIds,
},
});
prevLabels = await sails.helpers.cards.getLabels(inputs.record.id);
await CardLabel.destroy({
cardId: inputs.record.id,
});
}
2022-12-26 21:10:50 +01:00
card = await Card.updateOne(inputs.record.id).set({ ...values });
2019-08-31 04:07:25 +05:00
if (!card) {
return card;
2019-08-31 04:07:25 +05:00
}
2022-12-26 21:10:50 +01:00
if (values.board) {
const labels = await sails.helpers.boards.getLabels(card.boardId);
2022-12-26 21:10:50 +01:00
const labelByName = _.keyBy(labels, 'name');
const labelIds = await Promise.all(
2022-12-26 21:10:50 +01:00
prevLabels.map(async (label) => {
if (labelByName[label.name]) {
return labelByName[label.name].id;
}
2022-12-26 21:10:50 +01:00
const { id } = await sails.helpers.labels.createOne.with({
values: {
..._.omit(label, ['id', 'boardId']),
board: values.board,
},
});
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
2022-12-26 21:10:50 +01:00
if (!values.board && values.list) {
await sails.helpers.actions.createOne.with({
values: {
card,
user: inputs.user,
type: Action.Types.MOVE_CARD,
data: {
fromList: _.pick(inputs.list, ['id', 'name']),
2022-12-26 21:10:50 +01:00
toList: _.pick(values.list, ['id', 'name']),
},
},
board: inputs.board,
request: inputs.request,
2022-12-26 21:10:50 +01:00
});
2019-08-31 04:07:25 +05:00
}
2022-12-26 21:10:50 +01:00
// TODO: add transfer action
2019-08-31 04:07:25 +05:00
}
if (!_.isUndefined(isSubscribed)) {
2022-12-26 21:10:50 +01:00
const prevIsSubscribed = await sails.helpers.users.isCardSubscriber(inputs.user.id, card.id);
2019-08-31 04:07:25 +05:00
2022-12-26 21:10:50 +01:00
if (isSubscribed !== prevIsSubscribed) {
2019-08-31 04:07:25 +05:00
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
);
}
}
await sails.helpers.utils.sendWebhook.with({
event: 'CARD_UPDATE',
data: card,
projectId: inputs.board.projectId,
user: inputs.request.currentUser,
card,
board: inputs.board,
});
return card;
},
2019-08-31 04:07:25 +05:00
};