-
Notifications
You must be signed in to change notification settings - Fork 2
Expand file tree
/
Copy pathbulkEvents.js
More file actions
116 lines (100 loc) · 3.29 KB
/
bulkEvents.js
File metadata and controls
116 lines (100 loc) · 3.29 KB
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
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
const sendPersonalNotification = require('../../utils/personalNotifications').default;
const { UserInputError } = require('apollo-server-express');
const { ObjectId } = require('mongodb');
const ASSIGNEE_NOTIFICATIONS_CHUNK_SIZE = 25;
/**
* Enqueue assignee notifications in background (do not block resolver response)
*
* @param {object} args - notification args
* @param {object} args.assigneeData - assigned user data
* @param {string[]} args.eventIds - original event ids
* @param {string} args.projectId - project id
* @param {string} args.assigneeId - assignee id
* @param {string} args.whoAssignedId - user id who performed assignment
* @returns {void}
*/
function fireAndForgetAssigneeNotifications({
assigneeData,
eventIds,
projectId,
assigneeId,
whoAssignedId,
}) {
if (!assigneeData) {
console.error('Failed to enqueue assignee notifications: assignee data is empty');
return;
}
Promise.resolve()
.then(async () => {
const failedResults = [];
for (let i = 0; i < eventIds.length; i += ASSIGNEE_NOTIFICATIONS_CHUNK_SIZE) {
const chunk = eventIds.slice(i, i + ASSIGNEE_NOTIFICATIONS_CHUNK_SIZE);
const results = await Promise.allSettled(chunk.map(eventId => sendPersonalNotification(assigneeData, {
type: 'assignee',
payload: {
assigneeId,
projectId,
whoAssignedId,
eventId,
},
})));
failedResults.push(...results.filter(result => result.status === 'rejected'));
}
if (failedResults.length > 0) {
const failedMessages = failedResults.map((result) => {
const reason = result && result.reason;
if (reason && typeof reason.message === 'string') {
return reason.message;
}
return String(reason || 'Unknown error');
});
console.error('Failed to enqueue assignee notifications', {
failedCount: failedResults.length,
errors: failedMessages,
});
}
})
.catch((error) => {
console.error('Failed to enqueue assignee notifications', error);
});
}
/**
* Validate and normalize bulk event ids from resolver input.
*
* @param {string[]} eventIds - raw event ids from GraphQL input
* @returns {{ validEventIds: string[], invalidEventIds: string[] }}
*/
function parseBulkEventIds(eventIds) {
if (!eventIds || !eventIds.length) {
throw new UserInputError('eventIds must contain at least one id');
}
const uniqueEventIds = [ ...new Set(eventIds.map(id => String(id))) ];
const invalidEventIds = [];
const validEventIds = [];
uniqueEventIds.forEach((id) => {
if (ObjectId.isValid(id)) {
validEventIds.push(id);
} else {
invalidEventIds.push(id);
}
});
return {
validEventIds,
invalidEventIds,
};
}
/**
* Merge failed ids returned by factory with invalid ids from resolver validation.
*
* @param {{ failedEventIds?: string[] }} result - factory response
* @param {string[]} invalidEventIds - invalid ids detected on resolver level
* @returns {string[]}
*/
function mergeFailedEventIds(result, invalidEventIds) {
return [ ...new Set([...(result.failedEventIds || []), ...invalidEventIds]) ];
}
module.exports = {
fireAndForgetAssigneeNotifications,
parseBulkEventIds,
mergeFailedEventIds,
};