forked from StellarSplit/StellarSplit
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathpush-notifications.controller.ts
More file actions
69 lines (60 loc) · 2.44 KB
/
Copy pathpush-notifications.controller.ts
File metadata and controls
69 lines (60 loc) · 2.44 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
import { Controller, Post, Body, Delete, Param, Get, Put, Req, Logger, UseGuards } from '@nestjs/common';
import { PushNotificationsService } from './push-notifications.service';
import { RegisterDeviceDto } from './dto/register-device.dto';
import { UpdatePreferencesDto } from './dto/update-preferences.dto';
import { NotificationEventType } from './entities/notification-preference.entity';
// Assuming there's an AuthGuard available globally or I should add it.
// Given the other controller example, there's no explicit @UseGuards on the controller class,
// maybe it's global or I missed it. But req.user is accessed.
@Controller('push')
export class PushNotificationsController {
private readonly logger = new Logger(PushNotificationsController.name);
constructor(private readonly pushService: PushNotificationsService) {}
@Post('register-device')
async registerDevice(@Body() dto: RegisterDeviceDto, @Req() req: any) {
// If authenticated, use the wallet address from the token
if (req.user && req.user.walletAddress) {
dto.userId = req.user.walletAddress;
}
return this.pushService.registerDevice(dto);
}
@Delete('unregister-device/:deviceId')
async unregisterDevice(@Param('deviceId') deviceId: string) {
return this.pushService.unregisterDevice(deviceId);
}
@Get('devices')
async getDevices(@Req() req: any) {
const userId = req.user?.walletAddress || req.query?.userId;
if (!userId) {
// Return 400 or empty list
return [];
}
return this.pushService.getDevices(userId);
}
@Put('preferences')
async updatePreferences(@Body() dto: UpdatePreferencesDto, @Req() req: any) {
if (req.user && req.user.walletAddress) {
dto.userId = req.user.walletAddress;
}
return this.pushService.updatePreferences(dto);
}
@Get('preferences')
async getPreferences(@Req() req: any) {
const userId = req.user?.walletAddress || req.query.userId;
if (!userId) {
return [];
}
return this.pushService.getPreferences(userId);
}
@Post('test')
async sendTestNotification(@Body() body: { userId: string, title?: string, message?: string }) {
const { userId, title, message } = body;
await this.pushService.sendNotification(
userId,
NotificationEventType.SPLIT_CREATED, // Just a test event type
title || 'Test Notification',
message || 'This is a test notification from StellarSplit'
);
return { success: true };
}
}