fase(15): reporting module with pdf generation
This commit is contained in:
@@ -0,0 +1,50 @@
|
||||
import { UseCase } from '../../../../shared/application/UseCase';
|
||||
import { Result, Ok, Err } from '../../../../shared/domain/Result';
|
||||
import { EventBus } from '../../../../shared/application/EventBus';
|
||||
import { IReportRepository } from '../../domain/ports/IReportRepository';
|
||||
import { Report, ReportFilters } from '../../domain/entities/Report';
|
||||
import { ReportFormat } from '../../domain/value-objects/ReportFormat';
|
||||
|
||||
export interface GenerateReportRequest {
|
||||
title: string;
|
||||
format: 'html' | 'json' | 'pdf';
|
||||
filters?: ReportFilters;
|
||||
}
|
||||
|
||||
export interface GenerateReportResponse {
|
||||
reportId: string;
|
||||
status: string;
|
||||
}
|
||||
|
||||
export class GenerateReportCommand
|
||||
implements UseCase<GenerateReportRequest, GenerateReportResponse, string>
|
||||
{
|
||||
constructor(
|
||||
private readonly reportRepository: IReportRepository,
|
||||
private readonly eventBus: EventBus
|
||||
) {}
|
||||
|
||||
async execute(request: GenerateReportRequest): Promise<Result<GenerateReportResponse, string>> {
|
||||
let format: ReportFormat;
|
||||
try {
|
||||
format = ReportFormat.fromString(request.format);
|
||||
} catch {
|
||||
return Err(`Invalid format: ${request.format}`);
|
||||
}
|
||||
|
||||
const report = Report.create({
|
||||
title: request.title,
|
||||
format,
|
||||
filters: request.filters ?? {},
|
||||
});
|
||||
|
||||
await this.reportRepository.save(report);
|
||||
|
||||
const events = report.clearEvents();
|
||||
for (const event of events) {
|
||||
await this.eventBus.publish(event);
|
||||
}
|
||||
|
||||
return Ok({ reportId: report.id.toString(), status: report.status.value });
|
||||
}
|
||||
}
|
||||
Reference in New Issue
Block a user