-
Notifications
You must be signed in to change notification settings - Fork 2
Expand file tree
/
Copy pathbulkEventUtils.ts
More file actions
60 lines (53 loc) · 1.59 KB
/
bulkEventUtils.ts
File metadata and controls
60 lines (53 loc) · 1.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
import { UserInputError } from 'apollo-server-express';
import { ObjectId } from 'mongodb';
import sendPersonalNotification from '../../utils/personalNotifications';
import type { UserDBScheme } from '@hawk.so/types';
import { SenderWorkerTaskType } from '../../types/userNotifications/task-type';
/**
* Validate and normalize bulk event ids from resolver input.
*
* @param {string[]} eventIds - raw event ids from GraphQL input
* @returns {string[]} unique valid event ids
*/
export function parseBulkEventIds(eventIds: string[]): string[] {
if (!eventIds || !eventIds.length) {
throw new UserInputError('eventIds must contain at least one id');
}
const uniqueEventIds = [ ...new Set(eventIds.map(id => String(id))) ];
if (!uniqueEventIds.every((id) => ObjectId.isValid(id))) {
throw new UserInputError('eventIds must contain only valid ids');
}
return uniqueEventIds;
}
type AssigneeNotificationParams = {
assigneeData: UserDBScheme | null;
assigneeId: string;
projectId: string;
whoAssignedId: string;
eventId: string;
};
/**
* Enqueue one assignee notification without blocking resolver response.
*/
export function enqueueAssigneeNotification({
assigneeData,
assigneeId,
projectId,
whoAssignedId,
eventId,
}: AssigneeNotificationParams): void {
if (!assigneeData) {
return;
}
sendPersonalNotification(assigneeData, {
type: SenderWorkerTaskType.Assignee,
payload: {
assigneeId,
projectId,
whoAssignedId,
eventId,
},
}).catch((error: unknown) => {
console.error('Failed to enqueue assignee notification', error);
});
}