fix: bugs sprint

This commit is contained in:
Ahmed Bouhuolia
2025-12-18 13:48:12 +02:00
parent 17651e0768
commit 636d206b0e
9 changed files with 86 additions and 71 deletions

View File

@@ -18,7 +18,7 @@ import { CurrencyParsingDTOs } from './_constants';
export class ImportFileDataTransformer {
constructor(
private readonly resource: ResourceService,
) {}
) { }
/**
* Parses the given sheet data before passing to the service layer.

View File

@@ -6,7 +6,7 @@ import { ContextIdFactory, ModuleRef } from '@nestjs/core';
@Injectable()
export class ImportableRegistry {
constructor(private readonly moduleRef: ModuleRef) {}
constructor(private readonly moduleRef: ModuleRef) { }
/**
* Retrieves the importable service instance of the given resource name.
* @param {string} name
@@ -15,6 +15,12 @@ export class ImportableRegistry {
public async getImportable(name: string) {
const _name = this.sanitizeResourceName(name);
const importable = getImportableService(_name);
if (!importable) {
throw new Error(
`No importable service found for resource "${_name}". Make sure the resource has an @ImportableService decorator registered.`,
);
}
const contextId = ContextIdFactory.create();
const importableInstance = await this.moduleRef.resolve(importable, contextId, {

View File

@@ -253,28 +253,28 @@ export const getResourceColumns = (resourceColumns: {
}) => {
const mapColumn =
(group: string) =>
([fieldKey, { name, importHint, required, order, ...field }]: [
string,
IModelMetaField2,
]) => {
const extra: Record<string, any> = {};
const key = fieldKey;
([fieldKey, { name, importHint, required, order, ...field }]: [
string,
IModelMetaField2,
]) => {
const extra: Record<string, any> = {};
const key = fieldKey;
if (group) {
extra.group = group;
}
if (field.fieldType === 'collection') {
extra.fields = mapColumns(field.fields, key);
}
return {
key,
name,
required,
hint: importHint,
order,
...extra,
if (group) {
extra.group = group;
}
if (field.fieldType === 'collection') {
extra.fields = mapColumns(field.fields, key);
}
return {
key,
name,
required,
hint: importHint,
order,
...extra,
};
};
};
const sortColumn = (a, b) =>
a.order && b.order ? a.order - b.order : a.order ? -1 : b.order ? 1 : 0;
@@ -287,49 +287,49 @@ export const getResourceColumns = (resourceColumns: {
// Prases the given object value based on the field key type.
export const valueParser =
(fields: ResourceMetaFieldsMap, tenantModels: any, trx?: Knex.Transaction) =>
async (value: any, key: string, group = '') => {
let _value = value;
async (value: any, key: string, group = '') => {
let _value = value;
const fieldKey = key.includes('.') ? key.split('.')[0] : key;
const field = group ? fields[group]?.fields[fieldKey] : fields[fieldKey];
const fieldKey = key.includes('.') ? key.split('.')[0] : key;
const field = group ? fields[group]?.fields[fieldKey] : fields[fieldKey];
// Parses the boolean value.
if (field.fieldType === 'boolean') {
_value = parseBoolean(value);
// Parses the boolean value.
if (field.fieldType === 'boolean') {
_value = parseBoolean(value);
// Parses the enumeration value.
} else if (field.fieldType === 'enumeration') {
const option = get(field, 'options', []).find(
(option) => option.label?.toLowerCase() === value?.toLowerCase(),
);
_value = get(option, 'key');
// Parses the numeric value.
} else if (field.fieldType === 'number') {
_value = multiNumberParse(value);
// Parses the relation value.
} else if (field.fieldType === 'relation') {
const RelationModel = tenantModels[field.relationModel];
// Parses the enumeration value.
} else if (field.fieldType === 'enumeration') {
const option = get(field, 'options', []).find(
(option) => option.label?.toLowerCase() === value?.toLowerCase(),
);
_value = get(option, 'key');
// Parses the numeric value.
} else if (field.fieldType === 'number') {
_value = multiNumberParse(value);
// Parses the relation value.
} else if (field.fieldType === 'relation') {
const RelationModel = tenantModels[field.relationModel];
if (!RelationModel) {
throw new Error(`The relation model of ${key} field is not exist.`);
}
const relationQuery = RelationModel.query(trx);
const relationKeys = castArray(field?.relationImportMatch);
if (!RelationModel) {
throw new Error(`The relation model of ${key} field is not exist.`);
}
const relationQuery = RelationModel.query(trx);
const relationKeys = castArray(field?.relationImportMatch);
relationQuery.where(function () {
relationKeys.forEach((relationKey: string) => {
this.orWhereRaw('LOWER(??) = LOWER(?)', [relationKey, value]);
relationQuery.where(function () {
relationKeys.forEach((relationKey: string) => {
this.orWhereRaw('LOWER(??) = LOWER(?)', [relationKey, value]);
});
});
});
const result = await relationQuery.first();
_value = get(result, 'id');
} else if (field.fieldType === 'collection') {
const ObjectFieldKey = key.includes('.') ? key.split('.')[1] : key;
const _valueParser = valueParser(fields, tenantModels);
_value = await _valueParser(value, ObjectFieldKey, fieldKey);
}
return _value;
};
const result = await relationQuery.first();
_value = get(result, 'id');
} else if (field.fieldType === 'collection') {
const ObjectFieldKey = key.includes('.') ? key.split('.')[1] : key;
const _valueParser = valueParser(fields, tenantModels);
_value = await _valueParser(value, ObjectFieldKey, fieldKey);
}
return _value;
};
/**
* Parses the field key and detarmines the key path.