refactor(nestjs): export and import module

This commit is contained in:
Ahmed Bouhuolia
2025-04-09 18:35:17 +02:00
parent d851e5b646
commit ab49113d5a
38 changed files with 2403 additions and 117 deletions

View File

@@ -2,8 +2,8 @@ import * as Yup from 'yup';
import * as moment from 'moment';
import * as R from 'ramda';
import { Knex } from 'knex';
import fs from 'fs/promises';
import path from 'path';
import * as fs from 'fs/promises';
import * as path from 'path';
import {
defaultTo,
upperFirst,
@@ -18,7 +18,7 @@ import {
split,
last,
} from 'lodash';
import pluralize from 'pluralize';
import * as pluralize from 'pluralize';
import { ResourceMetaFieldsMap } from './interfaces';
import { multiNumberParse } from '@/utils/multi-number-parse';
import { ServiceError } from '../Items/ServiceError';
@@ -70,13 +70,13 @@ export const convertFieldsToYupValidation = (fields: ResourceMetaFieldsMap) => {
if (!isUndefined(field.minLength)) {
fieldSchema = fieldSchema.min(
field.minLength,
`Minimum length is ${field.minLength} characters`
`Minimum length is ${field.minLength} characters`,
);
}
if (!isUndefined(field.maxLength)) {
fieldSchema = fieldSchema.max(
field.maxLength,
`Maximum length is ${field.maxLength} characters`
`Maximum length is ${field.maxLength} characters`,
);
}
} else if (field.fieldType === 'number') {
@@ -106,7 +106,7 @@ export const convertFieldsToYupValidation = (fields: ResourceMetaFieldsMap) => {
return true;
}
return moment(val, 'YYYY-MM-DD', true).isValid();
}
},
);
} else if (field.fieldType === 'url') {
fieldSchema = fieldSchema.url();
@@ -150,12 +150,12 @@ const parseFieldName = (fieldName: string, field: IModelMetaField) => {
*/
export const getUnmappedSheetColumns = (columns, mapping) => {
return columns.filter(
(column) => !mapping.some((map) => map.from === column)
(column) => !mapping.some((map) => map.from === column),
);
};
export const sanitizeResourceName = (resourceName: string) => {
return upperFirst(camelCase(pluralize.singular(resourceName)));
return upperFirst(camelCase(pluralize(resourceName, 1)));
};
export const getSheetColumns = (sheetData: unknown[]) => {
@@ -171,11 +171,11 @@ export const getSheetColumns = (sheetData: unknown[]) => {
*/
export const getUniqueImportableValue = (
importableFields: { [key: string]: IModelMetaField2 },
objectDTO: Record<string, any>
objectDTO: Record<string, any>,
) => {
const uniqueImportableValue = pickBy(
importableFields,
(field) => field.unique
(field) => field.unique,
);
const uniqueImportableKeys = Object.keys(uniqueImportableValue);
const uniqueImportableKey = first(uniqueImportableKeys);
@@ -255,7 +255,7 @@ export const getResourceColumns = (resourceColumns: {
(group: string) =>
([fieldKey, { name, importHint, required, order, ...field }]: [
string,
IModelMetaField2
IModelMetaField2,
]) => {
const extra: Record<string, any> = {};
const key = fieldKey;
@@ -300,7 +300,7 @@ export const valueParser =
// Parses the enumeration value.
} else if (field.fieldType === 'enumeration') {
const option = get(field, 'options', []).find(
(option) => option.label?.toLowerCase() === value?.toLowerCase()
(option) => option.label?.toLowerCase() === value?.toLowerCase(),
);
_value = get(option, 'key');
// Parses the numeric value.
@@ -356,7 +356,7 @@ export const parseKey = R.curry(
}
}
return _key;
}
},
);
/**
@@ -399,11 +399,11 @@ export const getFieldKey = (input: string) => {
export function aggregate(
input: Array<any>,
comparatorAttr: string,
groupOn: string
groupOn: string,
): Array<Record<string, any>> {
return input.reduce((acc, curr) => {
const existingEntry = acc.find(
(entry) => entry[comparatorAttr] === curr[comparatorAttr]
(entry) => entry[comparatorAttr] === curr[comparatorAttr],
);
if (existingEntry) {