refactor(nestjs): import module

This commit is contained in:
Ahmed Bouhuolia
2025-04-09 10:39:08 +02:00
parent e8f1fedf35
commit d851e5b646
18 changed files with 192 additions and 133 deletions

View File

@@ -1,14 +1,16 @@
import { chain } from 'lodash';
import { Knex } from 'knex';
import { Inject, Injectable } from '@nestjs/common';
import { ERRORS, getUnmappedSheetColumns, readImportFile } from './_utils';
import { ImportFileCommon } from './ImportFileCommon';
import { ImportFileDataTransformer } from './ImportFileDataTransformer';
import { ImportFilePreviewPOJO } from './interfaces';
import { parseSheetData } from './sheet_utils';
import { Injectable } from '@nestjs/common';
import { ResourceService } from '../Resource/ResourceService';
import { UnitOfWork } from '../Tenancy/TenancyDB/UnitOfWork.service';
import { ServiceError } from '../Items/ServiceError';
import { ImportModel } from './models/Import';
import { TenancyContext } from '../Tenancy/TenancyContext.service';
@Injectable()
export class ImportFileProcess {
@@ -17,6 +19,10 @@ export class ImportFileProcess {
private readonly importCommon: ImportFileCommon,
private readonly importParser: ImportFileDataTransformer,
private readonly uow: UnitOfWork,
private readonly tenancyContext: TenancyContext,
@Inject(ImportModel.name)
private readonly importModel: typeof ImportModel,
) {}
/**
@@ -29,7 +35,11 @@ export class ImportFileProcess {
importId: string,
trx?: Knex.Transaction,
): Promise<ImportFilePreviewPOJO> {
const importFile = await Import.query()
const tenant = await this.tenancyContext.getTenant();
const tenantId = tenant.id;
const importFile = await this.importModel
.query()
.findOne('importId', importId)
.where('tenantId', tenantId)
.throwIfNotFound();
@@ -48,28 +58,21 @@ export class ImportFileProcess {
// Runs the importing operation with ability to return errors that will happen.
const [successedImport, failedImport, allData] =
await this.uow.withTransaction(
tenantId,
async (trx: Knex.Transaction) => {
// Prases the sheet json data.
const parsedData = await this.importParser.parseSheetData(
tenantId,
importFile,
resourceFields,
sheetData,
trx,
);
const [successedImport, failedImport] =
await this.importCommon.import(
tenantId,
importFile,
parsedData,
trx,
);
return [successedImport, failedImport, parsedData];
},
trx,
);
await this.uow.withTransaction(async (trx: Knex.Transaction) => {
// Prases the sheet json data.
const parsedData = await this.importParser.parseSheetData(
importFile,
resourceFields,
sheetData,
trx,
);
const [successedImport, failedImport] = await this.importCommon.import(
importFile,
parsedData,
trx,
);
return [successedImport, failedImport, parsedData];
}, trx);
const mapping = importFile.mappingParsed;
const errors = chain(failedImport)
.map((oper) => oper.error)