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

315 lines
7.1 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)) {
if (!_.isPlainObject(value.project)) {
return false;
}
if (!_.isPlainObject(value.board)) {
return false;
}
2022-12-26 21:10:50 +01:00
}
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,
},
project: {
2019-08-31 04:07:25 +05:00
type: 'ref',
required: true,
2019-08-31 04:07:25 +05:00
},
board: {
type: 'ref',
required: true,
},
list: {
type: 'ref',
required: true,
},
actorUser: {
type: 'ref',
required: true,
},
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: {},
boardInValuesMustBelongToProject: {},
2022-12-26 21:10:50 +01:00
listMustBeInValues: {},
listInValuesMustBelongToBoard: {},
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 (values.project && values.project.id === inputs.project.id) {
delete values.project;
}
2019-08-31 04:07:25 +05:00
const project = values.project || inputs.project;
if (values.board) {
if (values.board.projectId !== project.id) {
throw 'boardInValuesMustBelongToProject';
}
if (values.board.id === inputs.board.id) {
delete values.board;
} else {
values.boardId = values.board.id;
}
}
const board = values.board || inputs.board;
if (values.list) {
if (values.list.boardId !== board.id) {
throw 'listInValuesMustBelongToBoard';
}
if (values.list.id === inputs.list.id) {
delete values.list;
} else {
values.listId = values.list.id;
2019-08-31 04:07:25 +05:00
}
} else if (values.board) {
throw 'listMustBeInValues';
2020-04-23 03:02:53 +05:00
}
const list = values.list || inputs.list;
if (values.list && _.isUndefined(values.position)) {
throw 'positionMustBeInValues';
2019-08-31 04:07:25 +05:00
}
2023-01-08 22:10:41 +01:00
if (!_.isUndefined(values.position)) {
const cards = await sails.helpers.lists.getCards(list.id, 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: list.id,
2019-08-31 04:07:25 +05:00
}).set({
position: nextPosition,
2019-08-31 04:07:25 +05:00
});
sails.sockets.broadcast(`board:${board.id}`, 'cardUpdate', {
item: {
id,
position: nextPosition,
},
});
// TODO: send webhooks
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({
project,
2022-12-26 21:10:50 +01:00
values: {
..._.omit(label, ['id', 'boardId']),
board: values.board,
},
actorUser: inputs.actorUser,
2022-12-26 21:10:50 +01:00
});
return id;
}),
);
await Promise.all(
labelIds.map(async (labelId) =>
CardLabel.create({
labelId,
cardId: card.id,
})
.tolerate('E_UNIQUE')
.fetch(),
),
);
sails.sockets.broadcast(
`board:${inputs.record.boardId}`,
'cardDelete', // TODO: introduce separate event
{
item: inputs.record,
},
inputs.request,
);
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,
},
});
// TODO: send webhooks
});
} else {
sails.sockets.broadcast(
`board:${card.boardId}`,
'cardUpdate',
{
item: card,
},
inputs.request,
);
}
2019-08-31 04:07:25 +05:00
sails.helpers.utils.sendWebhooks.with({
event: 'cardUpdate',
data: {
item: card,
included: {
projects: [project],
boards: [board],
lists: [list],
},
},
user: inputs.actorUser,
});
2022-12-26 21:10:50 +01:00
if (!values.board && values.list) {
await sails.helpers.actions.createOne.with({
project,
board,
list,
2022-12-26 21:10:50 +01:00
values: {
card,
user: inputs.actorUser,
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']),
},
},
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)) {
const prevIsSubscribed = await sails.helpers.users.isCardSubscriber(
inputs.actorUser.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.actorUser.id,
2019-08-31 04:07:25 +05:00
}).tolerate('E_UNIQUE');
} else {
await CardSubscription.destroyOne({
cardId: card.id,
userId: inputs.actorUser.id,
2019-08-31 04:07:25 +05:00
});
}
sails.sockets.broadcast(
`user:${inputs.actorUser.id}`,
2019-08-31 04:07:25 +05:00
'cardUpdate',
{
item: {
isSubscribed,
id: card.id,
},
2019-08-31 04:07:25 +05:00
},
inputs.request,
2019-08-31 04:07:25 +05:00
);
// TODO: send webhooks
2019-08-31 04:07:25 +05:00
}
}
return card;
},
2019-08-31 04:07:25 +05:00
};