forked from StellarSplit/StellarSplit
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathpush-notifications.service.ts
More file actions
178 lines (150 loc) · 5.63 KB
/
Copy pathpush-notifications.service.ts
File metadata and controls
178 lines (150 loc) · 5.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
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
import { Injectable, Logger } from '@nestjs/common';
import { InjectRepository } from '@nestjs/typeorm';
import { Repository, LessThan } from 'typeorm';
import { Cron, CronExpression } from '@nestjs/schedule';
import { InjectQueue } from '@nestjs/bull';
import { Queue } from 'bull';
import { DeviceRegistration } from './entities/device-registration.entity';
import { NotificationPreference, NotificationEventType } from './entities/notification-preference.entity';
import { RegisterDeviceDto } from './dto/register-device.dto';
import { UpdatePreferencesDto } from './dto/update-preferences.dto';
@Injectable()
export class PushNotificationsService {
private readonly logger = new Logger(PushNotificationsService.name);
constructor(
@InjectRepository(DeviceRegistration)
private deviceRepo: Repository<DeviceRegistration>,
@InjectRepository(NotificationPreference)
private prefRepo: Repository<NotificationPreference>,
@InjectQueue('push_queue') private pushQueue: Queue,
) {}
async registerDevice(dto: RegisterDeviceDto): Promise<DeviceRegistration> {
let device = await this.deviceRepo.findOne({
where: { deviceToken: dto.deviceToken },
});
if (device) {
device.userId = dto.userId;
device.lastSeenAt = new Date();
device.isActive = true;
if (dto.deviceName) device.deviceName = dto.deviceName;
} else {
device = this.deviceRepo.create({
userId: dto.userId,
deviceToken: dto.deviceToken,
platform: dto.platform,
deviceName: dto.deviceName,
});
}
return this.deviceRepo.save(device);
}
async unregisterDevice(deviceId: string, userId: string): Promise<void> {
const device = await this.deviceRepo.findOne({ where: { id: deviceId } });
if (!device) {
throw new Error('Device not found');
}
if (device.userId !== userId) {
throw new Error('Device does not belong to user');
}
await this.deviceRepo.delete(deviceId);
}
async getDevices(userId: string): Promise<DeviceRegistration[]> {
return this.deviceRepo.find({ where: { userId, isActive: true } });
}
async updatePreferences(dto: UpdatePreferencesDto): Promise<NotificationPreference[]> {
const results: NotificationPreference[] = [];
for (const prefDto of dto.preferences) {
let pref = await this.prefRepo.findOne({
where: { userId: dto.userId, eventType: prefDto.eventType },
});
if (!pref) {
pref = this.prefRepo.create({
userId: dto.userId,
eventType: prefDto.eventType,
});
}
if (prefDto.pushEnabled !== undefined) pref.pushEnabled = prefDto.pushEnabled;
if (prefDto.emailEnabled !== undefined) pref.emailEnabled = prefDto.emailEnabled;
if (prefDto.quietHoursStart !== undefined) pref.quietHoursStart = prefDto.quietHoursStart;
if (prefDto.quietHoursEnd !== undefined) pref.quietHoursEnd = prefDto.quietHoursEnd;
if (prefDto.timezone !== undefined) pref.timezone = prefDto.timezone;
results.push(await this.prefRepo.save(pref));
}
return results;
}
async getPreferences(userId: string): Promise<NotificationPreference[]> {
return this.prefRepo.find({ where: { userId } });
}
async sendNotification(
userId: string,
eventType: NotificationEventType,
title: string,
body: string,
data?: Record<string, string>,
): Promise<void> {
// Check preferences before queuing
const pref = await this.prefRepo.findOne({
where: { userId, eventType },
});
if (pref && !pref.pushEnabled) {
this.logger.log(`Push disabled for user ${userId} event ${eventType}`);
return;
}
// Check quiet hours before queuing
if (pref && this.isQuietHours(pref)) {
this.logger.log(`Quiet hours active for user ${userId}`);
return;
}
await this.pushQueue.add('sendPush', {
userId,
eventType,
title,
body,
data,
});
this.logger.log(`Queued push notification for user ${userId} event ${eventType}`);
}
async sendTestNotification(
userId: string,
title: string,
body: string,
data?: Record<string, string>,
): Promise<void> {
// Test notifications bypass preference and quiet hours checks
await this.pushQueue.add('sendPush', {
userId,
eventType: NotificationEventType.SPLIT_CREATED, // Use a default event type
title,
body,
data,
});
this.logger.log(`Queued test push notification for user ${userId}`);
}
private isQuietHours(pref: NotificationPreference): boolean {
if (!pref.quietHoursStart || !pref.quietHoursEnd) return false;
const now = new Date();
const userTime = pref.timezone
? new Date(now.toLocaleString('en-US', { timeZone: pref.timezone }))
: now;
const currentHour = userTime.getHours();
const currentMinute = userTime.getMinutes();
const currentTimeVal = currentHour * 60 + currentMinute;
const [startH, startM] = pref.quietHoursStart.split(':').map(Number);
const startVal = startH * 60 + startM;
const [endH, endM] = pref.quietHoursEnd.split(':').map(Number);
const endVal = endH * 60 + endM;
if (startVal < endVal) {
return currentTimeVal >= startVal && currentTimeVal < endVal;
} else {
return currentTimeVal >= startVal || currentTimeVal < endVal;
}
}
@Cron(CronExpression.EVERY_DAY_AT_MIDNIGHT)
async cleanupStaleTokens() {
const sixMonthsAgo = new Date();
sixMonthsAgo.setMonth(sixMonthsAgo.getMonth() - 6);
const result = await this.deviceRepo.delete({
lastSeenAt: LessThan(sixMonthsAgo),
});
this.logger.log(`Cleaned up ${result.affected} stale device tokens`);
}
}