forked from balahero03/eOrbitor_Pulse
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathnotify.ts
More file actions
58 lines (54 loc) · 1.3 KB
/
Copy pathnotify.ts
File metadata and controls
58 lines (54 loc) · 1.3 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
import { prisma } from '@/lib/prisma';
type NotifType =
| 'APPROVAL_REQUESTED'
| 'APPROVAL_APPROVED'
| 'APPROVAL_REJECTED'
| 'TASK_ASSIGNED'
| 'USER_INACTIVE'
| 'QUOTATION_APPROVED';
export async function createNotification(
userId: string,
type: NotifType,
title: string,
message: string,
relatedEntityType?: string,
relatedEntityId?: string,
) {
try {
await prisma.notification.create({
data: {
userId,
type: type as any,
title,
message,
relatedEntityType: relatedEntityType ?? null,
relatedEntityId: relatedEntityId ?? null,
isRead: false,
},
});
} catch (err) {
console.error('[notify] Failed to create notification:', err);
}
}
export async function notifyAdminsAndManagers(
type: NotifType,
title: string,
message: string,
relatedEntityType?: string,
relatedEntityId?: string,
excludeUserId?: string,
) {
const targets = await prisma.user.findMany({
where: {
role: { in: ['SUPER_ADMIN', 'ADMIN', 'BACKEND_TEAM'] },
isActive: true,
...(excludeUserId ? { id: { not: excludeUserId } } : {}),
},
select: { id: true },
});
await Promise.all(
targets.map((u) =>
createNotification(u.id, type, title, message, relatedEntityType, relatedEntityId),
),
);
}