# All files / modules/audit audit.controller.ts

0% Statements 0/28
0% Branches 0/6
0% Functions 0/9
0% Lines 0/26

Press n or j to go to the next uncovered block, b, p or k for the previous block.

    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
    99
    100
    101
    102
    103
    104
    105
    106
    107
    108
    109
    110
    111
    112
    113
    114
    115
    116
    117
    118
    119
    120
    121
    122
    123
    124
    125
    126
    127
    128
    129
    130
    131
    132
    133
    134
    135
    136
    137
    138
    139
    140
    141
    142
    143
    144
    145
    146

     import {
      Controller,
      Get,
      Post,
      Body,
      Param,
      Query,
      UseGuards,
      Req,
    } from '@nestjs/common';
    import {
      ApiTags,
      ApiOperation,
      ApiResponse,
      ApiBearerAuth,
      ApiParam,
    } from '@nestjs/swagger';
    import { AuditService } from './services/audit.service';
    import { QueryAuditLogsDto } from './dto/query-audit.dto';
    import { QueryActivityLogsDto } from './dto/query-activity.dto';
    import { CreateActivityLogDto } from './dto/create-activity.dto';
    import { JwtAuthGuard } from '../auth/guards';
    import { CurrentUser } from '../auth/decorators';
    import { RequestUser } from '../auth/strategies/jwt.strategy';

    @ApiTags('Audit')
    @ApiBearerAuth()
    @UseGuards(JwtAuthGuard)
    @Controller('audit')
    export class AuditController {
      constructor(private readonly auditService: AuditService) {}

      // ==================== AUDIT LOGS ====================

      @Get('logs')
      @ApiOperation({ summary: 'Query audit logs with filters' })
      @ApiResponse({ status: 200, description: 'Paginated audit logs' })
      async queryAuditLogs(
        @CurrentUser() user: RequestUser,
        @Query() query: QueryAuditLogsDto,
      ) {
        return this.auditService.queryAuditLogs(user.tenant_id, query);
      }

      @Get('logs/:id')
      @ApiOperation({ summary: 'Get audit log by ID' })
      @ApiParam({ name: 'id', description: 'Audit log ID' })
      @ApiResponse({ status: 200, description: 'Audit log details' })
      @ApiResponse({ status: 404, description: 'Audit log not found' })
      async getAuditLogById(
        @CurrentUser() user: RequestUser,
        @Param('id') id: string,
      ) {
        return this.auditService.getAuditLogById(user.tenant_id, id);
      }

      @Get('entity/:entityType/:entityId')
      @ApiOperation({ summary: 'Get audit history for a specific entity' })
      @ApiParam({ name: 'entityType', description: 'Entity type (e.g., user, product)' })
      @ApiParam({ name: 'entityId', description: 'Entity ID' })
      @ApiResponse({ status: 200, description: 'Entity audit history' })
      async getEntityAuditHistory(
        @CurrentUser() user: RequestUser,
        @Param('entityType') entityType: string,
        @Param('entityId') entityId: string,
      ) {
        return this.auditService.getEntityAuditHistory(
          user.tenant_id,
          entityType,
          entityId,
        );
      }

      @Get('stats')
      @ApiOperation({ summary: 'Get audit statistics for dashboard' })
      @ApiResponse({ status: 200, description: 'Audit statistics' })
      async getAuditStats(
        @CurrentUser() user: RequestUser,
        @Query('days') days?: number,
      ) {
        return this.auditService.getAuditStats(user.tenant_id, days || 7);
      }

      // ==================== ACTIVITY LOGS ====================

      @Get('activities')
      @ApiOperation({ summary: 'Query activity logs with filters' })
      @ApiResponse({ status: 200, description: 'Paginated activity logs' })
      async queryActivityLogs(
        @CurrentUser() user: RequestUser,
        @Query() query: QueryActivityLogsDto,
      ) {
        return this.auditService.queryActivityLogs(user.tenant_id, query);
      }

      @Post('activities')
      @ApiOperation({ summary: 'Create an activity log entry' })
      @ApiResponse({ status: 201, description: 'Activity log created' })
      async createActivityLog(
        @CurrentUser() user: RequestUser,
        @Body() dto: CreateActivityLogDto,
        @Req() request: any,
      ) {
        return this.auditService.createActivityLog(
          user.tenant_id,
          user.id,
          dto,
          {
            ip_address: request.ip,
            user_agent: request.headers['user-agent'],
            session_id: request.headers['x-session-id'],
          },
        );
      }

      @Get('activities/summary')
      @ApiOperation({ summary: 'Get user activity summary' })
      @ApiResponse({ status: 200, description: 'Activity summary by type' })
      async getUserActivitySummary(
        @CurrentUser() user: RequestUser,
        @Query('days') days?: number,
      ) {
        return this.auditService.getUserActivitySummary(
          user.tenant_id,
          user.id,
          days || 30,
        );
      }

      @Get('activities/user/:userId')
      @ApiOperation({ summary: 'Get activity summary for a specific user' })
      @ApiParam({ name: 'userId', description: 'User ID' })
      @ApiResponse({ status: 200, description: 'User activity summary' })
      async getSpecificUserActivitySummary(
        @CurrentUser() user: RequestUser,
        @Param('userId') userId: string,
        @Query('days') days?: number,
      ) {
        return this.auditService.getUserActivitySummary(
          user.tenant_id,
          userId,
          days || 30,
        );
      }
    }