-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Showing
7 changed files
with
171 additions
and
45 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,4 +1,26 @@ | ||
import { Injectable } from '@nestjs/common'; | ||
import { XlsxParser } from '@api/modules/import/services/xlsx.parser'; | ||
import { EcoSystemDataRepository } from '@api/modules/data/ecosystem-data.repository'; | ||
import { EcosystemProject } from '@api/modules/data/ecosystem-data.entity'; | ||
|
||
@Injectable() | ||
export class ImportService {} | ||
export class ImportService { | ||
constructor( | ||
private readonly xlsxParser: XlsxParser, | ||
private readonly repo: EcoSystemDataRepository, | ||
) {} | ||
|
||
async import(file: Express.Multer.File) { | ||
let data; | ||
try { | ||
data = data = await this.xlsxParser.parseExcel<EcosystemProject>( | ||
file.buffer, | ||
); | ||
await this.repo.insertData(data); | ||
} catch (e) { | ||
console.log(e); | ||
} finally { | ||
return data; | ||
} | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,13 +1,32 @@ | ||
import { ExcelParserInterface } from '@api/modules/import/services/excel-parser.interface'; | ||
import { Injectable } from '@nestjs/common'; | ||
import { read, utils, WorkBook, WorkSheet } from 'xlsx'; | ||
import { ExcelParserInterface } from './excel-parser.interface'; | ||
|
||
@Injectable() | ||
export class XlsxParser implements ExcelParserInterface { | ||
async parseExcel(buffer: Buffer): Promise<any> { | ||
async parseExcel<T>(buffer: Buffer): Promise<T[]> { | ||
const workbook: WorkBook = read(buffer); | ||
const sheet: WorkSheet = workbook.Sheets['master_table']; | ||
const data: any[] = utils.sheet_to_json(sheet, { raw: true, defval: null }); | ||
return data; | ||
const data: T[] = utils.sheet_to_json(sheet, { | ||
raw: true, | ||
defval: null, | ||
}); | ||
return data.map((row) => this.handleCrap<T>(row)); | ||
} | ||
|
||
// TODO: temporal hack to handle stuff, there are values that are No data that could be null in the excel, and missing values like country code or continent | ||
// double check the entity to update it | ||
|
||
private handleCrap<T>(row: T): T { | ||
return Object.fromEntries( | ||
Object.entries(row).map(([key, value]) => [ | ||
key, | ||
value === 'No data' | ||
? null | ||
: typeof value === 'string' && !isNaN(Number(value)) | ||
? Number(value) | ||
: value, | ||
]), | ||
) as T; | ||
} | ||
} |