1
0
Fork 0
mirror of https://github.com/plankanban/planka.git synced 2025-07-19 13:19:44 +02:00
planka/server/api/helpers/boards/create-one.js

120 lines
2.6 KiB
JavaScript
Raw Normal View History

2022-12-26 21:10:50 +01:00
const valuesValidator = (value) => {
if (!_.isPlainObject(value)) {
return false;
}
if (!_.isFinite(value.position)) {
return false;
}
if (!_.isPlainObject(value.project)) {
return false;
}
return true;
};
const importValidator = (value) => {
if (!value.type || !Object.values(Board.ImportTypes).includes(value.type)) {
return false;
}
if (!_.isPlainObject(value.board)) {
return false;
}
return true;
};
2019-08-31 04:07:25 +05:00
module.exports = {
inputs: {
values: {
2022-12-26 21:10:50 +01:00
type: 'ref',
custom: valuesValidator,
required: true,
2019-08-31 04:07:25 +05:00
},
import: {
type: 'json',
2022-12-26 21:10:50 +01:00
custom: importValidator,
},
user: {
type: 'ref',
required: true,
},
requestId: {
type: 'string',
isNotEmptyString: 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(values.project.id);
const boards = await sails.helpers.projects.getBoards(values.project.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
);
repositions.forEach(async ({ id, position: nextPosition }) => {
2019-08-31 04:07:25 +05:00
await Board.update({
id,
2022-12-26 21:10:50 +01:00
projectId: values.project.id,
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
// TODO: move out of loop
const boardMemberUserIds = await sails.helpers.boards.getMemberUserIds(id);
const boardRelatedUserIds = _.union(projectManagerUserIds, boardMemberUserIds);
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
});
});
});
const board = await Board.create({
2022-12-26 21:10:50 +01:00
...values,
2019-08-31 04:07:25 +05:00
position,
2022-12-26 21:10:50 +01:00
projectId: values.project.id,
2019-08-31 04:07:25 +05:00
}).fetch();
if (inputs.import && inputs.import.type === Board.ImportTypes.TRELLO) {
await sails.helpers.boards.importFromTrello(inputs.user, board, inputs.import.board);
}
const boardMembership = await BoardMembership.create({
boardId: board.id,
userId: inputs.user.id,
role: BoardMembership.Roles.EDITOR,
}).fetch();
2022-12-26 21:10:50 +01:00
projectManagerUserIds.forEach((userId) => {
2019-08-31 04:07:25 +05:00
sails.sockets.broadcast(
`user:${userId}`,
'boardCreate',
{
item: board,
requestId: inputs.requestId,
2019-08-31 04:07:25 +05:00
},
inputs.request,
2019-08-31 04:07:25 +05:00
);
});
return {
board,
boardMembership,
};
},
2019-08-31 04:07:25 +05:00
};