mirror of
https://github.com/bigcapitalhq/bigcapital.git
synced 2026-02-15 20:30:33 +00:00
WIP
This commit is contained in:
54
packages/server/src/services/Import/ImportFileUpload.ts
Normal file
54
packages/server/src/services/Import/ImportFileUpload.ts
Normal file
@@ -0,0 +1,54 @@
|
||||
import { first } from 'lodash';
|
||||
import { Inject, Service } from 'typedi';
|
||||
import { snakeCase, upperFirst } from 'lodash';
|
||||
import XLSX from 'xlsx';
|
||||
import * as R from 'ramda';
|
||||
import HasTenancyService from '../Tenancy/TenancyService';
|
||||
import { trimObject } from './_utils';
|
||||
const fs = require('fs').promises;
|
||||
|
||||
@Service()
|
||||
export class ImportFileUploadService {
|
||||
@Inject()
|
||||
private tenancy: HasTenancyService;
|
||||
|
||||
/**
|
||||
* Reads the imported file and stores the import file meta under unqiue id.
|
||||
* @param {number} tenantId -
|
||||
* @param {string} filePath -
|
||||
* @param {string} fileName -
|
||||
* @returns
|
||||
*/
|
||||
public async import(
|
||||
tenantId: number,
|
||||
resource: string,
|
||||
filePath: string,
|
||||
filename: string
|
||||
) {
|
||||
const { Import } = this.tenancy.models(tenantId);
|
||||
const buffer = await fs.readFile(filePath);
|
||||
const workbook = XLSX.read(buffer, { type: 'buffer' });
|
||||
|
||||
const firstSheetName = workbook.SheetNames[0];
|
||||
const worksheet = workbook.Sheets[firstSheetName];
|
||||
const jsonData = XLSX.utils.sheet_to_json(worksheet);
|
||||
|
||||
const _resource = upperFirst(snakeCase(resource));
|
||||
|
||||
const exportFile = await Import.query().insert({
|
||||
filename,
|
||||
importId: filename,
|
||||
resource: _resource,
|
||||
});
|
||||
const columns = this.getColumns(jsonData);
|
||||
|
||||
return {
|
||||
...exportFile,
|
||||
columns,
|
||||
};
|
||||
}
|
||||
|
||||
private getColumns(json: unknown[]) {
|
||||
return R.compose(Object.keys, trimObject, first)(json);
|
||||
}
|
||||
}
|
||||
Reference in New Issue
Block a user