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

80 lines
1.8 KiB
JavaScript
Raw Normal View History

2019-08-31 04:07:25 +05:00
module.exports = {
inputs: {
values: {
type: 'json',
2020-04-03 00:35:25 +05:00
custom: (value) => _.isPlainObject(value) && _.isFinite(value.position),
required: true,
2019-08-31 04:07:25 +05:00
},
user: {
type: 'ref',
required: true,
},
project: {
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) {
const managerUserIds = await sails.helpers.projects.getManagerUserIds(inputs.project.id);
const boards = await sails.helpers.projects.getBoards(inputs.project.id);
2019-08-31 04:07:25 +05:00
const { position, repositions } = sails.helpers.utils.insertToPositionables(
2019-08-31 04:07:25 +05:00
inputs.values.position,
boards,
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.project.id,
2019-08-31 04:07:25 +05:00
}).set({
position: nextPosition,
2019-08-31 04:07:25 +05:00
});
const memberUserIds = await sails.helpers.boards.getMemberUserIds(id);
const userIds = _.union(managerUserIds, memberUserIds);
2020-04-03 00:35:25 +05:00
userIds.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
});
});
});
const board = await Board.create({
...inputs.values,
position,
projectId: inputs.project.id,
2019-08-31 04:07:25 +05:00
}).fetch();
const boardMembership = await BoardMembership.create({
boardId: board.id,
userId: inputs.user.id,
role: BoardMembership.Roles.EDITOR,
}).fetch();
managerUserIds.forEach((userId) => {
2019-08-31 04:07:25 +05:00
sails.sockets.broadcast(
`user:${userId}`,
'boardCreate',
{
item: board,
},
inputs.request,
2019-08-31 04:07:25 +05:00
);
});
return {
board,
boardMembership,
};
},
2019-08-31 04:07:25 +05:00
};