feat(server): wip import resources

This commit is contained in:
Ahmed Bouhuolia
2024-03-11 20:05:12 +02:00
parent 90b4f3ef6d
commit 4270d66928
16 changed files with 328 additions and 75 deletions

View File

@@ -1,17 +1,22 @@
import { first } from 'lodash';
import { first, values } from 'lodash';
import { Inject, Service } from 'typedi';
import { snakeCase, upperFirst } from 'lodash';
import { ServiceError } from '@/exceptions';
import XLSX from 'xlsx';
import * as R from 'ramda';
import HasTenancyService from '../Tenancy/TenancyService';
import { trimObject } from './_utils';
const fs = require('fs').promises;
import { ERRORS, trimObject } from './_utils';
import ResourceService from '../Resource/ResourceService';
import fs from 'fs/promises';
import { IModelMetaField } from '@/interfaces';
@Service()
export class ImportFileUploadService {
@Inject()
private tenancy: HasTenancyService;
@Inject()
private resourceService: ResourceService;
/**
* Reads the imported file and stores the import file meta under unqiue id.
* @param {number} tenantId -
@@ -27,31 +32,54 @@ export class ImportFileUploadService {
) {
const { Import } = this.tenancy.models(tenantId);
const resourceMeta = this.resourceService.getResourceMeta(
tenantId,
resource
);
// Throw service error if the resource does not support importing.
if (!resourceMeta.importable) {
throw new ServiceError(ERRORS.RESOURCE_NOT_IMPORTABLE);
}
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 columns = this.getColumns(jsonData);
const coumnsStringified = JSON.stringify(columns);
// @todo validate the resource.
const _resource = upperFirst(snakeCase(resource));
const _resource = this.resourceService.resourceToModelName(resource);
const exportFile = await Import.query().insert({
filename,
importId: filename,
resource: _resource,
columns: coumnsStringified,
});
const columns = this.getColumns(jsonData);
const resourceColumns = this.resourceService.getResourceImportableFields(
tenantId,
resource
);
const resourceColumnsTransformeed = Object.entries(resourceColumns).map(
([key, { name }]: [string, IModelMetaField]) => ({ key, name })
);
// @todo return the resource importable columns.
return {
export: exportFile,
columns,
resourceColumns: resourceColumnsTransformeed,
};
}
private getColumns(json: unknown[]) {
/**
* Retrieves the sheet columns from the given sheet data.
* @param {unknown[]} json
* @returns {string[]}
*/
private getColumns(json: unknown[]): string[] {
return R.compose(Object.keys, trimObject, first)(json);
}
}