2020-08-04 01:32:46 +05:00
|
|
|
const Errors = {
|
|
|
|
BOARD_NOT_FOUND: {
|
|
|
|
boardNotFound: 'Board not found',
|
|
|
|
},
|
|
|
|
};
|
|
|
|
|
|
|
|
module.exports = {
|
|
|
|
inputs: {
|
|
|
|
boardId: {
|
|
|
|
type: 'string',
|
|
|
|
regex: /^[0-9]+$/,
|
|
|
|
required: true,
|
|
|
|
},
|
|
|
|
beforeId: {
|
|
|
|
type: 'string',
|
|
|
|
regex: /^[0-9]+$/,
|
|
|
|
},
|
|
|
|
},
|
|
|
|
|
|
|
|
exits: {
|
|
|
|
boardNotFound: {
|
|
|
|
responseType: 'notFound',
|
|
|
|
},
|
|
|
|
},
|
|
|
|
|
2022-08-26 02:45:27 +02:00
|
|
|
async fn(inputs) {
|
2020-08-04 01:32:46 +05:00
|
|
|
const { currentUser } = this.req;
|
|
|
|
|
2021-06-24 01:05:22 +05:00
|
|
|
const { board } = await sails.helpers.boards
|
|
|
|
.getProjectPath(inputs.boardId)
|
2020-08-04 01:32:46 +05:00
|
|
|
.intercept('pathNotFound', () => Errors.BOARD_NOT_FOUND);
|
|
|
|
|
2021-06-24 01:05:22 +05:00
|
|
|
const isBoardMember = await sails.helpers.users.isBoardMember(currentUser.id, board.id);
|
2020-08-04 01:32:46 +05:00
|
|
|
|
2021-06-24 01:05:22 +05:00
|
|
|
if (!isBoardMember) {
|
2020-08-04 01:32:46 +05:00
|
|
|
throw Errors.BOARD_NOT_FOUND; // Forbidden
|
|
|
|
}
|
|
|
|
|
2021-06-24 01:05:22 +05:00
|
|
|
const cards = await sails.helpers.boards.getCards(board, inputs.beforeId);
|
|
|
|
const cardIds = sails.helpers.utils.mapRecords(cards);
|
2020-08-04 01:32:46 +05:00
|
|
|
|
2021-06-24 01:05:22 +05:00
|
|
|
const cardSubscriptions = await sails.helpers.cardSubscriptions.getMany({
|
|
|
|
cardId: cardIds,
|
|
|
|
userId: currentUser.id,
|
|
|
|
});
|
2020-08-04 01:32:46 +05:00
|
|
|
|
|
|
|
const isSubscribedByCardId = cardSubscriptions.reduce(
|
|
|
|
(result, cardSubscription) => ({
|
|
|
|
...result,
|
|
|
|
[cardSubscription.cardId]: true,
|
|
|
|
}),
|
|
|
|
{},
|
|
|
|
);
|
|
|
|
|
2021-06-24 01:05:22 +05:00
|
|
|
cards.forEach((card) => {
|
|
|
|
card.isSubscribed = isSubscribedByCardId[card.id] || false; // eslint-disable-line no-param-reassign
|
|
|
|
});
|
|
|
|
|
|
|
|
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);
|
2020-08-04 01:32:46 +05:00
|
|
|
|
2022-08-26 02:45:27 +02:00
|
|
|
return {
|
2020-08-04 01:32:46 +05:00
|
|
|
items: cards,
|
|
|
|
included: {
|
|
|
|
cardMemberships,
|
|
|
|
cardLabels,
|
|
|
|
tasks,
|
|
|
|
attachments,
|
|
|
|
},
|
2022-08-26 02:45:27 +02:00
|
|
|
};
|
2020-08-04 01:32:46 +05:00
|
|
|
},
|
|
|
|
};
|