diff --git a/api/src/modules/import/decorators/xlsm-upload.decorator.ts b/api/src/modules/import/decorators/xlsm-upload.decorator.ts new file mode 100644 index 00000000..a18b093d --- /dev/null +++ b/api/src/modules/import/decorators/xlsm-upload.decorator.ts @@ -0,0 +1,17 @@ +import { UploadedFile, ParseFilePipeBuilder, HttpStatus } from '@nestjs/common'; + +export function UploadXlsm(maxSize: number = 5000000) { + return UploadedFile( + new ParseFilePipeBuilder() + .addFileTypeValidator({ + fileType: 'application/vnd.ms-excel.sheet.macroenabled.12', + }) + .addMaxSizeValidator({ + maxSize: maxSize, + }) + .build({ + errorHttpStatusCode: HttpStatus.BAD_REQUEST, + fileIsRequired: true, + }), + ); +} diff --git a/api/src/modules/import/import.controller.ts b/api/src/modules/import/import.controller.ts index ec1b0da6..8c78dc16 100644 --- a/api/src/modules/import/import.controller.ts +++ b/api/src/modules/import/import.controller.ts @@ -5,12 +5,23 @@ import { UseInterceptors, } from '@nestjs/common'; import { FileInterceptor } from '@nestjs/platform-express'; +import { JwtAuthGuard } from '@api/modules/auth/guards/jwt-auth.guard'; +import { RolesGuard } from '@api/modules/auth/guards/roles.guard'; +import { RequiredRoles } from '@api/modules/auth/decorators/roles.decorator'; +import { ROLES } from '@api/modules/auth/roles.enum'; +import { UploadXlsm } from '@api/modules/import/decorators/xlsm-upload.decorator'; @Controller() +//@UseInterceptors(JwtAuthGuard, RolesGuard) export class ImportController { - @Post() + // TODO: File validation following: + // https://docs.nestjs.com/techniques/file-upload + + @Post('/admin/upload/xlsx') + //@RequiredRoles(ROLES.ADMIN) @UseInterceptors(FileInterceptor('file')) - async uploadFile(@UploadedFile() file: Express.Multer.File): Promise { - return file; + async uploadFile(@UploadXlsm() file: Express.Multer.File): Promise { + const { buffer, ...rest } = file; + return rest; } }