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),
|
2019-11-05 18:01:42 +05:00
|
|
|
required: true,
|
2019-08-31 04:07:25 +05:00
|
|
|
},
|
2022-12-16 23:48:06 +01:00
|
|
|
import: {
|
|
|
|
type: 'json',
|
|
|
|
custom: (value) =>
|
|
|
|
value.type &&
|
|
|
|
Object.values(Board.ImportTypes).includes(value.type) &&
|
|
|
|
_.isPlainObject(value.board),
|
|
|
|
},
|
2021-06-24 01:05:22 +05:00
|
|
|
user: {
|
|
|
|
type: 'ref',
|
|
|
|
required: true,
|
|
|
|
},
|
|
|
|
project: {
|
|
|
|
type: 'ref',
|
|
|
|
required: true,
|
|
|
|
},
|
2022-12-16 23:48:06 +01:00
|
|
|
requestId: {
|
|
|
|
type: 'string',
|
|
|
|
isNotEmptyString: true,
|
|
|
|
},
|
2019-08-31 04:07:25 +05:00
|
|
|
request: {
|
2019-11-05 18:01:42 +05:00
|
|
|
type: 'ref',
|
|
|
|
},
|
2019-08-31 04:07:25 +05:00
|
|
|
},
|
|
|
|
|
2021-06-24 01:05:22 +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
|
|
|
|
2021-06-24 01:05:22 +05:00
|
|
|
const { position, repositions } = sails.helpers.utils.insertToPositionables(
|
2019-08-31 04:07:25 +05:00
|
|
|
inputs.values.position,
|
2019-11-05 18:01:42 +05:00
|
|
|
boards,
|
2019-08-31 04:07:25 +05:00
|
|
|
);
|
|
|
|
|
2019-11-05 18:01:42 +05:00
|
|
|
repositions.forEach(async ({ id, position: nextPosition }) => {
|
2019-08-31 04:07:25 +05:00
|
|
|
await Board.update({
|
|
|
|
id,
|
2019-11-05 18:01:42 +05:00
|
|
|
projectId: inputs.project.id,
|
2019-08-31 04:07:25 +05:00
|
|
|
}).set({
|
2019-11-05 18:01:42 +05:00
|
|
|
position: nextPosition,
|
2019-08-31 04:07:25 +05:00
|
|
|
});
|
|
|
|
|
2021-06-24 01:05:22 +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,
|
2019-11-05 18:01:42 +05:00
|
|
|
position: nextPosition,
|
|
|
|
},
|
2019-08-31 04:07:25 +05:00
|
|
|
});
|
|
|
|
});
|
|
|
|
});
|
|
|
|
|
|
|
|
const board = await Board.create({
|
|
|
|
...inputs.values,
|
|
|
|
position,
|
2019-11-05 18:01:42 +05:00
|
|
|
projectId: inputs.project.id,
|
2019-08-31 04:07:25 +05:00
|
|
|
}).fetch();
|
|
|
|
|
2022-12-16 23:48:06 +01:00
|
|
|
if (inputs.import && inputs.import.type === Board.ImportTypes.TRELLO) {
|
|
|
|
await sails.helpers.boards.importFromTrello(inputs.user, board, inputs.import.board);
|
|
|
|
}
|
|
|
|
|
2021-06-24 01:05:22 +05:00
|
|
|
const boardMembership = await BoardMembership.create({
|
|
|
|
boardId: board.id,
|
|
|
|
userId: inputs.user.id,
|
2022-08-19 14:00:40 +02:00
|
|
|
role: BoardMembership.Roles.EDITOR,
|
2021-06-24 01:05:22 +05:00
|
|
|
}).fetch();
|
|
|
|
|
|
|
|
managerUserIds.forEach((userId) => {
|
2019-08-31 04:07:25 +05:00
|
|
|
sails.sockets.broadcast(
|
|
|
|
`user:${userId}`,
|
|
|
|
'boardCreate',
|
|
|
|
{
|
|
|
|
item: board,
|
2022-12-16 23:48:06 +01:00
|
|
|
requestId: inputs.requestId,
|
2019-08-31 04:07:25 +05:00
|
|
|
},
|
2019-11-05 18:01:42 +05:00
|
|
|
inputs.request,
|
2019-08-31 04:07:25 +05:00
|
|
|
);
|
|
|
|
});
|
|
|
|
|
2021-06-24 01:05:22 +05:00
|
|
|
return {
|
|
|
|
board,
|
|
|
|
boardMembership,
|
|
|
|
};
|
2019-11-05 18:01:42 +05:00
|
|
|
},
|
2019-08-31 04:07:25 +05:00
|
|
|
};
|