forked from nathydre21/nepa
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathRealTimeService.ts
More file actions
80 lines (71 loc) · 2.52 KB
/
RealTimeService.ts
File metadata and controls
80 lines (71 loc) · 2.52 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
import { SocketServer } from '../websocket/SocketServer';
export enum NotificationType {
PAYMENT_SUCCESS = 'PAYMENT_SUCCESS',
PAYMENT_FAILED = 'PAYMENT_FAILED',
PAYMENT_PENDING = 'PAYMENT_PENDING',
BILL_GENERATED = 'BILL_GENERATED',
SYSTEM_ALERT = 'SYSTEM_ALERT'
}
export interface NotificationPayload {
type: NotificationType;
title: string;
message: string;
data?: any;
timestamp: string;
}
export class RealTimeService {
/**
* Send a real-time update to a specific user
*/
static sendUserUpdate(userId: string, type: NotificationType, data: any) {
try {
const io = SocketServer.getIO();
const payload: NotificationPayload = {
type,
title: this.getTitleForType(type),
message: this.getMessageForType(type, data),
data,
timestamp: new Date().toISOString()
};
io.to(`user_${userId}`).emit('notification', payload);
// Also emit specific event for granular listening
io.to(`user_${userId}`).emit(type.toLowerCase(), data);
console.log(`📡 Sent ${type} to user ${userId}`);
} catch (error) {
console.error(`Failed to send real-time update to user ${userId}:`, error);
}
}
/**
* Broadcast update to all connected users (Admin use case)
*/
static broadcast(type: NotificationType, data: any) {
try {
const io = SocketServer.getIO();
io.emit('broadcast', { type, data, timestamp: new Date().toISOString() });
} catch (error) {
console.error('Failed to broadcast message:', error);
}
}
private static getTitleForType(type: NotificationType): string {
switch (type) {
case NotificationType.PAYMENT_SUCCESS: return 'Payment Successful';
case NotificationType.PAYMENT_FAILED: return 'Payment Failed';
case NotificationType.PAYMENT_PENDING: return 'Payment Processing';
case NotificationType.BILL_GENERATED: return 'New Bill Available';
case NotificationType.SYSTEM_ALERT: return 'System Alert';
default: return 'Notification';
}
}
private static getMessageForType(type: NotificationType, data: any): string {
switch (type) {
case NotificationType.PAYMENT_SUCCESS:
return `Your payment of ₦${data.amount} was successful.`;
case NotificationType.PAYMENT_FAILED:
return `Payment failed: ${data.reason || 'Unknown error'}`;
case NotificationType.BILL_GENERATED:
return `A new bill for ${data.utilityName} is ready.`;
default:
return 'You have a new notification.';
}
}
}