-
Notifications
You must be signed in to change notification settings - Fork 2
Expand file tree
/
Copy pathbulkEvents.js
More file actions
94 lines (84 loc) · 2.59 KB
/
bulkEvents.js
File metadata and controls
94 lines (84 loc) · 2.59 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
const sendPersonalNotification = require('../../utils/personalNotifications').default;
const { UserInputError } = require('apollo-server-express');
const { ObjectId } = require('mongodb');
/**
* 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.allSettled(eventIds.map(eventId => sendPersonalNotification(assigneeData, {
type: 'assignee',
payload: {
assigneeId,
projectId,
whoAssignedId,
eventId,
},
})))
.then((results) => {
const failedResults = results.filter(result => result.status === 'rejected');
if (failedResults.length > 0) {
console.error('Failed to enqueue assignee notifications', failedResults);
}
})
.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,
};