|
| 1 | +import { |
| 2 | + Body, |
| 3 | + Controller, |
| 4 | + HttpCode, |
| 5 | + InternalServerErrorException, |
| 6 | + Logger, |
| 7 | + Post, |
| 8 | + UseGuards, |
| 9 | +} from '@nestjs/common'; |
| 10 | +import { ApiHeader, ApiOperation, ApiProperty, ApiResponse, ApiTags } from '@nestjs/swagger'; |
| 11 | +import { TaskStatus } from '@db'; |
| 12 | +import { IsBoolean, IsEnum, IsInt, IsString, Min } from 'class-validator'; |
| 13 | +import { InternalTokenGuard } from '../auth/internal-token.guard'; |
| 14 | +import { TaskNotifierService } from './task-notifier.service'; |
| 15 | + |
| 16 | +const TaskStatusValues = Object.values(TaskStatus); |
| 17 | + |
| 18 | +class NotifyAutomationFailuresDto { |
| 19 | + @ApiProperty({ description: 'Organization ID' }) |
| 20 | + @IsString() |
| 21 | + organizationId: string; |
| 22 | + |
| 23 | + @ApiProperty({ description: 'Task ID' }) |
| 24 | + @IsString() |
| 25 | + taskId: string; |
| 26 | + |
| 27 | + @ApiProperty({ description: 'Task title' }) |
| 28 | + @IsString() |
| 29 | + taskTitle: string; |
| 30 | + |
| 31 | + @ApiProperty({ description: 'Number of failed automations' }) |
| 32 | + @IsInt() |
| 33 | + @Min(1) |
| 34 | + failedCount: number; |
| 35 | + |
| 36 | + @ApiProperty({ description: 'Total number of automations' }) |
| 37 | + @IsInt() |
| 38 | + @Min(1) |
| 39 | + totalCount: number; |
| 40 | + |
| 41 | + @ApiProperty({ description: 'Whether task status was changed to failed' }) |
| 42 | + @IsBoolean() |
| 43 | + taskStatusChanged: boolean; |
| 44 | +} |
| 45 | + |
| 46 | +class NotifyStatusChangeDto { |
| 47 | + @ApiProperty({ description: 'Organization ID' }) |
| 48 | + @IsString() |
| 49 | + organizationId: string; |
| 50 | + |
| 51 | + @ApiProperty({ description: 'Task ID' }) |
| 52 | + @IsString() |
| 53 | + taskId: string; |
| 54 | + |
| 55 | + @ApiProperty({ description: 'Task title' }) |
| 56 | + @IsString() |
| 57 | + taskTitle: string; |
| 58 | + |
| 59 | + @ApiProperty({ description: 'Previous task status', enum: TaskStatusValues }) |
| 60 | + @IsEnum(TaskStatusValues) |
| 61 | + oldStatus: TaskStatus; |
| 62 | + |
| 63 | + @ApiProperty({ description: 'New task status', enum: TaskStatusValues }) |
| 64 | + @IsEnum(TaskStatusValues) |
| 65 | + newStatus: TaskStatus; |
| 66 | +} |
| 67 | + |
| 68 | +@ApiTags('Internal - Tasks') |
| 69 | +@Controller({ path: 'internal/tasks', version: '1' }) |
| 70 | +@UseGuards(InternalTokenGuard) |
| 71 | +@ApiHeader({ |
| 72 | + name: 'X-Internal-Token', |
| 73 | + description: 'Internal service token (required in production)', |
| 74 | + required: false, |
| 75 | +}) |
| 76 | +export class InternalTaskNotificationController { |
| 77 | + private readonly logger = new Logger(InternalTaskNotificationController.name); |
| 78 | + |
| 79 | + constructor(private readonly taskNotifierService: TaskNotifierService) {} |
| 80 | + |
| 81 | + @Post('notify-status-change') |
| 82 | + @HttpCode(200) |
| 83 | + @ApiOperation({ |
| 84 | + summary: |
| 85 | + 'Send task status change notifications (email + in-app) without a user actor (internal)', |
| 86 | + }) |
| 87 | + @ApiResponse({ status: 200, description: 'Notifications sent' }) |
| 88 | + @ApiResponse({ status: 500, description: 'Notification delivery failed' }) |
| 89 | + async notifyStatusChange(@Body() body: NotifyStatusChangeDto) { |
| 90 | + this.logger.log( |
| 91 | + `[notifyStatusChange] Received request for task ${body.taskId} (${body.oldStatus} -> ${body.newStatus})`, |
| 92 | + ); |
| 93 | + |
| 94 | + try { |
| 95 | + await this.taskNotifierService.notifyStatusChange({ |
| 96 | + organizationId: body.organizationId, |
| 97 | + taskId: body.taskId, |
| 98 | + taskTitle: body.taskTitle, |
| 99 | + oldStatus: body.oldStatus, |
| 100 | + newStatus: body.newStatus, |
| 101 | + }); |
| 102 | + |
| 103 | + return { success: true }; |
| 104 | + } catch (error) { |
| 105 | + this.logger.error( |
| 106 | + `[notifyStatusChange] Failed for task ${body.taskId}:`, |
| 107 | + error instanceof Error ? error.message : 'Unknown error', |
| 108 | + ); |
| 109 | + |
| 110 | + throw new InternalServerErrorException('Failed to send notifications'); |
| 111 | + } |
| 112 | + } |
| 113 | + |
| 114 | + @Post('notify-automation-failures') |
| 115 | + @HttpCode(200) |
| 116 | + @ApiOperation({ |
| 117 | + summary: |
| 118 | + 'Send automation failure notifications (email + in-app) when one or more automations fail (internal)', |
| 119 | + }) |
| 120 | + @ApiResponse({ status: 200, description: 'Notifications sent' }) |
| 121 | + @ApiResponse({ status: 500, description: 'Notification delivery failed' }) |
| 122 | + async notifyAutomationFailures(@Body() body: NotifyAutomationFailuresDto) { |
| 123 | + this.logger.log( |
| 124 | + `[notifyAutomationFailures] Received request for task ${body.taskId} (${body.failedCount}/${body.totalCount} failed, statusChanged=${body.taskStatusChanged})`, |
| 125 | + ); |
| 126 | + |
| 127 | + try { |
| 128 | + await this.taskNotifierService.notifyAutomationFailures({ |
| 129 | + organizationId: body.organizationId, |
| 130 | + taskId: body.taskId, |
| 131 | + taskTitle: body.taskTitle, |
| 132 | + failedCount: body.failedCount, |
| 133 | + totalCount: body.totalCount, |
| 134 | + taskStatusChanged: body.taskStatusChanged, |
| 135 | + }); |
| 136 | + |
| 137 | + return { success: true }; |
| 138 | + } catch (error) { |
| 139 | + this.logger.error( |
| 140 | + `[notifyAutomationFailures] Failed for task ${body.taskId}:`, |
| 141 | + error instanceof Error ? error.message : 'Unknown error', |
| 142 | + ); |
| 143 | + |
| 144 | + throw new InternalServerErrorException('Failed to send notifications'); |
| 145 | + } |
| 146 | + } |
| 147 | +} |
0 commit comments