File size: 3,452 Bytes
f5957a1
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
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
import {
  Controller,
  Get,
  Patch,
  Param,
  Query,
  Body,
  UseGuards,
  ParseIntPipe,
  Request,
} from '@nestjs/common';
import {
  ApiTags,
  ApiBearerAuth,
  ApiOperation,
  ApiResponse,
  ApiParam,
} from '@nestjs/swagger';
import { JwtAuthGuard } from '../../auth/guards/jwt-auth.guard';
import { RolesGuard } from '../../auth/guards/roles.guard';
import { Roles } from '../../../common/decorators/roles.decorator';
import { RoleName } from '../../auth/entities/role.entity';
import { ErrorsService } from '../services/errors.service';
import { ErrorQueryDto } from '../dto/error-query.dto';
import { UpdateErrorDto } from '../dto/update-error.dto';

@ApiTags('๐Ÿ› System Errors')
@ApiBearerAuth('JWT-auth')
@Controller('api/errors')
@UseGuards(JwtAuthGuard, RolesGuard)
export class ErrorsController {
  constructor(private readonly errorsService: ErrorsService) {}

  @Get()
  @Roles(RoleName.ADMIN, RoleName.IT_ADMIN)
  @ApiOperation({
    summary: 'Get system errors (paginated, filterable)',
    description:
      'Retrieves a paginated list of system errors with optional filters for type, severity, and resolved status. ' +
      'Access roles: ADMIN, IT_ADMIN. Uses table: `system_errors`.',
  })
  @ApiResponse({ status: 200, description: 'Paginated system errors returned (data + meta)' })
  @ApiResponse({ status: 401, description: 'Unauthorized' })
  @ApiResponse({ status: 403, description: 'Forbidden โ€“ insufficient role' })
  async getErrors(@Query() query: ErrorQueryDto) {
    return this.errorsService.getErrors(query);
  }

  @Get('stats')
  @Roles(RoleName.ADMIN, RoleName.IT_ADMIN)
  @ApiOperation({
    summary: 'Get error statistics',
    description:
      'Returns error counts grouped by type and severity, plus total/unresolved counts. ' +
      'Access roles: ADMIN, IT_ADMIN. Uses table: `system_errors`.',
  })
  @ApiResponse({ status: 200, description: 'Error statistics returned' })
  @ApiResponse({ status: 401, description: 'Unauthorized' })
  @ApiResponse({ status: 403, description: 'Forbidden โ€“ insufficient role' })
  async getErrorStats() {
    return this.errorsService.getErrorStats();
  }

  @Get(':id')
  @Roles(RoleName.ADMIN, RoleName.IT_ADMIN)
  @ApiOperation({
    summary: 'Get error details by ID',
    description:
      'Returns full details of a specific system error including resolver info. ' +
      'Access roles: ADMIN, IT_ADMIN. Uses table: `system_errors`.',
  })
  @ApiParam({ name: 'id', type: Number, description: 'System error ID' })
  @ApiResponse({ status: 200, description: 'Error details returned' })
  @ApiResponse({ status: 404, description: 'Error not found' })
  async getErrorDetails(@Param('id', ParseIntPipe) id: number) {
    return this.errorsService.getErrorDetails(id);
  }

  @Patch(':id')
  @Roles(RoleName.IT_ADMIN)
  @ApiOperation({
    summary: 'Update error status',
    description:
      'Updates the status of a system error (investigating, resolved, ignored). ' +
      'Access roles: IT_ADMIN. Uses table: `system_errors`.',
  })
  @ApiParam({ name: 'id', type: Number, description: 'System error ID' })
  @ApiResponse({ status: 200, description: 'Error status updated' })
  @ApiResponse({ status: 404, description: 'Error not found' })
  async updateErrorStatus(
    @Param('id', ParseIntPipe) id: number,
    @Body() dto: UpdateErrorDto,
    @Request() req,
  ) {
    return this.errorsService.updateErrorStatus(id, dto, req.user.sub);
  }
}