-
Notifications
You must be signed in to change notification settings - Fork 2
Expand file tree
/
Copy pathnotify.ts
More file actions
63 lines (59 loc) · 1.55 KB
/
Copy pathnotify.ts
File metadata and controls
63 lines (59 loc) · 1.55 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
import { prisma } from '@/lib/prisma';
// A subset of Prisma's NotificationType — the values the app actually sends.
// Widen it as new notifications are added rather than casting at call sites,
// so the compiler keeps catching typos in enum names.
type NotifType =
| 'APPROVAL_REQUESTED'
| 'APPROVAL_APPROVED'
| 'APPROVAL_REJECTED'
| 'TASK_ASSIGNED'
| 'USER_INACTIVE'
| 'QUOTATION_APPROVED'
| 'LEAD_ASSIGNED'
| 'ORDER_CONFIRMED';
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),
),
);
}