1
0
Fork 0
mirror of https://github.com/plankanban/planka.git synced 2025-07-18 12:49:43 +02:00
planka/server/api/helpers/boards/update-one.js
HannesOberreiter 193daf6cfb feat: Events via webhook (#771)
Closes #215, closes #656
2024-06-06 20:22:14 +02:00

96 lines
2.1 KiB
JavaScript

const valuesValidator = (value) => {
if (!_.isPlainObject(value)) {
return false;
}
if (!_.isUndefined(value.position) && !_.isFinite(value.position)) {
return false;
}
return true;
};
module.exports = {
inputs: {
record: {
type: 'ref',
required: true,
},
values: {
type: 'json',
custom: valuesValidator,
required: true,
},
request: {
type: 'ref',
},
},
async fn(inputs) {
const { values } = inputs;
const projectManagerUserIds = await sails.helpers.projects.getManagerUserIds(
inputs.record.projectId,
);
const boardMemberUserIds = await sails.helpers.boards.getMemberUserIds(inputs.record.id);
const boardRelatedUserIds = _.union(projectManagerUserIds, boardMemberUserIds);
if (!_.isUndefined(values.position)) {
const boards = await sails.helpers.projects.getBoards(
inputs.record.projectId,
inputs.record.id,
);
const { position, repositions } = sails.helpers.utils.insertToPositionables(
values.position,
boards,
);
values.position = position;
repositions.forEach(async ({ id, position: nextPosition }) => {
await Board.update({
id,
projectId: inputs.record.projectId,
}).set({
position: nextPosition,
});
boardRelatedUserIds.forEach((userId) => {
sails.sockets.broadcast(`user:${userId}`, 'boardUpdate', {
item: {
id,
position: nextPosition,
},
});
});
});
}
const board = await Board.updateOne(inputs.record.id).set({ ...values });
if (board) {
boardRelatedUserIds.forEach((userId) => {
sails.sockets.broadcast(
`user:${userId}`,
'boardUpdate',
{
item: board,
},
inputs.request,
);
});
}
await sails.helpers.utils.sendWebhook.with({
event: 'BOARD_UPDATE',
data: board,
projectId: board.projectId,
user: inputs.request.currentUser,
board,
});
return board;
},
};