Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

File and Report module #263

Merged
merged 18 commits into from
Sep 24, 2024
Merged
Show file tree
Hide file tree
Changes from 2 commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
15 changes: 15 additions & 0 deletions packages/nestjs-report/README.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,15 @@
# Rockets NestJS Report Manager

Manage reports for several components using one module.

## Project

[![NPM Latest](https://img.shields.io/npm/v/@concepta/nestjs-report)](https://www.npmjs.com/package/@concepta/nestjs-report)
[![NPM Downloads](https://img.shields.io/npm/dw/@conceptadev/nestjs-report)](https://www.npmjs.com/package/@concepta/nestjs-report)
[![GH Last Commit](https://img.shields.io/github/last-commit/conceptadev/rockets?logo=github)](https://github.com/conceptadev/rockets)
[![GH Contrib](https://img.shields.io/github/contributors/conceptadev/rockets?logo=github)](https://github.com/conceptadev/rockets/graphs/contributors)
[![NestJS Dep](https://img.shields.io/github/package-json/dependency-version/conceptadev/rockets/@nestjs/common?label=NestJS&logo=nestjs&reportname=packages%2Fnestjs-core%2Fpackage.json)](https://www.npmjs.com/package/@nestjs/common)

## Installation

`yarn add @concepta/nestjs-report`
36 changes: 36 additions & 0 deletions packages/nestjs-report/package.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,36 @@
{
"name": "@concepta/nestjs-report",
"version": "5.0.0-alpha.3",
"main": "dist/index.js",
"types": "dist/index.d.ts",
"license": "BSD-3-Clause",
"publishConfig": {
"access": "public"
},
"reports": [
"dist/**/!(*.spec|*.e2e-spec|*.fixture).{js,d.ts}"
],
"dependencies": {
"@concepta/nestjs-common": "^5.0.0-alpha.3",
"@concepta/nestjs-exception": "^5.0.0-alpha.3",
"@concepta/nestjs-file": "^5.0.0-alpha.3",
"@concepta/nestjs-typeorm-ext": "^5.0.0-alpha.3",
"@concepta/ts-common": "^5.0.0-alpha.3",
"@concepta/ts-core": "^5.0.0-alpha.3",
"@concepta/typeorm-common": "^5.0.0-alpha.3",
"@nestjs/common": "^10.4.1",
"@nestjs/config": "^3.2.3",
"@nestjs/swagger": "^7.4.0"
},
"devDependencies": {
"@concepta/nestjs-user": "^5.0.0-alpha.3",
"@nestjs/testing": "^10.4.1",
"jest-mock-extended": "^2.0.9"
},
"peerDependencies": {
"class-transformer": "*",
"class-validator": "*",
"rxjs": "^7.1.0",
"typeorm": "^0.3.0"
}
}
21 changes: 21 additions & 0 deletions packages/nestjs-report/src/__fixtures__/aws-storage.service.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,21 @@
import { FileInterface } from '@concepta/ts-common';
import { FileStorageServiceInterface } from '@concepta/nestjs-file';

import {
AWS_KEY_FIXTURE,
DOWNLOAD_URL_FIXTURE,
UPLOAD_URL_FIXTURE,
} from './constants.fixture';

export class AwsStorageService implements FileStorageServiceInterface {
KEY: string = AWS_KEY_FIXTURE;

getUploadUrl(_file: FileInterface): string {
return UPLOAD_URL_FIXTURE;
}

getDownloadUrl(_file: FileInterface): string {
// make a call to aws to get signed download url
return DOWNLOAD_URL_FIXTURE;
}
}
6 changes: 6 additions & 0 deletions packages/nestjs-report/src/__fixtures__/constants.fixture.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,6 @@
export const AWS_KEY_FIXTURE = 'my-aws';
export const REPORT_KEY_FIXTURE = 'my-report';
export const REPORT_SHORT_DELAY_KEY_FIXTURE = 'my-report-short-delay';
export const DOWNLOAD_URL_FIXTURE = 'https://aws-storage.com/downloaded';
export const UPLOAD_URL_FIXTURE = 'https://aws-storage.com/upload';
export const REPORT_NAME_FIXTURE = 'test.pdf';
Original file line number Diff line number Diff line change
@@ -0,0 +1,10 @@
import { FileSqliteEntity } from '@concepta/nestjs-file';
import { Entity, OneToOne } from 'typeorm';
import { ReportEntityFixture } from '../report/report-entity.fixture';
import { ReportEntityInterface } from '../../interfaces/report-entity.interface';

@Entity()
export class FileEntityFixture extends FileSqliteEntity {
@OneToOne(() => ReportEntityFixture, (report) => report.file)
report!: ReportEntityInterface;
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,47 @@
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
implements ReportGeneratorServiceInterface
{
constructor(
@Inject(FileService)
private readonly fileService: FileService,
) {}

KEY: string = REPORT_SHORT_DELAY_KEY_FIXTURE;

generateTimeout: number = 100;

async getDownloadUrl(report: ReportEntityInterface): Promise<string> {
const file = await this.fileService.fetch({ id: report.id });
return file.downloadUrl || '';
}

async generate(
report: ReportEntityInterface,
): Promise<ReportGeneratorResultInterface> {
const file = await this.fileService.push({
fileName: report.name,
// TODO: should i have contenType on reports as well?
contentType: 'application/pdf',
serviceKey: AWS_KEY_FIXTURE,
});

await delay(200);
return {
id: report.id,
status: ReportStatusEnum.Complete,
file,
} as unknown as ReportGeneratorResultInterface;
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,45 @@
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
{
constructor(
@Inject(FileService)
private readonly fileService: FileService,
) {}

KEY: string = REPORT_KEY_FIXTURE;
generateTimeout: number = 60000;

async getDownloadUrl(report: ReportEntityInterface): Promise<string> {
if (!report?.file?.id) return '';
const file = await this.fileService.fetch({ id: report.file.id });
return file.downloadUrl || '';
}

async generate(
report: ReportEntityInterface,
): Promise<ReportGeneratorResultInterface> {
const file = await this.fileService.push({
fileName: report.name,
// TODO: should i have contenType on reports as well?
contentType: 'application/pdf',
serviceKey: AWS_KEY_FIXTURE,
});

// Logic to generate file
// PUT file

return {
id: report.id,
status: ReportStatusEnum.Complete,
file,
} as unknown as ReportGeneratorResultInterface;
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,10 @@
import { Global, Module } from '@nestjs/common';
import { MyReportGeneratorService } from './my-report-generator.service';
import { MyReportGeneratorShortDelayService } from './my-report-generator-short-delay.service';

@Global()
@Module({
providers: [MyReportGeneratorService, MyReportGeneratorShortDelayService],
exports: [MyReportGeneratorService, MyReportGeneratorShortDelayService],
})
export class ReportGeneratorModuleFixture {}
Original file line number Diff line number Diff line change
@@ -0,0 +1,11 @@
import { FileEntityInterface } from '@concepta/nestjs-file';
import { Entity, JoinColumn, OneToOne } from 'typeorm';
import { ReportSqliteEntity } from '../../entities/report-sqlite.entity';
import { FileEntityFixture } from '../file/file-entity.fixture';

@Entity()
export class ReportEntityFixture extends ReportSqliteEntity {
@OneToOne(() => FileEntityFixture, (file) => file.report)
@JoinColumn()
file!: FileEntityInterface;
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,9 @@
import { Entity, OneToOne } from 'typeorm';
import { UserSqliteEntity } from '@concepta/nestjs-user';
import { ReportEntityFixture } from '../../report/report-entity.fixture';

@Entity()
export class UserEntityFixture extends UserSqliteEntity {
@OneToOne(() => ReportEntityFixture)
document!: ReportEntityFixture;
}
5 changes: 5 additions & 0 deletions packages/nestjs-report/src/__fixtures__/user/user.fixture.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
export const UserFixture = {
id: 'abc',
email: '[email protected]',
username: '[email protected]',
};
15 changes: 15 additions & 0 deletions packages/nestjs-report/src/config/report-default.config.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,15 @@
import { registerAs } from '@nestjs/config';
import { REPORT_MODULE_DEFAULT_SETTINGS_TOKEN } from '../report.constants';
import { ReportSettingsInterface } from '../interfaces/report-settings.interface';

/**
* Default configuration for report module.
*/
export const reportDefaultConfig = registerAs(
REPORT_MODULE_DEFAULT_SETTINGS_TOKEN,
(): ReportSettingsInterface => ({
generateTimeout: process.env?.REPORT_MODULE_TIMEOUT_IN_SECONDS
? Number(process.env.REPORT_MODULE_TIMEOUT_IN_SECONDS)
: 60000,
}),
);
12 changes: 12 additions & 0 deletions packages/nestjs-report/src/dto/report-create.dto.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,12 @@
import { Exclude } from 'class-transformer';
import { PickType } from '@nestjs/swagger';
import { ReportCreatableInterface } from '@concepta/ts-common';
import { ReportDto } from './report.dto';

/**
* Report Create DTO
*/
@Exclude()
export class ReportCreateDto
extends PickType(ReportDto, ['serviceKey', 'name'] as const)
implements ReportCreatableInterface {}
71 changes: 71 additions & 0 deletions packages/nestjs-report/src/dto/report.dto.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,71 @@
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';

/**
* Report DTO
*/
@Exclude()
export class ReportDto
extends CommonEntityDto
implements ReportEntityInterface
{
/**
* Storage provider key
*/
@Expose()
@ApiProperty({
type: 'string',
description: 'serviceKey of the report',
})
@IsString()
serviceKey = '';

@Expose()
@ApiProperty({
type: 'string',
description: 'Name of the report',
})
@IsString()
name = '';

@Expose()
@ApiProperty({
type: 'string',
description: 'Error message for when tried to generate report',
})
@IsString()
errorMessage = '';

@Expose()
@ApiProperty({
enum: ReportStatusEnum,
enumName: 'ReportStatusEnum',
description: 'Status of the report',
})
@IsEnum(ReportStatusEnum)
status = ReportStatusEnum.Processing;

// TODO: maybe we do not need this, since we gonna have the file reference
MrMaz marked this conversation as resolved.
Show resolved Hide resolved
@Expose()
@ApiProperty({
type: 'string',
description: 'Dynamic download URL for the report',
})
@IsString()
@IsOptional()
downloadUrl = '';

@Expose()
@ApiProperty({
type: ReferenceIdDto,
description: 'The file of the report',
})
@Type(() => ReferenceIdDto)
@ValidateNested()
file: ReferenceIdInterface = { id: '' };
}
32 changes: 32 additions & 0 deletions packages/nestjs-report/src/entities/report-postgres.entity.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,32 @@
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';

/**
* Report Postgres Entity
*/
@Entity()
@Unique(['serviceKey', 'name'])
export class ReportPostgresEntity
extends CommonPostgresEntity
implements ReportEntityInterface
{
@Column()
serviceKey!: string;

@Column({ type: 'citext' })
name!: string;

@Column({
type: 'enum',
enum: ReportStatusEnum,
})
status!: ReportStatusEnum;

@Column({ nullable: true })
errorMessage: string | null = null;

file!: ReferenceIdInterface;
}
32 changes: 32 additions & 0 deletions packages/nestjs-report/src/entities/report-sqlite.entity.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,32 @@
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';

/**
* Report Sqlite Entity
*/
@Entity()
@Unique(['name', 'serviceKey'])
export class ReportSqliteEntity
extends CommonSqliteEntity
implements ReportEntityInterface
{
@Column()
serviceKey!: string;

@Column({ collation: 'NOCASE' })
name!: string;

@Column({
type: 'text',
enum: ReportStatusEnum,
})
status!: ReportStatusEnum;

@Column({ nullable: true })
errorMessage!: string | null;

file!: ReferenceIdInterface;
}
Loading
Loading