feat: wip import resources

This commit is contained in:
Ahmed Bouhuolia
2024-03-15 00:18:41 +02:00
parent 084d9d3d10
commit ab4c0ab7a7
14 changed files with 96 additions and 125 deletions

View File

@@ -1,46 +0,0 @@
import { Inject, Service } from 'typedi';
import { Knex } from 'knex';
import { IAccountCreateDTO } from '@/interfaces';
import { CreateAccount } from '../Accounts/CreateAccount';
@Service()
export class AccountsImportable {
@Inject()
private createAccountService: CreateAccount;
/**
*
* @param {number} tenantId
* @param {IAccountCreateDTO} createAccountDTO
* @returns
*/
public importable(
tenantId: number,
createAccountDTO: IAccountCreateDTO,
trx?: Knex.Transaction
) {
return this.createAccountService.createAccount(
tenantId,
createAccountDTO,
trx
);
}
/**
*
* @param data
* @returns
*/
public transform(data) {
return { ...data };
}
/**
*
* @param data
* @returns
*/
public preTransform(data) {
return { ...data };
}
}

View File

@@ -11,7 +11,7 @@ import {
ImportOperError,
ImportOperSuccess,
} from './interfaces';
import { AccountsImportable } from './AccountsImportable';
import { AccountsImportable } from '../Accounts/AccountsImportable';
import { ServiceError } from '@/exceptions';
import { trimObject } from './_utils';
import { ImportableResources } from './ImportableResources';
@@ -57,12 +57,12 @@ export class ImportFileCommon {
}
/**
*
* Imports the given parsed data to the resource storage through registered importable service.
* @param {number} tenantId -
* @param {string} resourceName - Resource name.
* @param {Record<string, any>} parsedData -
* @param {Knex.Transaction} trx
* @returns
* @param {Record<string, any>} parsedData - Parsed data.
* @param {Knex.Transaction} trx - Knex transaction.
* @returns {Promise<[ImportOperSuccess[], ImportOperError[]]>}
*/
public async import(
tenantId: number,
@@ -77,6 +77,8 @@ export class ImportFileCommon {
const ImportableRegistry = this.importable.registry;
const importable = ImportableRegistry.getImportable(resourceName);
const concurrency = importable.concurrency || 10;
const success: ImportOperSuccess[] = [];
const failed: ImportOperError[] = [];
@@ -108,7 +110,7 @@ export class ImportFileCommon {
failed.push({ index, error });
}
};
await bluebird.map(parsedData, importAsync, { concurrency: 2 });
await bluebird.map(parsedData, importAsync, { concurrency });
return [success, failed];
}
@@ -127,7 +129,7 @@ export class ImportFileCommon {
* @param {number} tenantId
* @param {} importFile
*/
private async deleteImportFile(tenantId: number, importFile: any) {
public async deleteImportFile(tenantId: number, importFile: any) {
const { Import } = this.tenancy.models(tenantId);
// Deletes the import row.

View File

@@ -1,10 +1,9 @@
import { Inject, Service } from 'typedi';
import { Service } from 'typedi';
import * as R from 'ramda';
import { isUndefined, mapValues, get, pickBy, chain } from 'lodash';
import { isUndefined, get, chain } from 'lodash';
import { ImportMappingAttr, ResourceMetaFieldsMap } from './interfaces';
import { parseBoolean } from '@/utils';
import { trimObject } from './_utils';
import ResourceService from '../Resource/ResourceService';
@Service()
export class ImportFileDataTransformer {

View File

@@ -27,7 +27,7 @@ export class ImportFileUploadService {
* @param {string} resource - Resource name.
* @param {string} filePath - File path.
* @param {string} fileName - File name.
* @returns
* @returns {Promise<ImportFileUploadPOJO>}
*/
public async import(
tenantId: number,

View File

@@ -52,7 +52,7 @@ export class ImportResourceApplication {
* Preview the mapped results before process importing.
* @param {number} tenantId
* @param {number} importId
* @returns {}
* @returns {Promise<ImportFilePreviewPOJO>}
*/
public async preview(tenantId: number, importId: number) {
return this.ImportFilePreviewService.preview(tenantId, importId);
@@ -62,7 +62,7 @@ export class ImportResourceApplication {
* Process the import file sheet through service for creating entities.
* @param {number} tenantId
* @param {number} importId
* @returns {Promise<void>}
* @returns {Promise<ImportFilePreviewPOJO>}
*/
public async process(tenantId: number, importId: number) {
return this.importProcessService.import(tenantId, importId);

View File

@@ -1,7 +0,0 @@
import { Service } from "typedi";
@Service()
export class ImportResourceRegistry {
}

View File

@@ -1,7 +1,23 @@
import { Knex } from 'knex';
export abstract class Importable {
/**
*
* @param {number} tenantId
* @param {any} createDTO
* @param {Knex.Transaction} trx
*/
public importable(tenantId: number, createDTO: any, trx?: Knex.Transaction) {
throw new Error(
'The `importable` function is not defined in service importable.'
);
}
abstract class importable {
}
/**
* Concurrency controlling of the importing process.
* @returns {number}
*/
public get concurrency() {
return 10;
}
}

View File

@@ -1,13 +1,18 @@
import { camelCase, upperFirst } from 'lodash';
import { Importable } from './Importable';
export class ImportableRegistry {
private static instance: ImportableRegistry;
private importables: Record<string, any>;
private importables: Record<string, Importable>;
private constructor() {
this.importables = {};
}
/**
* Gets singleton instance of registry.
* @returns {ImportableRegistry}
*/
public static getInstance(): ImportableRegistry {
if (!ImportableRegistry.instance) {
ImportableRegistry.instance = new ImportableRegistry();
@@ -15,12 +20,22 @@ export class ImportableRegistry {
return ImportableRegistry.instance;
}
public registerImportable(resource: string, importable: any): void {
/**
* Registers the given importable service.
* @param {string} resource
* @param {Importable} importable
*/
public registerImportable(resource: string, importable: Importable): void {
const _resource = this.sanitizeResourceName(resource);
this.importables[_resource] = importable;
}
public getImportable(name: string): any {
/**
* Retrieves the importable service instance of the given resource name.
* @param {string} name
* @returns {Importable}
*/
public getImportable(name: string): Importable {
const _name = this.sanitizeResourceName(name);
return this.importables[_name];
}

View File

@@ -1,12 +1,11 @@
import Container, { Service } from 'typedi';
import { AccountsImportable } from './AccountsImportable';
import { AccountsImportable } from '../Accounts/AccountsImportable';
import { ImportableRegistry } from './ImportableRegistry';
@Service()
export class ImportableResources {
private static registry: ImportableRegistry;
constructor() {
this.boot();
}