Skip to content

Commit

Permalink
Merge pull request #456 from andrechristikan/development
Browse files Browse the repository at this point in the history
Development
  • Loading branch information
andrechristikan authored Aug 10, 2023
2 parents b954683 + 8c1a4de commit d568d7a
Show file tree
Hide file tree
Showing 12 changed files with 1,696 additions and 1,223 deletions.
42 changes: 21 additions & 21 deletions package.json
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
{
"name": "ack-nestjs-boilerplate",
"version": "5.4.1",
"version": "5.4.2",
"description": "Ack NestJs Boilerplate",
"repository": {
"type": "git",
Expand Down Expand Up @@ -47,20 +47,20 @@
"rollback": "yarn rollback:setting && yarn rollback:apikey && yarn rollback:user && yarn rollback:role"
},
"dependencies": {
"@aws-sdk/client-s3": "^3.377.0",
"@aws-sdk/client-s3": "^3.388.0",
"@casl/ability": "^6.5.0",
"@faker-js/faker": "^8.0.2",
"@joi/date": "^2.1.0",
"@nestjs/axios": "^3.0.0",
"@nestjs/common": "^10.1.2",
"@nestjs/common": "^10.1.3",
"@nestjs/config": "^3.0.0",
"@nestjs/core": "^10.1.2",
"@nestjs/core": "^10.1.3",
"@nestjs/jwt": "^10.1.0",
"@nestjs/mongoose": "^10.0.1",
"@nestjs/passport": "^10.0.0",
"@nestjs/platform-express": "^10.1.2",
"@nestjs/schedule": "^3.0.1",
"@nestjs/swagger": "^7.1.2",
"@nestjs/platform-express": "^10.1.3",
"@nestjs/schedule": "^3.0.2",
"@nestjs/swagger": "^7.1.8",
"@nestjs/terminus": "^10.0.1",
"@nestjs/throttler": "^4.2.1",
"@types/response-time": "^2.3.5",
Expand All @@ -72,7 +72,7 @@
"helmet": "^7.0.0",
"joi": "^17.9.2",
"moment": "^2.29.4",
"mongoose": "^7.4.1",
"mongoose": "^7.4.2",
"morgan": "^1.10.0",
"nest-winston": "^1.9.3",
"nestjs-command": "^3.1.4",
Expand All @@ -95,35 +95,35 @@
"yarn": "^1.22.19"
},
"devDependencies": {
"@nestjs/cli": "^10.1.10",
"@nestjs/schematics": "^10.0.1",
"@nestjs/testing": "^10.1.2",
"@nestjs/cli": "^10.1.11",
"@nestjs/schematics": "^10.0.2",
"@nestjs/testing": "^10.1.3",
"@types/bcryptjs": "^2.4.2",
"@types/bytes": "^3.1.1",
"@types/cors": "^2.8.13",
"@types/cron": "^2.0.1",
"@types/crypto-js": "^4.1.1",
"@types/express": "^4.17.17",
"@types/jest": "^29.5.3",
"@types/lodash": "^4.14.196",
"@types/lodash": "^4.14.197",
"@types/morgan": "^1.9.4",
"@types/ms": "^0.7.31",
"@types/multer": "^1.4.7",
"@types/node": "^20.4.5",
"@types/node": "^20.4.9",
"@types/passport-google-oauth20": "^2.0.11",
"@types/passport-jwt": "^3.0.9",
"@types/supertest": "^2.0.12",
"@types/ua-parser-js": "^0.7.36",
"@types/uuid": "^9.0.2",
"@typescript-eslint/eslint-plugin": "^6.2.0",
"@typescript-eslint/parser": "^6.2.0",
"cspell": "^6.31.2",
"eslint": "^8.45.0",
"eslint-config-prettier": "^8.8.0",
"eslint-plugin-import": "^2.27.5",
"@typescript-eslint/eslint-plugin": "^6.3.0",
"@typescript-eslint/parser": "^6.3.0",
"cspell": "^6.31.3",
"eslint": "^8.46.0",
"eslint-config-prettier": "^9.0.0",
"eslint-plugin-import": "^2.28.0",
"husky": "^8.0.3",
"jest": "^29.6.1",
"prettier": "^3.0.0",
"jest": "^29.6.2",
"prettier": "^3.0.1",
"supertest": "^6.3.3",
"ts-jest": "^29.1.1",
"ts-loader": "^9.4.4",
Expand Down
1 change: 1 addition & 0 deletions src/common/error/interfaces/error.interface.ts
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,7 @@ export interface IErrors {
export interface IErrorsImport {
row: number;
file?: string;
sheet?: number;
errors: IErrors[];
}

Expand Down
1 change: 1 addition & 0 deletions src/common/file/constants/file.status-code.constant.ts
Original file line number Diff line number Diff line change
Expand Up @@ -5,4 +5,5 @@ export enum ENUM_FILE_STATUS_CODE_ERROR {
FILE_MAX_FILES_ERROR = 5033,
FILE_VALIDATION_DTO_ERROR = 5034,
FILE_NEED_EXTRACT_FIRST_ERROR = 5035,
FILE_VALIDATION_ALL_SHEET_DTO_ERROR = 5036,
}
13 changes: 10 additions & 3 deletions src/common/file/interfaces/file.interface.ts
Original file line number Diff line number Diff line change
@@ -1,6 +1,13 @@
export type IFile = Express.Multer.File;
import { IHelperFileRows } from 'src/common/helper/interfaces/helper.interface';

export type IFileExtract<T = Record<string, any>> = IFile & {
extract: Record<string, any>[];
export type IFile = Omit<Express.Multer.File, 'filename'>;

export type IFileExtract<T> = IFile & {
extract: IHelperFileRows[];
dto?: T[];
};

export type IFileExtractAllSheets<T> = IFile & {
extracts: IHelperFileRows[][];
dto?: T[][];
};
89 changes: 78 additions & 11 deletions src/common/file/pipes/file.extract.pipe.ts
Original file line number Diff line number Diff line change
Expand Up @@ -2,28 +2,33 @@ import { Injectable, UnsupportedMediaTypeException } from '@nestjs/common';
import { PipeTransform } from '@nestjs/common/interfaces';
import { ENUM_FILE_EXCEL_MIME } from 'src/common/file/constants/file.enum.constant';
import { ENUM_FILE_STATUS_CODE_ERROR } from 'src/common/file/constants/file.status-code.constant';
import { IFile, IFileExtract } from 'src/common/file/interfaces/file.interface';
import {
IFile,
IFileExtract,
IFileExtractAllSheets,
} from 'src/common/file/interfaces/file.interface';
import { IHelperFileRows } from 'src/common/helper/interfaces/helper.interface';
import { HelperFileService } from 'src/common/helper/services/helper.file.service';

// only for excel
@Injectable()
export class FileExtractPipe implements PipeTransform {
export class FileExtractPipe<T> implements PipeTransform {
constructor(private readonly helperFileService: HelperFileService) {}

async transform(
value: IFile | IFile[]
): Promise<IFileExtract | IFileExtract[]> {
): Promise<IFileExtract<T> | IFileExtract<T>[]> {
if (!value) {
return;
}

if (Array.isArray(value)) {
const extracts: IFileExtract[] = [];
const extracts: IFileExtract<T>[] = [];

for (const val of value) {
await this.validate(val.mimetype);

const extract: IFileExtract = await this.extract(val);
const extract: IFileExtract<T> = await this.extract(val);
extracts.push(extract);
}

Expand All @@ -33,7 +38,70 @@ export class FileExtractPipe implements PipeTransform {
const file: IFile = value as IFile;
await this.validate(file.mimetype);

return this.extract(file);
const extract: IFileExtract<T> = await this.extract(file);

return extract;
}

async validate(mimetype: string): Promise<void> {
if (
!Object.values(ENUM_FILE_EXCEL_MIME).find(
(val) => val === mimetype.toLowerCase()
)
) {
throw new UnsupportedMediaTypeException({
statusCode: ENUM_FILE_STATUS_CODE_ERROR.FILE_EXTENSION_ERROR,
message: 'file.error.mimeInvalid',
});
}
}

async extract(value: IFile): Promise<IFileExtract<T>> {
const extracts: IHelperFileRows[][] =
this.helperFileService.readExcelFromBuffer(value.buffer, {
sheet: 0,
});

return {
...value,
extract: extracts[0],
};
}
}

// only for excel
@Injectable()
export class FileExtractAllSheetPipe<T> implements PipeTransform {
constructor(private readonly helperFileService: HelperFileService) {}

async transform(
value: IFile | IFile[]
): Promise<IFileExtractAllSheets<T> | IFileExtractAllSheets<T>[]> {
if (!value) {
return;
}

if (Array.isArray(value)) {
const extracts: IFileExtractAllSheets<T>[] = [];

for (const val of value) {
await this.validate(val.mimetype);

const extract: IFileExtractAllSheets<T> = await this.extract(
val
);
extracts.push(extract);
}

return extracts;
}

const file: IFile = value as IFile;
await this.validate(file.mimetype);

const extract: IFileExtractAllSheets<T> = await this.extract(file);

return extract;
}

async validate(mimetype: string): Promise<void> {
Expand All @@ -49,14 +117,13 @@ export class FileExtractPipe implements PipeTransform {
}
}

async extract(value: IFile): Promise<IFileExtract> {
const extract = this.helperFileService.readExcelFromBuffer(
value.buffer
);
async extract(value: IFile): Promise<IFileExtractAllSheets<T>> {
const extracts: IHelperFileRows[][] =
this.helperFileService.readExcelFromBuffer(value.buffer);

return {
...value,
extract,
extracts: extracts,
};
}
}
Loading

0 comments on commit d568d7a

Please sign in to comment.