mirror of
https://github.com/bigcapitalhq/bigcapital.git
synced 2026-02-18 13:50:31 +00:00
feat: wip import resources
This commit is contained in:
@@ -1,15 +1,16 @@
|
||||
import { Inject, Service } from 'typedi';
|
||||
import { Knex } from 'knex';
|
||||
import { IAccountCreateDTO } from '@/interfaces';
|
||||
import { CreateAccount } from '../Accounts/CreateAccount';
|
||||
import { CreateAccount } from './CreateAccount';
|
||||
import { Importable } from '../Import/Importable';
|
||||
|
||||
@Service()
|
||||
export class AccountsImportable {
|
||||
export class AccountsImportable extends Importable {
|
||||
@Inject()
|
||||
private createAccountService: CreateAccount;
|
||||
|
||||
/**
|
||||
*
|
||||
* Importing to account service.
|
||||
* @param {number} tenantId
|
||||
* @param {IAccountCreateDTO} createAccountDTO
|
||||
* @returns
|
||||
@@ -27,20 +28,10 @@ export class AccountsImportable {
|
||||
}
|
||||
|
||||
/**
|
||||
*
|
||||
* @param data
|
||||
* @returns
|
||||
* Concurrrency controlling of the importing process.
|
||||
* @returns {number}
|
||||
*/
|
||||
public transform(data) {
|
||||
return { ...data };
|
||||
}
|
||||
|
||||
/**
|
||||
*
|
||||
* @param data
|
||||
* @returns
|
||||
*/
|
||||
public preTransform(data) {
|
||||
return { ...data };
|
||||
public get concurrency() {
|
||||
return 1;
|
||||
}
|
||||
}
|
||||
@@ -1,33 +0,0 @@
|
||||
import { DATATYPES_LENGTH } from '@/data/DataTypes';
|
||||
import { IsInt, IsOptional, IsString, Length, Min, Max, IsNotEmpty } from 'class-validator';
|
||||
|
||||
export class AccountDTOSchema {
|
||||
@IsString()
|
||||
@Length(3, DATATYPES_LENGTH.STRING)
|
||||
@IsNotEmpty()
|
||||
name: string;
|
||||
|
||||
@IsString()
|
||||
@IsOptional()
|
||||
@Length(3, 6)
|
||||
code?: string;
|
||||
|
||||
@IsOptional()
|
||||
currencyCode?: string;
|
||||
|
||||
@IsString()
|
||||
@Length(3, DATATYPES_LENGTH.STRING)
|
||||
@IsNotEmpty()
|
||||
accountType: string;
|
||||
|
||||
@IsString()
|
||||
@IsOptional()
|
||||
@Length(0, DATATYPES_LENGTH.TEXT)
|
||||
description?: string;
|
||||
|
||||
@IsInt()
|
||||
@IsOptional()
|
||||
@Min(0)
|
||||
@Max(DATATYPES_LENGTH.INT_10)
|
||||
parentAccountId?: number;
|
||||
}
|
||||
@@ -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.
|
||||
|
||||
@@ -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 {
|
||||
|
||||
@@ -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,
|
||||
|
||||
@@ -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);
|
||||
|
||||
@@ -1,7 +0,0 @@
|
||||
import { Service } from "typedi";
|
||||
|
||||
|
||||
@Service()
|
||||
export class ImportResourceRegistry {
|
||||
|
||||
}
|
||||
@@ -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;
|
||||
}
|
||||
}
|
||||
|
||||
@@ -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];
|
||||
}
|
||||
|
||||
@@ -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();
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user