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

97 lines
2.5 KiB
JavaScript
Raw Normal View History

2019-08-31 04:07:25 +05:00
const Errors = {
BOARD_NOT_FOUND: {
2020-04-03 00:35:25 +05:00
boardNotFound: 'Board not found',
},
2019-08-31 04:07:25 +05:00
};
module.exports = {
inputs: {
id: {
type: 'string',
regex: /^[0-9]+$/,
required: true,
},
2019-08-31 04:07:25 +05:00
},
exits: {
2020-04-03 00:35:25 +05:00
boardNotFound: {
responseType: 'notFound',
},
2019-08-31 04:07:25 +05:00
},
async fn(inputs) {
2019-08-31 04:07:25 +05:00
const { currentUser } = this.req;
const { board, project } = await sails.helpers.boards
.getProjectPath(inputs.id)
2020-04-03 00:35:25 +05:00
.intercept('pathNotFound', () => Errors.BOARD_NOT_FOUND);
2019-08-31 04:07:25 +05:00
const isBoardMember = await sails.helpers.users.isBoardMember(currentUser.id, board.id);
if (!isBoardMember) {
const isProjectManager = await sails.helpers.users.isProjectManager(
currentUser.id,
project.id,
);
2019-08-31 04:07:25 +05:00
if (!isProjectManager) {
throw Errors.BOARD_NOT_FOUND; // Forbidden
}
2019-08-31 04:07:25 +05:00
}
const boardMemberships = await sails.helpers.boards.getBoardMemberships(board.id);
2019-08-31 04:07:25 +05:00
const userIds = sails.helpers.utils.mapRecords(boardMemberships, 'userId');
const users = await sails.helpers.users.getMany(userIds);
2019-08-31 04:07:25 +05:00
const labels = await sails.helpers.boards.getLabels(board.id);
const lists = await sails.helpers.boards.getLists(board.id);
2019-08-31 04:07:25 +05:00
2022-12-26 21:10:50 +01:00
const cards = await sails.helpers.boards.getCards(board.id);
const cardIds = sails.helpers.utils.mapRecords(cards);
2019-08-31 04:07:25 +05:00
const cardSubscriptions = await sails.helpers.cardSubscriptions.getMany({
cardId: cardIds,
userId: currentUser.id,
});
const cardMemberships = await sails.helpers.cards.getCardMemberships(cardIds);
const cardLabels = await sails.helpers.cards.getCardLabels(cardIds);
const tasks = await sails.helpers.cards.getTasks(cardIds);
const attachments = await sails.helpers.cards.getAttachments(cardIds);
2019-08-31 04:07:25 +05:00
const isSubscribedByCardId = cardSubscriptions.reduce(
(result, cardSubscription) => ({
...result,
[cardSubscription.cardId]: true,
2019-08-31 04:07:25 +05:00
}),
{},
2019-08-31 04:07:25 +05:00
);
cards.forEach((card) => {
2022-12-26 21:10:50 +01:00
// eslint-disable-next-line no-param-reassign
card.isSubscribed = isSubscribedByCardId[card.id] || false;
});
2019-08-31 04:07:25 +05:00
2022-06-24 11:59:04 +02:00
if (this.req.isSocket) {
sails.sockets.join(this.req, `board:${board.id}`); // TODO: only when subscription needed
}
2019-08-31 04:07:25 +05:00
return {
2019-08-31 04:07:25 +05:00
item: board,
included: {
users,
boardMemberships,
2019-08-31 04:07:25 +05:00
labels,
lists,
2019-08-31 04:07:25 +05:00
cards,
cardMemberships,
cardLabels,
tasks,
2020-04-21 05:04:34 +05:00
attachments,
projects: [project],
},
};
},
2019-08-31 04:07:25 +05:00
};