Skip to content

Commit

Permalink
parse buffer to json
Browse files Browse the repository at this point in the history
  • Loading branch information
alexeh committed Sep 29, 2024
1 parent 5c3bbd0 commit 88b07fc
Show file tree
Hide file tree
Showing 6 changed files with 41 additions and 9 deletions.
2 changes: 2 additions & 0 deletions api/src/app.module.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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: [
Expand All @@ -14,6 +15,7 @@ import { AdminModule } from '@api/modules/admin/admin.module';
NotificationsModule,
EventsModule,
AdminModule,
ImportModule,
],
controllers: [AppController],
providers: [AppService],
Expand Down
7 changes: 4 additions & 3 deletions api/src/modules/admin/admin.module.ts
Original file line number Diff line number Diff line change
@@ -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 {}
9 changes: 7 additions & 2 deletions api/src/modules/import/import.controller.ts
Original file line number Diff line number Diff line change
Expand Up @@ -10,18 +10,23 @@ 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

@Post('/admin/upload/xlsx')
//@RequiredRoles(ROLES.ADMIN)
@UseInterceptors(FileInterceptor('file'))
async uploadFile(@UploadXlsm() file: Express.Multer.File): Promise<any> {
const { buffer, ...rest } = file;
return rest;
return this.parser.parseExcel(file.buffer);
}
}
10 changes: 7 additions & 3 deletions api/src/modules/import/import.module.ts
Original file line number Diff line number Diff line change
@@ -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 {}
4 changes: 3 additions & 1 deletion api/src/modules/import/services/excel-parser.interface.ts
Original file line number Diff line number Diff line change
@@ -1,3 +1,5 @@
export const ExcelParserToken = Symbol('ExcelParserInterface');

export interface ExcelParserInterface {
parseExcel(file: Express.Multer.File): Promise<any>;
parseExcel(data: Buffer): Promise<any>;
}
18 changes: 18 additions & 0 deletions api/src/modules/import/services/xlsx.parser.ts
Original file line number Diff line number Diff line change
@@ -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<any> {
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<string[]> {
const workbook: WorkBook = read(file.buffer);
return workbook.SheetNames;
}
}

0 comments on commit 88b07fc

Please sign in to comment.