Skip to content

Commit

Permalink
abstract report generation service
Browse files Browse the repository at this point in the history
  • Loading branch information
alexeh committed Dec 19, 2023
1 parent 84c3a6a commit 1ef8f2c
Show file tree
Hide file tree
Showing 7 changed files with 53 additions and 46 deletions.
23 changes: 15 additions & 8 deletions api/src/modules/reports/csv-report.service.ts
Original file line number Diff line number Diff line change
@@ -1,13 +1,20 @@
import { AsyncParser } from '@json2csv/node';
import { Injectable } from '@nestjs/common';
import { IReportService } from 'modules/reports/report-service.interface';
import { ParserOptions } from '@json2csv/plainjs';

// import { AsyncParser } from '@json2csv/node';
// Above blows because of node module resolution. if updated to node16 or nodenext, other
// dependencies cant be transpiled. We could update some, but i.e class-validator is at its last version
// eslint-disable-next-line @typescript-eslint/no-var-requires
const { AsyncParser } = require('@json2csv/node');

@Injectable()
export class CSVReportService {
async generateImportErrorReportStream(errors: any): Promise<string> {
const json2csvOptions: any = {
fields: ['line', 'error'],
};
const asyncParser: AsyncParser<any, any> = new AsyncParser(json2csvOptions);
return asyncParser.parse(errors).promise();
export class CSVReportService implements IReportService {
private getParser(parserOptions: ParserOptions): typeof AsyncParser {
return new AsyncParser(parserOptions);
}

generateReport(data: any, options: ParserOptions): Promise<string> {
return this.getParser(options).parse(data).promise();
}
}
3 changes: 3 additions & 0 deletions api/src/modules/reports/report-service.interface.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
export interface IReportService {
generateReport(data: any, options: object): Promise<string>;
}
6 changes: 3 additions & 3 deletions api/src/modules/reports/reports.module.ts
Original file line number Diff line number Diff line change
@@ -1,10 +1,10 @@
import { Module } from '@nestjs/common';
import { CSVReportService } from 'modules/reports/csv-report.service';

export const IReportService: string = 'IReportService';
export const ReportServiceToken: string = 'IReportService';

@Module({
providers: [{ provide: IReportService, useClass: CSVReportService }],
exports: [IReportService],
providers: [{ provide: ReportServiceToken, useClass: CSVReportService }],
exports: [ReportServiceToken],
})
export class ReportsModule {}
27 changes: 0 additions & 27 deletions api/src/modules/tasks/report.service.ts

This file was deleted.

22 changes: 22 additions & 0 deletions api/src/modules/tasks/task-report.service.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,22 @@
import { Inject, Injectable } from '@nestjs/common';
import { IReportService } from 'modules/reports/report-service.interface';
import { ReportServiceToken } from 'modules/reports/reports.module';

export interface ErrorRecord {
line: number;
error: string;
}

@Injectable()
export class TaskReportService {
constructor(
@Inject(ReportServiceToken) private reportService: IReportService,
) {}

async createImportErrorReport(errors: ErrorRecord[]): Promise<string> {
const parserOptions: { fields: ['line', 'error'] } = {
fields: ['line', 'error'],
};
return this.reportService.generateReport(errors, parserOptions);
}
}
7 changes: 4 additions & 3 deletions api/src/modules/tasks/tasks.module.ts
Original file line number Diff line number Diff line change
Expand Up @@ -4,12 +4,13 @@ import { TasksService } from 'modules/tasks/tasks.service';
import { Task } from 'modules/tasks/task.entity';
import { TypeOrmModule } from '@nestjs/typeorm';
import { TasksRepository } from 'modules/tasks/tasks.repository';
import { ReportService } from 'modules/tasks/report.service';
import { TaskReportService } from 'modules/tasks/task-report.service';
import { ReportsModule } from 'modules/reports/reports.module';

@Module({
imports: [TypeOrmModule.forFeature([Task])],
imports: [TypeOrmModule.forFeature([Task]), ReportsModule],
controllers: [TasksController],
providers: [TasksService, TasksRepository, ReportService],
providers: [TasksService, TasksRepository, TaskReportService],
exports: [TasksService],
})
export class TasksModule {}
11 changes: 6 additions & 5 deletions api/src/modules/tasks/tasks.service.ts
Original file line number Diff line number Diff line change
Expand Up @@ -17,7 +17,10 @@ import {
UpdateTaskDto,
} from 'modules/tasks/dto/update-task.dto';
import { GetReportsDto } from 'modules/tasks/dto/get-reports.dto';
import { ErrorRecord, ReportService } from 'modules/tasks/report.service';
import {
ErrorRecord,
TaskReportService,
} from 'modules/tasks/task-report.service';

@Injectable()
export class TasksService extends AppBaseService<
Expand All @@ -28,7 +31,7 @@ export class TasksService extends AppBaseService<
> {
constructor(
public readonly taskRepository: TasksRepository,
public readonly reportService: ReportService,
public readonly reportService: TaskReportService,
) {
super(
taskRepository,
Expand Down Expand Up @@ -141,8 +144,6 @@ export class TasksService extends AppBaseService<
throw new NotFoundException(`Could not found Task with ID: ${taskId}`);
}
const { errors } = task;
return this.reportService.generateImportErrorReportStream(
errors as ErrorRecord[],
);
return this.reportService.createImportErrorReport(errors as ErrorRecord[]);
}
}

0 comments on commit 1ef8f2c

Please sign in to comment.