|
| 1 | +import { |
| 2 | + Body, |
| 3 | + Controller, |
| 4 | + Delete, |
| 5 | + Get, |
| 6 | + NotFoundException, |
| 7 | + Param, |
| 8 | + ParseUUIDPipe, |
| 9 | + Post, |
| 10 | + Query, |
| 11 | + UseGuards, |
| 12 | +} from '@nestjs/common'; |
| 13 | +import { |
| 14 | + ApiBearerAuth, |
| 15 | + ApiOperation, |
| 16 | + ApiQuery, |
| 17 | + ApiResponse, |
| 18 | + ApiTags, |
| 19 | +} from '@nestjs/swagger'; |
| 20 | +import { JwtAuthGuard } from '../../auth/guards/jwt-auth.guard'; |
| 21 | +import { RolesGuard } from '../guards/roles.guard'; |
| 22 | +import { Roles } from '../decorators/roles.decorator'; |
| 23 | +import { Role } from '../enums/role.enum'; |
| 24 | +import { EventDlqService } from './event-dlq.service'; |
| 25 | +import { FailedEventStatus } from './entities/failed-event.entity'; |
| 26 | +import { ReplayEventsDto } from './dto/replay-events.dto'; |
| 27 | + |
| 28 | +@ApiTags('admin: event-dlq') |
| 29 | +@ApiBearerAuth() |
| 30 | +@Controller('admin/event-dlq') |
| 31 | +@UseGuards(JwtAuthGuard, RolesGuard) |
| 32 | +@Roles(Role.ADMIN) |
| 33 | +export class EventDlqController { |
| 34 | + constructor(private readonly dlqService: EventDlqService) {} |
| 35 | + |
| 36 | + @Get('stats') |
| 37 | + @ApiOperation({ summary: 'DLQ summary stats for monitoring dashboards' }) |
| 38 | + @ApiResponse({ status: 200, description: 'Counts by status & event name' }) |
| 39 | + async getStats() { |
| 40 | + return this.dlqService.getStats(); |
| 41 | + } |
| 42 | + |
| 43 | + @Get() |
| 44 | + @ApiOperation({ summary: 'List failed events' }) |
| 45 | + @ApiQuery({ name: 'status', enum: FailedEventStatus, required: false }) |
| 46 | + @ApiQuery({ name: 'eventName', required: false }) |
| 47 | + @ApiQuery({ name: 'limit', required: false, type: Number }) |
| 48 | + @ApiQuery({ name: 'offset', required: false, type: Number }) |
| 49 | + async list( |
| 50 | + @Query('status') status?: FailedEventStatus, |
| 51 | + @Query('eventName') eventName?: string, |
| 52 | + @Query('limit') limit?: string, |
| 53 | + @Query('offset') offset?: string, |
| 54 | + ) { |
| 55 | + return this.dlqService.list({ |
| 56 | + status, |
| 57 | + eventName, |
| 58 | + limit: limit ? parseInt(limit, 10) : undefined, |
| 59 | + offset: offset ? parseInt(offset, 10) : undefined, |
| 60 | + }); |
| 61 | + } |
| 62 | + |
| 63 | + @Get(':id') |
| 64 | + @ApiOperation({ summary: 'Get a failed event by id' }) |
| 65 | + async getOne(@Param('id', new ParseUUIDPipe()) id: string) { |
| 66 | + const record = await this.dlqService.getOne(id); |
| 67 | + if (!record) throw new NotFoundException('Failed event not found'); |
| 68 | + return record; |
| 69 | + } |
| 70 | + |
| 71 | + @Post(':id/replay') |
| 72 | + @ApiOperation({ summary: 'Replay a single failed event' }) |
| 73 | + async replayOne(@Param('id', new ParseUUIDPipe()) id: string) { |
| 74 | + const result = await this.dlqService.replay(id); |
| 75 | + if (!result.success && result.error === 'Failed event not found') { |
| 76 | + throw new NotFoundException(result.error); |
| 77 | + } |
| 78 | + return result; |
| 79 | + } |
| 80 | + |
| 81 | + @Post('replay') |
| 82 | + @ApiOperation({ summary: 'Replay multiple failed events' }) |
| 83 | + async replayMany(@Body() dto: ReplayEventsDto) { |
| 84 | + return this.dlqService.replayMany(dto.ids); |
| 85 | + } |
| 86 | + |
| 87 | + @Delete(':id') |
| 88 | + @ApiOperation({ summary: 'Discard a failed event without replaying' }) |
| 89 | + async discard(@Param('id', new ParseUUIDPipe()) id: string) { |
| 90 | + const ok = await this.dlqService.discard(id); |
| 91 | + if (!ok) throw new NotFoundException('Failed event not found'); |
| 92 | + return { discarded: true }; |
| 93 | + } |
| 94 | +} |
0 commit comments