Skip to content

Commit

Permalink
chore: adjustments on report
Browse files Browse the repository at this point in the history
  • Loading branch information
tnramalho committed Sep 18, 2024
1 parent a80adfc commit 3bc3519
Show file tree
Hide file tree
Showing 18 changed files with 253 additions and 68 deletions.
6 changes: 3 additions & 3 deletions packages/nestjs-report/src/dto/report-create.dto.ts
Original file line number Diff line number Diff line change
@@ -1,12 +1,12 @@
import { Exclude } from 'class-transformer';
import { PickType } from '@nestjs/swagger';
import { ReportCreatableInterface } from '@concepta/ts-common';
import { PickType } from '@nestjs/swagger';
import { Exclude } from 'class-transformer';
import { ReportDto } from './report.dto';

/**
* Report Create DTO
*/
@Exclude()
export class ReportCreateDto
extends PickType(ReportDto, ['serviceKey', 'name'] as const)
extends PickType(ReportDto, ['serviceKey', 'name', 'status'] as const)
implements ReportCreatableInterface {}
16 changes: 16 additions & 0 deletions packages/nestjs-report/src/dto/report-update.dto.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,16 @@
import { Exclude } from 'class-transformer';
import { IntersectionType, PartialType, PickType } from '@nestjs/swagger';
import { ReportCreatableInterface } from '@concepta/ts-common';
import { ReportDto } from './report.dto';
import { ReportUpdatableInterface } from '@concepta/ts-common/src/report/interfaces/report-updatable.interface';

/**
* Report Update DTO
*/
@Exclude()
export class ReportUpdateDto
extends IntersectionType(
PickType(ReportDto, ['status', 'file'] as const),
PartialType(PickType(ReportDto, ['errorMessage'] as const))
)
implements ReportUpdatableInterface {}
Original file line number Diff line number Diff line change
@@ -0,0 +1,17 @@
import { HttpStatus } from '@nestjs/common';
import {
RuntimeException,
RuntimeExceptionOptions,
} from '@concepta/nestjs-exception';

export class ReportDownloadUrlMissingException extends RuntimeException {
constructor(options?: RuntimeExceptionOptions) {
super({
message: 'Error trying to generate signed download url',
httpStatus: HttpStatus.BAD_REQUEST,
...options,
});

this.errorCode = 'REPORT_DOWNLOAD_URL_ERROR';
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,17 @@
import { HttpStatus } from '@nestjs/common';
import {
RuntimeException,
RuntimeExceptionOptions,
} from '@concepta/nestjs-exception';

export class ReportnameMissingException extends RuntimeException {
constructor(options?: RuntimeExceptionOptions) {
super({
message: 'Reportname is missing.',
httpStatus: HttpStatus.BAD_REQUEST,
...options,
});

this.errorCode = 'REPORTNAME_MISSING_ERROR';
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,17 @@
import { HttpStatus } from '@nestjs/common';
import {
RuntimeException,
RuntimeExceptionOptions,
} from '@concepta/nestjs-exception';

export class ReportServiceKeyMissingException extends RuntimeException {
constructor(options?: RuntimeExceptionOptions) {
super({
message: 'Service key is missing.',
httpStatus: HttpStatus.BAD_REQUEST,
...options,
});

this.errorCode = 'SERVICE_KEY_MISSING_ERROR';
}
}
Original file line number Diff line number Diff line change
@@ -1,5 +1,4 @@
import { ReportInterface } from '@concepta/ts-common';
import { ReportUpdatableInterface } from '@concepta/ts-common/src/report/interfaces/report-updatable.interface';
import { ReferenceIdInterface } from '@concepta/ts-core';

export interface ReportGeneratorResultInterface
extends Pick<ReportInterface, 'id' | 'status' | 'file'>,
Partial<Pick<ReportInterface, 'errorMessage'>> {}
export interface ReportGeneratorResultInterface extends ReportUpdatableInterface, ReferenceIdInterface {}
Original file line number Diff line number Diff line change
@@ -0,0 +1,12 @@
import { ReportCreatableInterface, ReportInterface } from '@concepta/ts-common';
import { LookupIdInterface, ReferenceId, ReferenceIdInterface } from '@concepta/ts-core';
import { QueryOptionsInterface } from '@concepta/typeorm-common';

export interface ReportLookupServiceInterface
extends LookupIdInterface<ReferenceId, ReportInterface, QueryOptionsInterface> {
getUniqueReport(
org: Pick<ReportCreatableInterface, 'serviceKey' | 'name'>,
queryOptions?: QueryOptionsInterface,
): Promise<ReportInterface | null>;
getWithFile(report: ReferenceIdInterface, queryOptions?: QueryOptionsInterface): Promise<ReportInterface | null>;
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,11 @@
import { ReportCreatableInterface } from '@concepta/ts-common';
import { CreateOneInterface, ReferenceIdInterface, UpdateOneInterface } from '@concepta/ts-core';
import { ReportEntityInterface } from './report-entity.interface';
import { ReportUpdatableInterface } from '@concepta/ts-common/src/report/interfaces/report-updatable.interface';

export interface ReportMutateServiceInterface
extends CreateOneInterface<ReportCreatableInterface, ReportEntityInterface>,
UpdateOneInterface<
ReportUpdatableInterface & ReferenceIdInterface,
ReportEntityInterface
> {}
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
import { ReportInterface } from '@concepta/ts-common';

export interface ReportStatusInterface extends Pick<ReportInterface, 'status'> {}
4 changes: 4 additions & 0 deletions packages/nestjs-report/src/report.module-definition.ts
Original file line number Diff line number Diff line change
Expand Up @@ -21,6 +21,8 @@ import { ReportService } from './services/report.service';
import { ReportStrategyService } from './services/report-strategy.service';

import { reportDefaultConfig } from './config/report-default.config';
import { ReportMutateService } from './services/report-mutate.service';
import { ReportLookupService } from './services/report-lookup.service';

const RAW_OPTIONS_TOKEN = Symbol('__REPORT_MODULE_RAW_OPTIONS_TOKEN__');

Expand Down Expand Up @@ -88,6 +90,8 @@ export function createReportProviders(options: {
...(options.providers ?? []),
createReportSettingsProvider(options.overrides),
createStrategyServiceProvider(options.overrides),
ReportMutateService,
ReportLookupService,
ReportStrategyService,
ReportService,
];
Expand Down
4 changes: 4 additions & 0 deletions packages/nestjs-report/src/report.module.spec.ts
Original file line number Diff line number Diff line change
Expand Up @@ -82,6 +82,8 @@ describe(ReportModule, () => {
const result = await reportService.generate({
name: REPORT_NAME_FIXTURE,
serviceKey: REPORT_KEY_FIXTURE,
// TODO: check if proceesing should be defined here or automatically
status: ReportStatusEnum.Processing,
});
return result;
};
Expand All @@ -93,6 +95,7 @@ describe(ReportModule, () => {
const result = await reportService.generate({
name: REPORT_NAME_FIXTURE,
serviceKey: REPORT_SHORT_DELAY_KEY_FIXTURE,
status: ReportStatusEnum.Processing,
});
return result;
};
Expand Down Expand Up @@ -185,6 +188,7 @@ describe(ReportModule, () => {
const result = await reportService.generate({
name: REPORT_NAME_FIXTURE,
serviceKey: REPORT_KEY_FIXTURE,
status: ReportStatusEnum.Processing,
});
expect(result.serviceKey).toBe(REPORT_KEY_FIXTURE);
expect(result.name).toBe(REPORT_NAME_FIXTURE);
Expand Down
67 changes: 67 additions & 0 deletions packages/nestjs-report/src/services/report-lookup.service.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,67 @@
import { InjectDynamicRepository } from '@concepta/nestjs-typeorm-ext';
import { LookupService, QueryOptionsInterface } from '@concepta/typeorm-common';
import { Injectable } from '@nestjs/common';
import { Repository } from 'typeorm';

import { REPORT_MODULE_REPORT_ENTITY_KEY } from '../report.constants';
import { ReportEntityInterface } from '../interfaces/report-entity.interface';
import { ReportLookupServiceInterface } from '../interfaces/report-lookup-service.interface';
import { ReportInterface } from '@concepta/ts-common';
import { ReportServiceKeyMissingException } from '../exceptions/report-service-key-missing.exception';
import { ReportnameMissingException } from '../exceptions/report-name-missing.exception';
import { ReferenceIdInterface } from '@concepta/ts-core';
import { ReportQueryException } from '../exceptions/report-query.exception';

/**
* Report lookup service
*/
@Injectable()
export class ReportLookupService
extends LookupService<ReportEntityInterface>
implements ReportLookupServiceInterface
{
constructor(
@InjectDynamicRepository(REPORT_MODULE_REPORT_ENTITY_KEY)
repo: Repository<ReportEntityInterface>,
) {
super(repo);
}
async getUniqueReport(
report: Pick<ReportInterface, 'serviceKey' | 'name'>,
queryOptions?: QueryOptionsInterface,
) {
if (!report.serviceKey) {
throw new ReportServiceKeyMissingException();
}
if (!report.name) {
throw new ReportnameMissingException();
}
return this.findOne(
{
where: {
serviceKey: report.serviceKey,
name: report.name,
},
},
queryOptions,
);
}

async getWithFile(
report: ReferenceIdInterface,
queryOptions?: QueryOptionsInterface,
) {
try {
return this.findOne({
where: {
id: report.id,
},
relations: ['file'],
},
queryOptions,
);
} catch (originalError) {
throw new ReportQueryException({ originalError });
}
}
}
40 changes: 40 additions & 0 deletions packages/nestjs-report/src/services/report-mutate.service.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,40 @@
import { InjectDynamicRepository } from '@concepta/nestjs-typeorm-ext';
import { ReportCreatableInterface } from '@concepta/ts-common';
import { MutateService } from '@concepta/typeorm-common';
import { Injectable } from '@nestjs/common';
import { Repository } from 'typeorm';
import { ReportEntityInterface } from '../interfaces/report-entity.interface';

import { ReportUpdatableInterface } from '@concepta/ts-common/src/report/interfaces/report-updatable.interface';
import { ReportCreateDto } from '../dto/report-create.dto';
import { ReportUpdateDto } from '../dto/report-update.dto';
import { ReportMutateServiceInterface } from '../interfaces/report-mutate-service.interface';
import { REPORT_MODULE_REPORT_ENTITY_KEY } from '../report.constants';

/**
* Report mutate service
*/
@Injectable()
export class ReportMutateService
extends MutateService<
ReportEntityInterface,
ReportCreatableInterface,
ReportUpdatableInterface
>
implements ReportMutateServiceInterface
{
protected createDto = ReportCreateDto;
protected updateDto = ReportUpdateDto;

/**
* Constructor
*
* @param repo - instance of the report repo
*/
constructor(
@InjectDynamicRepository(REPORT_MODULE_REPORT_ENTITY_KEY)
repo: Repository<ReportEntityInterface>,
) {
super(repo);
}
}
10 changes: 9 additions & 1 deletion packages/nestjs-report/src/services/report.service.spec.ts
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,8 @@ import { ReportQueryException } from '../exceptions/report-query.exception';
import { ReportEntityInterface } from '../interfaces/report-entity.interface';
import { ReportStrategyService } from './report-strategy.service';
import { ReportService } from './report.service';
import { ReportMutateService } from './report-mutate.service';
import { ReportLookupService } from './report-lookup.service';

const mockReport: ReportEntityInterface = {
id: randomUUID(),
Expand All @@ -28,17 +30,23 @@ const mockReport: ReportEntityInterface = {
const mockReportCreateDto: ReportCreateDto = {
serviceKey: mockReport.serviceKey,
name: mockReport.name,
status: ReportStatusEnum.Processing,
};

describe(ReportService.name, () => {
let reportService: ReportService;
let reportRepo: jest.Mocked<Repository<ReportEntityInterface>>;
let reportStrategyService: jest.Mocked<ReportStrategyService>;
let reportMutateService: ReportMutateService;
let reportLookupService: ReportLookupService;

beforeEach(() => {
reportRepo = createMockRepository();
reportStrategyService = createMockReportStrategyService();
reportService = new ReportService(reportRepo, reportStrategyService);
reportMutateService = new ReportMutateService(reportRepo);
reportLookupService = new ReportLookupService(reportRepo);

reportService = new ReportService(reportStrategyService, reportMutateService, reportLookupService);
reportRepo.create.mockReturnValue(mockReport);
const mockTransactionalEntityManager = {
findOne: jest.fn().mockResolvedValue(null),
Expand Down
Loading

0 comments on commit 3bc3519

Please sign in to comment.