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-11-05 18:01:42 +05:00
|
|
|
},
|
2019-08-31 04:07:25 +05:00
|
|
|
};
|
|
|
|
|
|
|
|
module.exports = {
|
|
|
|
inputs: {
|
|
|
|
id: {
|
2019-10-10 02:51:54 +05:00
|
|
|
type: 'string',
|
|
|
|
regex: /^[0-9]+$/,
|
2019-11-05 18:01:42 +05:00
|
|
|
required: true,
|
|
|
|
},
|
2019-08-31 04:07:25 +05:00
|
|
|
},
|
|
|
|
|
|
|
|
exits: {
|
2020-04-03 00:35:25 +05:00
|
|
|
boardNotFound: {
|
2019-11-05 18:01:42 +05:00
|
|
|
responseType: 'notFound',
|
|
|
|
},
|
2019-08-31 04:07:25 +05:00
|
|
|
},
|
|
|
|
|
2019-11-05 18:01:42 +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,
|
2019-11-05 18:01:42 +05:00
|
|
|
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,
|
2019-11-05 18:01:42 +05:00
|
|
|
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,
|
2019-11-05 18:01:42 +05:00
|
|
|
[cardSubscription.cardId]: true,
|
2019-08-31 04:07:25 +05:00
|
|
|
}),
|
2019-11-05 18:01:42 +05:00
|
|
|
{},
|
2019-08-31 04:07:25 +05:00
|
|
|
);
|
|
|
|
|
2020-04-03 00:35:25 +05:00
|
|
|
cards.map((card) => ({
|
2020-02-03 18:42:31 +05:00
|
|
|
...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,
|
2019-11-05 18:01:42 +05:00
|
|
|
tasks,
|
2020-04-21 05:04:34 +05:00
|
|
|
attachments,
|
2019-11-05 18:01:42 +05:00
|
|
|
},
|
2019-08-31 04:07:25 +05:00
|
|
|
});
|
2019-11-05 18:01:42 +05:00
|
|
|
},
|
2019-08-31 04:07:25 +05:00
|
|
|
};
|