添加新的API端点用于更新设备推送令牌绑定的用户ID,包括: - 新增UpdateTokenUserIdDto和UpdateTokenUserIdResponseDto - 在控制器中添加updateTokenUserId端点 - 在服务层实现updateTokenUserId方法 - 在push-token服务中添加底层更新逻辑 - 优化推送测试服务,仅在主进程中执行
120 lines
5.5 KiB
TypeScript
120 lines
5.5 KiB
TypeScript
import { Controller, Post, Put, Delete, Body, Param, Get, Query, UseGuards } from '@nestjs/common';
|
|
import { ApiTags, ApiOperation, ApiResponse, ApiParam, ApiQuery } from '@nestjs/swagger';
|
|
import { PushNotificationsService } from './push-notifications.service';
|
|
import { RegisterDeviceTokenDto } from './dto/register-device-token.dto';
|
|
import { UpdateDeviceTokenDto } from './dto/update-device-token.dto';
|
|
import { UpdateTokenUserIdDto } from './dto/update-token-user-id.dto';
|
|
import { SendPushNotificationDto } from './dto/send-push-notification.dto';
|
|
import { SendPushByTemplateDto } from './dto/send-push-by-template.dto';
|
|
import { SendPushToDevicesDto } from './dto/send-push-to-devices.dto';
|
|
import { PushResponseDto, BatchPushResponseDto, RegisterTokenResponseDto, UpdateTokenResponseDto, UnregisterTokenResponseDto, UpdateTokenUserIdResponseDto } from './dto/push-response.dto';
|
|
import { DevicePushResponseDto, BatchDevicePushResponseDto } from './dto/device-push-response.dto';
|
|
import { CurrentUser } from '../common/decorators/current-user.decorator';
|
|
import { AccessTokenPayload } from '../users/services/apple-auth.service';
|
|
import { JwtAuthGuard } from '../common/guards/jwt-auth.guard';
|
|
import { Public } from '../common/decorators/public.decorator';
|
|
|
|
@ApiTags('推送通知')
|
|
@Controller('push-notifications')
|
|
@UseGuards(JwtAuthGuard)
|
|
export class PushNotificationsController {
|
|
constructor(private readonly pushNotificationsService: PushNotificationsService) { }
|
|
|
|
@Post('register-token')
|
|
@ApiOperation({ summary: '注册设备推送令牌' })
|
|
@Public()
|
|
@ApiResponse({ status: 200, description: '注册成功', type: RegisterTokenResponseDto })
|
|
async registerToken(
|
|
@CurrentUser() user: AccessTokenPayload,
|
|
@Body() registerTokenDto: RegisterDeviceTokenDto,
|
|
): Promise<RegisterTokenResponseDto> {
|
|
return this.pushNotificationsService.registerToken(registerTokenDto, user?.sub || '');
|
|
}
|
|
|
|
@Put('update-token')
|
|
@Public()
|
|
|
|
@ApiOperation({ summary: '更新设备推送令牌' })
|
|
@ApiResponse({ status: 200, description: '更新成功', type: UpdateTokenResponseDto })
|
|
async updateToken(
|
|
@CurrentUser() user: AccessTokenPayload,
|
|
@Body() updateTokenDto: UpdateDeviceTokenDto,
|
|
): Promise<UpdateTokenResponseDto> {
|
|
return this.pushNotificationsService.updateToken(user?.sub || '', updateTokenDto);
|
|
}
|
|
|
|
@Put('update-token-user-id')
|
|
@ApiOperation({ summary: '更新令牌绑定的用户ID' })
|
|
@ApiResponse({ status: 200, description: '更新成功', type: UpdateTokenUserIdResponseDto })
|
|
async updateTokenUserId(
|
|
@CurrentUser() user: AccessTokenPayload,
|
|
@Body() updateTokenUserIdDto: UpdateTokenUserIdDto,
|
|
): Promise<UpdateTokenUserIdResponseDto> {
|
|
return this.pushNotificationsService.updateTokenUserId(user.sub, updateTokenUserIdDto.deviceToken);
|
|
}
|
|
|
|
@Delete('unregister-token')
|
|
@Public()
|
|
@ApiOperation({ summary: '注销设备推送令牌' })
|
|
@ApiResponse({ status: 200, description: '注销成功', type: UnregisterTokenResponseDto })
|
|
async unregisterToken(
|
|
@CurrentUser() user: AccessTokenPayload,
|
|
@Body() body: { deviceToken: string },
|
|
): Promise<UnregisterTokenResponseDto> {
|
|
return this.pushNotificationsService.unregisterToken(user?.sub || '', body.deviceToken);
|
|
}
|
|
|
|
@Post('send')
|
|
@ApiOperation({ summary: '发送推送通知' })
|
|
@ApiResponse({ status: 200, description: '发送成功', type: PushResponseDto })
|
|
async sendNotification(
|
|
@Body() sendNotificationDto: SendPushNotificationDto,
|
|
): Promise<PushResponseDto> {
|
|
return this.pushNotificationsService.sendNotification(sendNotificationDto);
|
|
}
|
|
|
|
@Post('send-by-template')
|
|
@ApiOperation({ summary: '使用模板发送推送' })
|
|
@ApiResponse({ status: 200, description: '发送成功', type: PushResponseDto })
|
|
async sendNotificationByTemplate(
|
|
@Body() sendByTemplateDto: SendPushByTemplateDto,
|
|
): Promise<PushResponseDto> {
|
|
return this.pushNotificationsService.sendNotificationByTemplate(sendByTemplateDto);
|
|
}
|
|
|
|
@Post('send-batch')
|
|
@ApiOperation({ summary: '批量发送推送' })
|
|
@ApiResponse({ status: 200, description: '发送成功', type: BatchPushResponseDto })
|
|
async sendBatchNotifications(
|
|
@Body() sendBatchDto: SendPushNotificationDto,
|
|
): Promise<BatchPushResponseDto> {
|
|
return this.pushNotificationsService.sendBatchNotifications(sendBatchDto);
|
|
}
|
|
|
|
@Post('send-silent')
|
|
@ApiOperation({ summary: '发送静默推送' })
|
|
@ApiResponse({ status: 200, description: '发送成功', type: PushResponseDto })
|
|
async sendSilentNotification(
|
|
@Body() body: { userId: string; payload: any },
|
|
): Promise<PushResponseDto> {
|
|
return this.pushNotificationsService.sendSilentNotification(body.userId, body.payload);
|
|
}
|
|
|
|
@Post('send-to-devices')
|
|
@ApiOperation({ summary: '向指定设备发送推送通知' })
|
|
@ApiResponse({ status: 200, description: '发送成功', type: DevicePushResponseDto })
|
|
async sendNotificationToDevices(
|
|
@Body() sendToDevicesDto: SendPushToDevicesDto,
|
|
): Promise<DevicePushResponseDto> {
|
|
return this.pushNotificationsService.sendNotificationToDevices(sendToDevicesDto);
|
|
}
|
|
|
|
@Post('send-batch-to-devices')
|
|
@ApiOperation({ summary: '批量向指定设备发送推送通知' })
|
|
@ApiResponse({ status: 200, description: '发送成功', type: BatchDevicePushResponseDto })
|
|
async sendBatchNotificationToDevices(
|
|
@Body() sendBatchToDevicesDto: SendPushToDevicesDto,
|
|
): Promise<BatchDevicePushResponseDto> {
|
|
return this.pushNotificationsService.sendBatchNotificationToDevices(sendBatchToDevicesDto);
|
|
}
|
|
} |