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

89 lines
2.1 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, exits) {
2019-08-31 04:07:25 +05:00
// TODO: allow over HTTP without subscription
if (!this.req.isSocket) {
return this.res.badRequest();
}
const { currentUser } = this.req;
const { board, project } = await sails.helpers
.getBoardToProjectPath(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 isUserMemberForProject = await sails.helpers.isUserMemberForProject(
project.id,
currentUser.id,
2019-08-31 04:07:25 +05:00
);
if (!isUserMemberForProject) {
throw Errors.BOARD_NOT_FOUND; // Forbidden
}
const lists = await sails.helpers.getListsForBoard(board.id);
const labels = await sails.helpers.getLabelsForBoard(board.id);
const cards = await sails.helpers.getCardsForBoard(board.id);
const cardIds = sails.helpers.mapRecords(cards);
const cardSubscriptions = await sails.helpers.getSubscriptionsByUserForCard(
cardIds,
currentUser.id,
2019-08-31 04:07:25 +05:00
);
const cardMemberships = await sails.helpers.getMembershipsForCard(cardIds);
const cardLabels = await sails.helpers.getCardLabelsForCard(cardIds);
const tasks = await sails.helpers.getTasksForCard(cardIds);
2020-04-21 05:04:34 +05:00
const attachments = await sails.helpers.getAttachmentsForCard(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
);
2020-04-03 00:35:25 +05:00
cards.map((card) => ({
...card,
isSubscribed: isSubscribedByCardId[card.id] || false,
}));
2019-08-31 04:07:25 +05:00
sails.sockets.join(this.req, `board:${board.id}`); // TODO: only when subscription needed
return exits.success({
item: board,
included: {
lists,
labels,
cards,
cardMemberships,
cardLabels,
tasks,
2020-04-21 05:04:34 +05:00
attachments,
},
2019-08-31 04:07:25 +05:00
});
},
2019-08-31 04:07:25 +05:00
};