diff --git a/api/src/app.module.ts b/api/src/app.module.ts index 16951d5b..e1da66fe 100644 --- a/api/src/app.module.ts +++ b/api/src/app.module.ts @@ -6,6 +6,7 @@ import { AuthModule } from '@api/modules/auth/auth.module'; import { NotificationsModule } from '@api/modules/notifications/notifications.module'; import { EventsModule } from '@api/modules/events/events.module'; import { AdminModule } from '@api/modules/admin/admin.module'; +import { ImportModule } from '@api/modules/import/import.module'; @Module({ imports: [ @@ -14,6 +15,7 @@ import { AdminModule } from '@api/modules/admin/admin.module'; NotificationsModule, EventsModule, AdminModule, + ImportModule, ], controllers: [AppController], providers: [AppService], diff --git a/api/src/modules/admin/admin.module.ts b/api/src/modules/admin/admin.module.ts index 2f93b121..60d0e54e 100644 --- a/api/src/modules/admin/admin.module.ts +++ b/api/src/modules/admin/admin.module.ts @@ -1,10 +1,11 @@ import { Module } from '@nestjs/common'; import { AdminController } from './admin.controller'; import { AuthModule } from '@api/modules/auth/auth.module'; -import { ImportController } from '@api/modules/import/import.controller'; + +import { ImportModule } from '@api/modules/import/import.module'; @Module({ - imports: [AuthModule], - controllers: [AdminController, ImportController], + imports: [AuthModule, ImportModule], + controllers: [AdminController], }) export class AdminModule {} diff --git a/api/src/modules/import/import.controller.ts b/api/src/modules/import/import.controller.ts index 8c78dc16..5f72f19c 100644 --- a/api/src/modules/import/import.controller.ts +++ b/api/src/modules/import/import.controller.ts @@ -10,10 +10,16 @@ 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'; +import { + ExcelParserInterface, + ExcelParserToken, +} from '@api/modules/import/services/excel-parser.interface'; +import { XlsxParser } from '@api/modules/import/services/xlsx.parser'; @Controller() //@UseInterceptors(JwtAuthGuard, RolesGuard) export class ImportController { + constructor(private readonly parser: XlsxParser) {} // TODO: File validation following: // https://docs.nestjs.com/techniques/file-upload @@ -21,7 +27,6 @@ export class ImportController { //@RequiredRoles(ROLES.ADMIN) @UseInterceptors(FileInterceptor('file')) async uploadFile(@UploadXlsm() file: Express.Multer.File): Promise { - const { buffer, ...rest } = file; - return rest; + return this.parser.parseExcel(file.buffer); } } diff --git a/api/src/modules/import/import.module.ts b/api/src/modules/import/import.module.ts index 097fb15a..b38b1d15 100644 --- a/api/src/modules/import/import.module.ts +++ b/api/src/modules/import/import.module.ts @@ -1,9 +1,13 @@ import { Module } from '@nestjs/common'; import { ImportService } from './import.service'; -import { ImportController } from './import.controller'; +import { MulterModule } from '@nestjs/platform-express'; +import { ImportController } from '@api/modules/import/import.controller'; + +import { XlsxParser } from '@api/modules/import/services/xlsx.parser'; @Module({ - providers: [ImportService], - controllers: [ImportController] + imports: [MulterModule.register({})], + controllers: [ImportController], + providers: [ImportService, XlsxParser], }) export class ImportModule {} diff --git a/api/src/modules/import/services/excel-parser.interface.ts b/api/src/modules/import/services/excel-parser.interface.ts index 331012d3..b2d9b23e 100644 --- a/api/src/modules/import/services/excel-parser.interface.ts +++ b/api/src/modules/import/services/excel-parser.interface.ts @@ -1,3 +1,5 @@ +export const ExcelParserToken = Symbol('ExcelParserInterface'); + export interface ExcelParserInterface { - parseExcel(file: Express.Multer.File): Promise; + parseExcel(data: Buffer): Promise; } diff --git a/api/src/modules/import/services/xlsx.parser.ts b/api/src/modules/import/services/xlsx.parser.ts index e69de29b..263d7bcb 100644 --- a/api/src/modules/import/services/xlsx.parser.ts +++ b/api/src/modules/import/services/xlsx.parser.ts @@ -0,0 +1,18 @@ +import { ExcelParserInterface } from '@api/modules/import/services/excel-parser.interface'; +import { Injectable } from '@nestjs/common'; +import { read, utils, WorkBook, WorkSheet } from 'xlsx'; + +@Injectable() +export class XlsxParser implements ExcelParserInterface { + async parseExcel(buffer: Buffer): Promise { + const workbook: WorkBook = read(buffer); + const sheet: WorkSheet = workbook.Sheets['master_table']; + const data: any[] = utils.sheet_to_json(sheet); + return data; + } + + async sheetNames(file: Express.Multer.File): Promise { + const workbook: WorkBook = read(file.buffer); + return workbook.SheetNames; + } +}