Files
CASAN/packages/casan-control-panel/backend/src/reports/reports.controller.ts
T

58 lines
2.2 KiB
TypeScript

import { BadRequestException, Controller, Get, Header, Inject, Param, Query, Res } from '@nestjs/common';
import type { Response } from 'express';
import { ok } from '../common/api-response.js';
import { parseH6ReportQuery, type H6ReportQueryParams } from './h6-report.js';
import { ReportsService } from './reports.service.js';
interface H6QueryParams extends H6ReportQueryParams {
format?: string;
}
@Controller('api/v1/reports')
export class ReportsController {
constructor(@Inject(ReportsService) private readonly reports: ReportsService) {}
@Get()
catalog() {
return ok(this.reports.catalog());
}
@Get('h6')
h6(@Query() raw: H6QueryParams) {
return ok(this.reports.h6(parseH6ReportQuery(raw)));
}
@Get('h6/export')
@Header('Cache-Control', 'no-store')
exportH6(@Query() raw: H6QueryParams, @Res() response: Response) {
const format = raw.format ?? 'json';
if (format !== 'json' && format !== 'html') throw new BadRequestException('H6_REPORT_INVALID_FORMAT');
const report = this.reports.h6(parseH6ReportQuery(raw));
const body = this.reports.serializeH6(report, format);
const stamp = report.generated_at.slice(0, 10);
response.type(format === 'html' ? 'text/html; charset=utf-8' : 'application/json; charset=utf-8');
response.setHeader('Content-Disposition', `attachment; filename="casan-h6-report-${stamp}.${format}"`);
response.send(body);
}
@Get('run/:traceId')
run(@Param('traceId') traceId: string) {
return ok(this.reports.run(traceId));
}
@Get('run/:traceId/export')
@Header('Cache-Control', 'no-store')
exportRun(
@Param('traceId') traceId: string,
@Query('format') rawFormat: string | undefined,
@Res() response: Response,
) {
const format = rawFormat ?? 'json';
if (format !== 'json' && format !== 'html') throw new BadRequestException('RUN_REPORT_INVALID_FORMAT');
const report = this.reports.run(traceId);
response.type(format === 'html' ? 'text/html; charset=utf-8' : 'application/json; charset=utf-8');
response.setHeader('Content-Disposition', `attachment; filename="casan-run-${traceId}.${format}"`);
response.send(this.reports.serializeRun(report, format));
}
}