/
index.js
74 lines (62 loc) · 1.77 KB
/
index.js
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
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',
},
},
async fn(inputs) {
const { currentUser } = this.req;
const { board } = await sails.helpers.boards
.getProjectPath(inputs.boardId)
.intercept('pathNotFound', () => Errors.BOARD_NOT_FOUND);
const isBoardMember = await sails.helpers.users.isBoardMember(currentUser.id, board.id);
if (!isBoardMember) {
throw Errors.BOARD_NOT_FOUND; // Forbidden
}
const cards = await sails.helpers.boards.getCards(board, inputs.beforeId);
const cardIds = sails.helpers.utils.mapRecords(cards);
const cardSubscriptions = await sails.helpers.cardSubscriptions.getMany({
cardId: cardIds,
userId: currentUser.id,
});
const isSubscribedByCardId = cardSubscriptions.reduce(
(result, cardSubscription) => ({
...result,
[cardSubscription.cardId]: true,
}),
{},
);
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);
return {
items: cards,
included: {
cardMemberships,
cardLabels,
tasks,
attachments,
},
};
},
};