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/create-board.js
2019-08-31 04:07:25 +05:00

70 lines
1.4 KiB
JavaScript

module.exports = {
inputs: {
project: {
type: 'ref',
required: true
},
values: {
type: 'json',
custom: value => _.isPlainObject(value) && _.isFinite(value.position),
required: true
},
request: {
type: 'ref'
}
},
fn: async function(inputs, exits) {
const boards = await sails.helpers.getBoardsForProject(inputs.project.id);
const { position, repositions } = sails.helpers.insertToPositionables(
inputs.values.position,
boards
);
const userIds = await sails.helpers.getMembershipUserIdsForProject(
inputs.project.id
);
repositions.forEach(async ({ id, position }) => {
await Board.update({
id,
projectId: inputs.project.id
}).set({
position
});
userIds.forEach(userId => {
sails.sockets.broadcast(`user:${userId}`, 'boardUpdate', {
item: {
id,
position
}
});
});
});
const board = await Board.create({
...inputs.values,
position,
projectId: inputs.project.id
}).fetch();
userIds.forEach(userId => {
sails.sockets.broadcast(
`user:${userId}`,
'boardCreate',
{
item: board,
included: {
lists: [],
labels: []
}
},
inputs.request
);
});
return exits.success(board);
}
};