diff --git a/packages/nestjs-report/src/__fixtures__/file/file-entity.fixture.ts b/packages/nestjs-report/src/__fixtures__/file/file-entity.fixture.ts index 97d60ada..9a9ecacc 100644 --- a/packages/nestjs-report/src/__fixtures__/file/file-entity.fixture.ts +++ b/packages/nestjs-report/src/__fixtures__/file/file-entity.fixture.ts @@ -1,5 +1,5 @@ -import { FileSqliteEntity } from '@concepta/nestjs-file'; import { Entity, OneToOne } from 'typeorm'; +import { FileSqliteEntity } from '@concepta/nestjs-file'; import { ReportEntityFixture } from '../report/report-entity.fixture'; import { ReportEntityInterface } from '../../interfaces/report-entity.interface'; diff --git a/packages/nestjs-report/src/__fixtures__/my-report-generator-short-delay.service.ts b/packages/nestjs-report/src/__fixtures__/my-report-generator-short-delay.service.ts index 6baf2bee..2e1e24ff 100644 --- a/packages/nestjs-report/src/__fixtures__/my-report-generator-short-delay.service.ts +++ b/packages/nestjs-report/src/__fixtures__/my-report-generator-short-delay.service.ts @@ -1,13 +1,12 @@ +import { Inject } from '@nestjs/common'; +import { ReportInterface, ReportStatusEnum } from '@concepta/ts-common'; import { FileService } from '@concepta/nestjs-file'; -import { ReportStatusEnum } from '@concepta/ts-common'; -import { ReportEntityInterface } from '../interfaces/report-entity.interface'; import { ReportGeneratorResultInterface } from '../interfaces/report-generator-result.interface'; import { ReportGeneratorServiceInterface } from '../interfaces/report-generator-service.interface'; import { AWS_KEY_FIXTURE, REPORT_SHORT_DELAY_KEY_FIXTURE, } from './constants.fixture'; -import { Inject } from '@nestjs/common'; import { delay } from '../utils/delay.util'; export class MyReportGeneratorShortDelayService @@ -22,13 +21,13 @@ export class MyReportGeneratorShortDelayService generateTimeout: number = 100; - async getDownloadUrl(report: ReportEntityInterface): Promise { + async getDownloadUrl(report: ReportInterface): Promise { const file = await this.fileService.fetch({ id: report.id }); return file.downloadUrl || ''; } async generate( - report: ReportEntityInterface, + report: ReportInterface, ): Promise { const file = await this.fileService.push({ fileName: report.name, diff --git a/packages/nestjs-report/src/__fixtures__/my-report-generator.service.ts b/packages/nestjs-report/src/__fixtures__/my-report-generator.service.ts index d92d66cf..a8e2e172 100644 --- a/packages/nestjs-report/src/__fixtures__/my-report-generator.service.ts +++ b/packages/nestjs-report/src/__fixtures__/my-report-generator.service.ts @@ -1,10 +1,9 @@ +import { Inject } from '@nestjs/common'; +import { ReportInterface, ReportStatusEnum } from '@concepta/ts-common'; import { FileService } from '@concepta/nestjs-file'; -import { ReportStatusEnum } from '@concepta/ts-common'; -import { ReportEntityInterface } from '../interfaces/report-entity.interface'; import { ReportGeneratorResultInterface } from '../interfaces/report-generator-result.interface'; import { ReportGeneratorServiceInterface } from '../interfaces/report-generator-service.interface'; import { AWS_KEY_FIXTURE, REPORT_KEY_FIXTURE } from './constants.fixture'; -import { Inject } from '@nestjs/common'; export class MyReportGeneratorService implements ReportGeneratorServiceInterface @@ -17,14 +16,14 @@ export class MyReportGeneratorService KEY: string = REPORT_KEY_FIXTURE; generateTimeout: number = 60000; - async getDownloadUrl(report: ReportEntityInterface): Promise { + async getDownloadUrl(report: ReportInterface): Promise { if (!report?.file?.id) return ''; const file = await this.fileService.fetch({ id: report.file.id }); return file.downloadUrl || ''; } async generate( - report: ReportEntityInterface, + report: ReportInterface, ): Promise { const file = await this.fileService.push({ fileName: report.name, diff --git a/packages/nestjs-report/src/__fixtures__/report/report-entity.fixture.ts b/packages/nestjs-report/src/__fixtures__/report/report-entity.fixture.ts index fb3e8e92..4d85d623 100644 --- a/packages/nestjs-report/src/__fixtures__/report/report-entity.fixture.ts +++ b/packages/nestjs-report/src/__fixtures__/report/report-entity.fixture.ts @@ -1,5 +1,5 @@ -import { FileEntityInterface } from '@concepta/nestjs-file'; import { Entity, JoinColumn, OneToOne } from 'typeorm'; +import { FileEntityInterface } from '@concepta/nestjs-file'; import { ReportSqliteEntity } from '../../entities/report-sqlite.entity'; import { FileEntityFixture } from '../file/file-entity.fixture'; diff --git a/packages/nestjs-report/src/dto/report.dto.ts b/packages/nestjs-report/src/dto/report.dto.ts index c8b51434..55684a49 100644 --- a/packages/nestjs-report/src/dto/report.dto.ts +++ b/packages/nestjs-report/src/dto/report.dto.ts @@ -1,19 +1,15 @@ -import { CommonEntityDto, ReferenceIdDto } from '@concepta/nestjs-common'; -import { ReportStatusEnum } from '@concepta/ts-common'; -import { ReferenceIdInterface } from '@concepta/ts-core'; -import { ApiProperty } from '@nestjs/swagger'; import { Exclude, Expose, Type } from 'class-transformer'; import { IsEnum, IsOptional, IsString, ValidateNested } from 'class-validator'; -import { ReportEntityInterface } from '../interfaces/report-entity.interface'; +import { ApiProperty } from '@nestjs/swagger'; +import { ReferenceIdInterface } from '@concepta/ts-core'; +import { ReportInterface, ReportStatusEnum } from '@concepta/ts-common'; +import { CommonEntityDto, ReferenceIdDto } from '@concepta/nestjs-common'; /** * Report DTO */ @Exclude() -export class ReportDto - extends CommonEntityDto - implements ReportEntityInterface -{ +export class ReportDto extends CommonEntityDto implements ReportInterface { /** * Storage provider key */ diff --git a/packages/nestjs-report/src/entities/report-postgres.entity.ts b/packages/nestjs-report/src/entities/report-postgres.entity.ts index 36abda37..14b69977 100644 --- a/packages/nestjs-report/src/entities/report-postgres.entity.ts +++ b/packages/nestjs-report/src/entities/report-postgres.entity.ts @@ -1,7 +1,7 @@ +import { Column, Entity, Unique } from 'typeorm'; import { ReportStatusEnum } from '@concepta/ts-common'; import { ReferenceIdInterface } from '@concepta/ts-core'; import { CommonPostgresEntity } from '@concepta/typeorm-common'; -import { Column, Entity, Unique } from 'typeorm'; import { ReportEntityInterface } from '../interfaces/report-entity.interface'; /** diff --git a/packages/nestjs-report/src/entities/report-sqlite.entity.ts b/packages/nestjs-report/src/entities/report-sqlite.entity.ts index d6033091..a18ea199 100644 --- a/packages/nestjs-report/src/entities/report-sqlite.entity.ts +++ b/packages/nestjs-report/src/entities/report-sqlite.entity.ts @@ -1,7 +1,7 @@ +import { Column, Entity, Unique } from 'typeorm'; import { ReportStatusEnum } from '@concepta/ts-common'; import { ReferenceIdInterface } from '@concepta/ts-core'; import { CommonSqliteEntity } from '@concepta/typeorm-common'; -import { Column, Entity, Unique } from 'typeorm'; import { ReportEntityInterface } from '../interfaces/report-entity.interface'; /** diff --git a/packages/nestjs-report/src/enum/report-status.enum.ts b/packages/nestjs-report/src/enum/report-status.enum.ts deleted file mode 100644 index 6c1cc170..00000000 --- a/packages/nestjs-report/src/enum/report-status.enum.ts +++ /dev/null @@ -1,8 +0,0 @@ -/** - * Enum for report status - */ -export enum ReportStatusEnum { - Processing = 'Processing', - Complete = 'Complete', - Error = 'Error', -} diff --git a/packages/nestjs-report/src/exceptions/report-create.exception.ts b/packages/nestjs-report/src/exceptions/report-create.exception.ts index e235c5fe..ae522373 100644 --- a/packages/nestjs-report/src/exceptions/report-create.exception.ts +++ b/packages/nestjs-report/src/exceptions/report-create.exception.ts @@ -1,15 +1,13 @@ -import { HttpStatus } from '@nestjs/common'; -import { RuntimeException } from '@concepta/nestjs-exception'; +import { + RuntimeException, + RuntimeExceptionOptions, +} from '@concepta/nestjs-exception'; export class ReportCreateException extends RuntimeException { - constructor( - message = 'Error while trying to create a report', - originalError: unknown, - ) { + constructor(options?: RuntimeExceptionOptions) { super({ - message, - originalError, - httpStatus: HttpStatus.INTERNAL_SERVER_ERROR, + message: 'Error while trying to create a report', + ...options, }); this.errorCode = 'REPORT_CREATE_ERROR'; diff --git a/packages/nestjs-report/src/exceptions/report-duplicated.exception.ts b/packages/nestjs-report/src/exceptions/report-duplicated.exception.ts index 7b2ea7f2..656c4888 100644 --- a/packages/nestjs-report/src/exceptions/report-duplicated.exception.ts +++ b/packages/nestjs-report/src/exceptions/report-duplicated.exception.ts @@ -1,5 +1,8 @@ import { HttpStatus } from '@nestjs/common'; -import { RuntimeException } from '@concepta/nestjs-exception'; +import { + RuntimeException, + RuntimeExceptionOptions, +} from '@concepta/nestjs-exception'; export class ReportDuplicateEntryException extends RuntimeException { context: RuntimeException['context'] & { @@ -10,14 +13,13 @@ export class ReportDuplicateEntryException extends RuntimeException { constructor( serviceKey: string, reportName: string, - originalError?: unknown, - message = 'Duplicate entry detected for service %s with report %s', + options?: RuntimeExceptionOptions, ) { super({ - message, - messageParams: [serviceKey, reportName], + message: 'Duplicate entry detected for service %s with report %s', httpStatus: HttpStatus.CONFLICT, - originalError, + messageParams: [serviceKey, reportName], + ...options, }); this.errorCode = 'REPORT_DUPLICATE_ENTRY_ERROR'; diff --git a/packages/nestjs-report/src/exceptions/report-generator-service-not-found.exception.ts b/packages/nestjs-report/src/exceptions/report-generator-service-not-found.exception.ts index 2d9a4266..4bdebc39 100644 --- a/packages/nestjs-report/src/exceptions/report-generator-service-not-found.exception.ts +++ b/packages/nestjs-report/src/exceptions/report-generator-service-not-found.exception.ts @@ -1,26 +1,27 @@ import { HttpStatus } from '@nestjs/common'; -import { RuntimeException } from '@concepta/nestjs-exception'; +import { + RuntimeException, + RuntimeExceptionOptions, +} from '@concepta/nestjs-exception'; export class ReportGeneratorServiceNotFoundException extends RuntimeException { context: RuntimeException['context'] & { generatorServiceName: string; }; - constructor( - assignmentName: string, - message = 'Report generator service %s was not registered to be used.', - ) { + constructor(generatorServiceName: string, options?: RuntimeExceptionOptions) { super({ - message, - messageParams: [assignmentName], + message: 'Report generator service %s was not registered to be used.', + messageParams: [generatorServiceName], httpStatus: HttpStatus.NOT_FOUND, + ...options, }); this.errorCode = 'REPORT_GENERATOR_SERVICE_NOT_FOUND_ERROR'; this.context = { ...super.context, - generatorServiceName: assignmentName, + generatorServiceName, }; } } diff --git a/packages/nestjs-report/src/exceptions/report-id-missing.exception.ts b/packages/nestjs-report/src/exceptions/report-id-missing.exception.ts index 8b7b67f5..cc8b0e04 100644 --- a/packages/nestjs-report/src/exceptions/report-id-missing.exception.ts +++ b/packages/nestjs-report/src/exceptions/report-id-missing.exception.ts @@ -1,11 +1,15 @@ import { HttpStatus } from '@nestjs/common'; -import { RuntimeException } from '@concepta/nestjs-exception'; +import { + RuntimeException, + RuntimeExceptionOptions, +} from '@concepta/nestjs-exception'; export class ReportIdMissingException extends RuntimeException { - constructor(message = 'Report id is missing.') { + constructor(options?: RuntimeExceptionOptions) { super({ - message, + message: 'Report id is missing.', httpStatus: HttpStatus.BAD_REQUEST, + ...options, }); this.errorCode = 'REPORT_ID_MISSING_ERROR'; diff --git a/packages/nestjs-report/src/exceptions/report-query.exception.ts b/packages/nestjs-report/src/exceptions/report-query.exception.ts index 6a0bba4b..b3d95d1d 100644 --- a/packages/nestjs-report/src/exceptions/report-query.exception.ts +++ b/packages/nestjs-report/src/exceptions/report-query.exception.ts @@ -1,15 +1,15 @@ import { HttpStatus } from '@nestjs/common'; -import { RuntimeException } from '@concepta/nestjs-exception'; +import { + RuntimeException, + RuntimeExceptionOptions, +} from '@concepta/nestjs-exception'; export class ReportQueryException extends RuntimeException { - constructor( - message = 'Error while trying to do a query to report', - originalError?: unknown, - ) { + constructor(options?: RuntimeExceptionOptions) { super({ - message, - originalError, + message: 'Error while trying to do a query to report', httpStatus: HttpStatus.INTERNAL_SERVER_ERROR, + ...options, }); this.errorCode = 'REPORT_QUERY_ERROR'; diff --git a/packages/nestjs-report/src/exceptions/report-timeout.exception.ts b/packages/nestjs-report/src/exceptions/report-timeout.exception.ts index 5a4d14d5..58df1748 100644 --- a/packages/nestjs-report/src/exceptions/report-timeout.exception.ts +++ b/packages/nestjs-report/src/exceptions/report-timeout.exception.ts @@ -1,11 +1,15 @@ import { HttpStatus } from '@nestjs/common'; -import { RuntimeException } from '@concepta/nestjs-exception'; +import { + RuntimeException, + RuntimeExceptionOptions, +} from '@concepta/nestjs-exception'; export class ReportTimeoutException extends RuntimeException { - constructor(message = 'Report generation timed out.') { + constructor(options?: RuntimeExceptionOptions) { super({ - message, + message: 'Report generation timed out.', httpStatus: HttpStatus.INTERNAL_SERVER_ERROR, + ...options, }); this.errorCode = 'REPORT_GENERATION_TIMEOUT'; diff --git a/packages/nestjs-report/src/index.ts b/packages/nestjs-report/src/index.ts index f2dfc6d9..3c8f3902 100644 --- a/packages/nestjs-report/src/index.ts +++ b/packages/nestjs-report/src/index.ts @@ -12,3 +12,5 @@ export { ReportService } from './services/report.service'; export { ReportDto } from './dto/report.dto'; export { ReportCreateDto } from './dto/report-create.dto'; + +export { DoneCallback } from './report.types'; diff --git a/packages/nestjs-report/src/interfaces/report-generator-result.interface.ts b/packages/nestjs-report/src/interfaces/report-generator-result.interface.ts index 16f47b64..89c1ed26 100644 --- a/packages/nestjs-report/src/interfaces/report-generator-result.interface.ts +++ b/packages/nestjs-report/src/interfaces/report-generator-result.interface.ts @@ -1,5 +1,5 @@ -import { ReportEntityInterface } from './report-entity.interface'; +import { ReportInterface } from '@concepta/ts-common'; export interface ReportGeneratorResultInterface - extends Pick, - Partial> {} + extends Pick, + Partial> {} diff --git a/packages/nestjs-report/src/interfaces/report-service.interface.ts b/packages/nestjs-report/src/interfaces/report-service.interface.ts index 04f6d6e9..16ae0d0e 100644 --- a/packages/nestjs-report/src/interfaces/report-service.interface.ts +++ b/packages/nestjs-report/src/interfaces/report-service.interface.ts @@ -1,11 +1,9 @@ -import { ReportEntityInterface } from './report-entity.interface'; import { DoneCallback } from '../report.types'; import { ReportCreateDto } from '../dto/report-create.dto'; +import { ReportInterface } from '@concepta/ts-common'; export interface ReportServiceInterface { - generate(report: ReportCreateDto): Promise; - fetch( - report: Pick, - ): Promise; + generate(report: ReportCreateDto): Promise; + fetch(report: Pick): Promise; done: DoneCallback; } diff --git a/packages/nestjs-report/src/interfaces/report-strategy-service.interface.ts b/packages/nestjs-report/src/interfaces/report-strategy-service.interface.ts index 08c6b861..1c62a55a 100644 --- a/packages/nestjs-report/src/interfaces/report-strategy-service.interface.ts +++ b/packages/nestjs-report/src/interfaces/report-strategy-service.interface.ts @@ -1,13 +1,7 @@ -import { ReportEntityInterface } from './report-entity.interface'; +import { ReportInterface } from '@concepta/ts-common'; import { ReportGeneratorResultInterface } from './report-generator-result.interface'; -import { ReportGeneratorServiceInterface } from './report-generator-service.interface'; export interface ReportStrategyServiceInterface { - generate( - report: ReportEntityInterface, - ): Promise; - getDownloadUrl(report: ReportEntityInterface): Promise; - resolveGeneratorService( - report: ReportEntityInterface, - ): ReportGeneratorServiceInterface; + generate(report: ReportInterface): Promise; + getDownloadUrl(report: ReportInterface): Promise; } diff --git a/packages/nestjs-report/src/report.module.spec.ts b/packages/nestjs-report/src/report.module.spec.ts index 0329c90b..aa68b5cc 100644 --- a/packages/nestjs-report/src/report.module.spec.ts +++ b/packages/nestjs-report/src/report.module.spec.ts @@ -1,11 +1,13 @@ +import { Repository } from 'typeorm'; +import { DynamicModule, ModuleMetadata } from '@nestjs/common'; +import { Test, TestingModule } from '@nestjs/testing'; +import { ReportStatusEnum } from '@concepta/ts-common'; +import { FileModule } from '@concepta/nestjs-file'; import { getDynamicRepositoryToken, getEntityRepositoryToken, TypeOrmExtModule, } from '@concepta/nestjs-typeorm-ext'; -import { DynamicModule, ModuleMetadata } from '@nestjs/common'; -import { Test, TestingModule } from '@nestjs/testing'; -import { Repository } from 'typeorm'; import { ReportService } from './services/report.service'; @@ -13,8 +15,6 @@ import { REPORT_MODULE_REPORT_ENTITY_KEY } from './report.constants'; import { ReportEntityInterface } from './interfaces/report-entity.interface'; -import { FileModule } from '@concepta/nestjs-file'; -import { ReportStatusEnum } from '@concepta/ts-common'; import { AwsStorageService } from './__fixtures__/aws-storage.service'; import { REPORT_KEY_FIXTURE, diff --git a/packages/nestjs-report/src/services/report-strategy.service.spec.ts b/packages/nestjs-report/src/services/report-strategy.service.spec.ts index 1f6f5807..3b372dfe 100644 --- a/packages/nestjs-report/src/services/report-strategy.service.spec.ts +++ b/packages/nestjs-report/src/services/report-strategy.service.spec.ts @@ -1,11 +1,11 @@ +import { randomUUID } from 'crypto'; +import { mock } from 'jest-mock-extended'; import { ReportCreatableInterface, + ReportInterface, ReportStatusEnum, } from '@concepta/ts-common'; -import { randomUUID } from 'crypto'; -import { mock } from 'jest-mock-extended'; import { ReportCreateDto } from '../dto/report-create.dto'; -import { ReportEntityInterface } from '../interfaces/report-entity.interface'; import { ReportGeneratorServiceInterface } from '../interfaces/report-generator-service.interface'; import { ReportStrategyService } from './report-strategy.service'; @@ -16,8 +16,8 @@ class MockStorageService implements ReportGeneratorServiceInterface { uploadTimeout = 3600; - generate(_report: ReportCreatableInterface): Promise { - const mockReport: ReportEntityInterface = { + generate(_report: ReportCreatableInterface): Promise { + const mockReport: ReportInterface = { id: randomUUID(), serviceKey: 'test-service', name: 'test.txt', @@ -65,7 +65,7 @@ describe(ReportStrategyService.name, () => { describe(ReportStrategyService.prototype.generate.name, () => { it('should return the upload URL from the correct storage service', async () => { - const mockReport: ReportEntityInterface = mock({ + const mockReport: ReportInterface = mock({ serviceKey: 'mock-service', name: 'test.jpg', }); @@ -95,17 +95,20 @@ describe(ReportStrategyService.name, () => { }); }); - describe(ReportStrategyService.prototype.resolveGeneratorService.name, () => { - it('should return the correct storage service for a given report', async () => { - const mockReport = new ReportCreateDto(); - mockReport.serviceKey = 'mock-service'; - reportStrategyService.addStorageService(mockStorageService); + describe( + ReportStrategyService.prototype['resolveGeneratorService'].name, + () => { + it('should return the correct storage service for a given report', async () => { + const mockReport = new ReportCreateDto(); + mockReport.serviceKey = 'mock-service'; + reportStrategyService.addStorageService(mockStorageService); - const result = await reportStrategyService.resolveGeneratorService( - mockReport, - ); + const result = await reportStrategyService['resolveGeneratorService']( + mockReport, + ); - expect(result).toBe(mockStorageService); - }); - }); + expect(result).toBe(mockStorageService); + }); + }, + ); }); diff --git a/packages/nestjs-report/src/services/report-strategy.service.ts b/packages/nestjs-report/src/services/report-strategy.service.ts index df9c239f..3afcc11e 100644 --- a/packages/nestjs-report/src/services/report-strategy.service.ts +++ b/packages/nestjs-report/src/services/report-strategy.service.ts @@ -1,6 +1,6 @@ +import { Inject } from '@nestjs/common'; import { ReportCreatableInterface } from '@concepta/ts-common'; import { mapNonErrorToException } from '@concepta/ts-core'; -import { Inject } from '@nestjs/common'; import { ReportGeneratorServiceNotFoundException } from '../exceptions/report-generator-service-not-found.exception'; import { ReportTimeoutException } from '../exceptions/report-timeout.exception'; import { ReportEntityInterface } from '../interfaces/report-entity.interface'; @@ -48,7 +48,7 @@ export class ReportStrategyService implements ReportStrategyServiceInterface { return this.resolveGeneratorService(report).getDownloadUrl(report); } - resolveGeneratorService( + protected resolveGeneratorService( report: ReportCreatableInterface, ): ReportGeneratorServiceInterface { const generatorService = this.reportGeneratorServices.find( @@ -64,7 +64,7 @@ export class ReportStrategyService implements ReportStrategyServiceInterface { throw new ReportGeneratorServiceNotFoundException(report.serviceKey); } - private createTimeout(timeoutMs: number): Promise { + protected createTimeout(timeoutMs: number): Promise { return new Promise((_, reject) => { setTimeout(() => reject(new ReportTimeoutException()), timeoutMs); }); diff --git a/packages/nestjs-report/src/services/report.service.spec.ts b/packages/nestjs-report/src/services/report.service.spec.ts index 2706e119..9750549a 100644 --- a/packages/nestjs-report/src/services/report.service.spec.ts +++ b/packages/nestjs-report/src/services/report.service.spec.ts @@ -1,10 +1,10 @@ +import { randomUUID } from 'crypto'; +import { mock } from 'jest-mock-extended'; +import { Repository } from 'typeorm'; import { ReportCreatableInterface, ReportStatusEnum, } from '@concepta/ts-common'; -import { randomUUID } from 'crypto'; -import { mock } from 'jest-mock-extended'; -import { Repository } from 'typeorm'; import { ReportCreateDto } from '../dto/report-create.dto'; import { ReportDuplicateEntryException } from '../exceptions/report-duplicated.exception'; import { ReportQueryException } from '../exceptions/report-query.exception'; diff --git a/packages/nestjs-report/src/services/report.service.ts b/packages/nestjs-report/src/services/report.service.ts index b50dee8e..bf19e3e6 100644 --- a/packages/nestjs-report/src/services/report.service.ts +++ b/packages/nestjs-report/src/services/report.service.ts @@ -1,9 +1,9 @@ -import { InjectDynamicRepository } from '@concepta/nestjs-typeorm-ext'; -import { BaseService } from '@concepta/typeorm-common'; -import { Inject, Injectable } from '@nestjs/common'; import { Repository } from 'typeorm'; - -import { ReportStatusEnum } from '@concepta/ts-common'; +import { HttpStatus, Inject, Injectable } from '@nestjs/common'; +import { mapNonErrorToException } from '@concepta/ts-core'; +import { ReportInterface, ReportStatusEnum } from '@concepta/ts-common'; +import { BaseService } from '@concepta/typeorm-common'; +import { InjectDynamicRepository } from '@concepta/nestjs-typeorm-ext'; import { REPORT_MODULE_REPORT_ENTITY_KEY, @@ -18,7 +18,6 @@ import { ReportCreateException } from '../exceptions/report-create.exception'; import { ReportIdMissingException } from '../exceptions/report-id-missing.exception'; import { ReportDuplicateEntryException } from '../exceptions/report-duplicated.exception'; import { ReportGeneratorResultInterface } from '../interfaces/report-generator-result.interface'; -import { mapNonErrorToException } from '@concepta/ts-core'; /** * Service responsible for managing report operations. @@ -37,7 +36,7 @@ export class ReportService super(reportRepo); } - async generate(report: ReportCreateDto): Promise { + async generate(report: ReportCreateDto): Promise { await this.checkExistingReport(report); try { const reportDb = await this.createAndSaveReport(report); @@ -46,42 +45,71 @@ export class ReportService this.generateAndProcessReport(reportDb); return reportDb; - } catch (err) { - throw new ReportCreateException(this.metadata.targetName, err); + } catch (originalError) { + throw new ReportCreateException({ originalError }); } } - async fetch( - report: Pick, - ): Promise { - if (!report.id) throw new ReportIdMissingException(); - const dbReport = await this.reportRepo.findOne({ - where: { - id: report.id, - }, - relations: ['file'], - }); - if (!dbReport) throw new ReportQueryException(); + async fetch(report: Pick): Promise { + if (!report.id) { + throw new ReportIdMissingException(); + } + + let dbReport: ReportEntityInterface | null = null; + + try { + dbReport = await this.reportRepo.findOne({ + where: { + id: report.id, + }, + relations: ['file'], + }); + } catch (originalError) { + throw new ReportQueryException({ originalError }); + } + + if (!dbReport) { + throw new ReportQueryException({ + message: 'Report with id %s not found', + messageParams: [report.id], + httpStatus: HttpStatus.NOT_FOUND, + }); + } + return this.addDownloadUrl(dbReport); } async done(report: ReportGeneratorResultInterface): Promise { - if (!report.id) throw new ReportIdMissingException(); + if (!report.id) { + throw new ReportIdMissingException(); + } - const updatedReport = await this.reportRepo.findOne({ - where: { id: report.id }, - }); + let reportToUpdate: ReportEntityInterface | null = null; - if (!updatedReport) throw new ReportQueryException(); + try { + reportToUpdate = await this.reportRepo.findOne({ + where: { id: report.id }, + }); + } catch (originalError) { + throw new ReportQueryException({ originalError }); + } + + if (!reportToUpdate) { + throw new ReportQueryException({ + message: 'Report with id %s not found', + messageParams: [report.id], + httpStatus: HttpStatus.NOT_FOUND, + }); + } - updatedReport.status = report.status; - updatedReport.errorMessage = report.errorMessage || null; - updatedReport.file = report.file; + reportToUpdate.status = report.status; + reportToUpdate.errorMessage = report.errorMessage ?? null; + reportToUpdate.file = report.file; - await this.reportRepo.save(updatedReport); + await this.reportRepo.save(reportToUpdate); } - private async generateAndProcessReport( + protected async generateAndProcessReport( reportDb: ReportEntityInterface, ): Promise { try { @@ -97,7 +125,7 @@ export class ReportService } } - private async createAndSaveReport( + protected async createAndSaveReport( report: ReportCreateDto, ): Promise { const newReport = this.reportRepo.create({ @@ -108,7 +136,7 @@ export class ReportService return await this.reportRepo.save(newReport); } - private async checkExistingReport(report: ReportCreateDto): Promise { + protected async checkExistingReport(report: ReportCreateDto): Promise { const existingReport = await this.reportRepo.findOne({ where: { serviceKey: report.serviceKey, @@ -121,7 +149,7 @@ export class ReportService } } - private async addDownloadUrl( + protected async addDownloadUrl( report: ReportEntityInterface, ): Promise { if (report.file?.id) { diff --git a/packages/ts-common/src/file/interfaces/file-ownable.interface.ts b/packages/ts-common/src/file/interfaces/file-ownable.interface.ts deleted file mode 100644 index 37b0f17f..00000000 --- a/packages/ts-common/src/file/interfaces/file-ownable.interface.ts +++ /dev/null @@ -1,8 +0,0 @@ -import { ReferenceId } from '@concepta/ts-core'; -import { FileInterface } from './file.interface'; - -export interface FileOwnableInterface { - // TODO: should i have this as ReferenceId or null? - fileId: ReferenceId | null; - file?: FileInterface; -} diff --git a/packages/ts-common/src/report/interfaces/report.interface.ts b/packages/ts-common/src/report/interfaces/report.interface.ts index 64aaab7f..ba3af556 100644 --- a/packages/ts-common/src/report/interfaces/report.interface.ts +++ b/packages/ts-common/src/report/interfaces/report.interface.ts @@ -1,6 +1,5 @@ import { AuditInterface, ReferenceIdInterface } from '@concepta/ts-core'; import { ReportStatusEnum } from '../enum/report-status.enum'; -// import { FileOwnableInterface } from '../../file/interfaces/file-ownable.interface'; /** * Interface representing a report entity