forked from nathydre21/nepa
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathNotificationService.ts
More file actions
83 lines (69 loc) · 2.63 KB
/
Copy pathNotificationService.ts
File metadata and controls
83 lines (69 loc) · 2.63 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
import { PrismaClient } from '@prisma/client';
const prisma = new PrismaClient();
export class NotificationService {
async sendBillCreated(userId: string, bill: any) {
await this.handleNotification(userId, 'BILL_CREATED', {
amount: bill.amount,
dueDate: bill.dueDate
});
}
async sendBillOverdue(userId: string, bill: any, lateFee: number) {
await this.handleNotification(userId, 'BILL_OVERDUE', {
amount: bill.amount,
lateFee: lateFee
});
}
private async handleNotification(userId: string, type: string, data: any) {
const user = await prisma.user.findUnique({
where: { id: userId },
include: { notificationPreference: true }
});
if (!user) return;
const prefs = user.notificationPreference || { email: true, sms: false, push: false };
const { subject, message } = this.getTemplate(type, user.name || 'Customer', data);
if (prefs.email && user.email) {
await this.sendEmail(user.email, subject, message);
await this.logNotification(userId, 'EMAIL', 'SENT', message);
}
if (prefs.sms && user.phoneNumber) {
await this.sendSMS(user.phoneNumber, message);
await this.logNotification(userId, 'SMS', 'SENT', message);
}
if (prefs.push) {
// Mock push token retrieval
const pushToken = "mock-device-token";
await this.sendPush(pushToken, subject, message);
await this.logNotification(userId, 'PUSH', 'SENT', message);
}
}
private getTemplate(type: string, userName: string, data: any) {
switch (type) {
case 'BILL_CREATED':
return {
subject: 'New Bill Generated',
message: `Hello ${userName}, a new bill of ${data.amount} has been generated. Due date: ${data.dueDate}.`
};
case 'BILL_OVERDUE':
return {
subject: 'Bill Overdue Notice',
message: `Hello ${userName}, your bill is overdue. A late fee of ${data.lateFee} has been applied.`
};
default:
return { subject: 'Notification', message: 'You have a new notification.' };
}
}
private async sendEmail(to: string, subject: string, body: string) {
console.log(`[SendGrid] Sending email to ${to}: ${subject}`);
}
private async sendSMS(to: string, body: string) {
console.log(`[Twilio] Sending SMS to ${to}: ${body}`);
}
private async sendPush(token: string, title: string, body: string) {
console.log(`[FCM] Sending Push to ${token}: ${title}`);
}
private async logNotification(userId: string, type: string, status: string, message: string) {
await prisma.notificationLog.create({
data: { userId, type, status, message }
});
}
}