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/boards/update-one.js

110 lines
2.3 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;
}
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: 'json',
2022-12-26 21:10:50 +01:00
custom: valuesValidator,
required: true,
2019-08-31 04:07:25 +05:00
},
project: {
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
},
async fn(inputs) {
2022-12-26 21:10:50 +01:00
const { values } = inputs;
const projectManagerUserIds = await sails.helpers.projects.getManagerUserIds(
inputs.record.projectId,
);
2022-10-03 12:11:19 +02:00
2022-12-26 21:10:50 +01:00
const boardMemberUserIds = await sails.helpers.boards.getMemberUserIds(inputs.record.id);
const boardRelatedUserIds = _.union(projectManagerUserIds, boardMemberUserIds);
2019-08-31 04:07:25 +05:00
2022-12-26 21:10:50 +01:00
if (!_.isUndefined(values.position)) {
const boards = await sails.helpers.projects.getBoards(
2019-08-31 04:07:25 +05:00
inputs.record.projectId,
inputs.record.id,
2019-08-31 04:07:25 +05:00
);
const { position, repositions } = sails.helpers.utils.insertToPositionables(
2022-12-26 21:10:50 +01:00
values.position,
boards,
2019-08-31 04:07:25 +05:00
);
2022-12-26 21:10:50 +01:00
values.position = position;
2019-08-31 04:07:25 +05:00
repositions.forEach(async ({ id, position: nextPosition }) => {
2019-08-31 04:07:25 +05:00
await Board.update({
id,
projectId: inputs.record.projectId,
2019-08-31 04:07:25 +05:00
}).set({
position: nextPosition,
2019-08-31 04:07:25 +05:00
});
2022-12-26 21:10:50 +01:00
boardRelatedUserIds.forEach((userId) => {
2019-08-31 04:07:25 +05:00
sails.sockets.broadcast(`user:${userId}`, 'boardUpdate', {
item: {
id,
position: nextPosition,
},
2019-08-31 04:07:25 +05:00
});
// TODO: send webhooks
2019-08-31 04:07:25 +05:00
});
});
}
2022-12-26 21:10:50 +01:00
const board = await Board.updateOne(inputs.record.id).set({ ...values });
2019-08-31 04:07:25 +05:00
if (board) {
2022-12-26 21:10:50 +01:00
boardRelatedUserIds.forEach((userId) => {
2019-08-31 04:07:25 +05:00
sails.sockets.broadcast(
`user:${userId}`,
'boardUpdate',
{
item: board,
2019-08-31 04:07:25 +05:00
},
inputs.request,
2019-08-31 04:07:25 +05:00
);
});
sails.helpers.utils.sendWebhooks.with({
event: 'boardUpdate',
data: {
item: board,
included: {
projects: [inputs.project],
},
},
user: inputs.actorUser,
});
}
return board;
},
2019-08-31 04:07:25 +05:00
};