forked from Shared/esg
81 lines
3.1 KiB
TypeScript
81 lines
3.1 KiB
TypeScript
import { Controller, Delete, Get, Query, Req, UseGuards } from '@nestjs/common';
|
|
import { ApiBearerAuth, ApiOperation, ApiTags } from '@nestjs/swagger';
|
|
import { Request } from 'express';
|
|
import { AuditEvent } from '../common/enums/audit-event.enum';
|
|
import { AppException } from '../common/exceptions/app-exception';
|
|
import { AuditService } from '../audit/audit.service';
|
|
import { CurrentUser } from '../auth/decorators/current-user.decorator';
|
|
import { AuthenticatedUser } from '../auth/interfaces/authenticated-user.interface';
|
|
import { Roles } from '../auth/decorators/roles.decorator';
|
|
import { JwtAuthGuard } from '../auth/guards/jwt-auth.guard';
|
|
import { RolesGuard } from '../auth/guards/roles.guard';
|
|
import { Role } from '../common/enums/role.enum';
|
|
import { InquiryLogQueryDto, InquiryLogTimelineQueryDto } from './dto/inquiry-log-query.dto';
|
|
import { InquiryLogService } from './inquiry-log.service';
|
|
|
|
@ApiTags('Inquiry Logs')
|
|
@ApiBearerAuth()
|
|
@Controller('inquiry-logs')
|
|
@UseGuards(JwtAuthGuard, RolesGuard)
|
|
// Inquiry logs contain operational and potentially sensitive provider payloads.
|
|
// Keep the dashboard data restricted to the seeded/managed SUPER_ADMIN role.
|
|
@Roles(Role.SUPER_ADMIN)
|
|
export class InquiryLogsController {
|
|
constructor(
|
|
private readonly inquiryLogService: InquiryLogService,
|
|
private readonly auditService: AuditService,
|
|
) {}
|
|
|
|
@Delete()
|
|
@ApiOperation({ summary: 'Delete all inquiry request logs (explicitly enabled local maintenance action)' })
|
|
async purgeAll(
|
|
@CurrentUser() user: AuthenticatedUser,
|
|
@Req() request: Request,
|
|
): Promise<{ deletedCount: number }> {
|
|
if (process.env.INQUIRY_LOG_PURGE_ENABLED !== 'true') {
|
|
throw new AppException('INQUIRY_LOG_PURGE_DISABLED');
|
|
}
|
|
|
|
const result = await this.inquiryLogService.purgeAll();
|
|
await this.auditService.log(AuditEvent.INQUIRY_LOGS_PURGED, {
|
|
userId: user.id,
|
|
username: user.username,
|
|
ip: request.ip,
|
|
userAgent: request.headers['user-agent'],
|
|
metadata: { deletedCount: result.deletedCount },
|
|
});
|
|
|
|
return result;
|
|
}
|
|
|
|
@Get()
|
|
@ApiOperation({ summary: 'Search inquiry request logs' })
|
|
async findLogs(@Query() query: InquiryLogQueryDto) {
|
|
return this.inquiryLogService.findLogs(query);
|
|
}
|
|
|
|
@Get('summary')
|
|
@ApiOperation({ summary: 'Get request totals, success/failure counts, and duration metrics' })
|
|
async getSummary(@Query() query: InquiryLogQueryDto) {
|
|
return this.inquiryLogService.getSummary(query);
|
|
}
|
|
|
|
@Get('errors/common')
|
|
@ApiOperation({ summary: 'Get most common errors for a provider, inquiry type, and time range' })
|
|
async getCommonErrors(@Query() query: InquiryLogQueryDto) {
|
|
return this.inquiryLogService.getCommonErrors(query);
|
|
}
|
|
|
|
@Get('durations/extremes')
|
|
@ApiOperation({ summary: 'Get fastest and slowest inquiry requests' })
|
|
async getDurationExtremes(@Query() query: InquiryLogQueryDto) {
|
|
return this.inquiryLogService.getDurationExtremes(query);
|
|
}
|
|
|
|
@Get('timeline')
|
|
@ApiOperation({ summary: 'Get request volume, status, error, and duration trend buckets' })
|
|
async getTimeline(@Query() query: InquiryLogTimelineQueryDto) {
|
|
return this.inquiryLogService.getTimeline(query);
|
|
}
|
|
}
|