refactor: settings module to Nestjs

This commit is contained in:
Ahmed Bouhuolia
2025-01-07 20:43:31 +02:00
parent 385d84d654
commit abf92ac83f
27 changed files with 2647 additions and 3 deletions

View File

@@ -0,0 +1,81 @@
import { Injectable } from '@nestjs/common';
import {
ITransactionLockingMetaPOJO,
ITransactionsLockingListPOJO,
ITransactionsLockingSchema,
TransactionsLockingGroup,
} from '../types/TransactionsLocking.types';
import { TRANSACTIONS_LOCKING_SCHEMA } from '../constants';
import { TransactionsLockingRepository } from '../TransactionsLockingRepository';
import { TransformerInjectable } from '@/modules/Transformer/TransformerInjectable.service';
import { TransactionsLockingMetaTransformer } from './TransactionsLockingMetaTransformer';
@Injectable()
export class QueryTransactionsLocking {
constructor(
private readonly transactionsLockingRepo: TransactionsLockingRepository,
private readonly transformer: TransformerInjectable,
) {}
/**
* Retrieve transactions locking modules.
* @returns {ITransactionLockingMetaPOJO[]}
*/
public getTransactionsLockingModules = (): Promise<
ITransactionLockingMetaPOJO[]
> => {
const modules = TRANSACTIONS_LOCKING_SCHEMA.map(
(schema: ITransactionsLockingSchema) =>
this.getTransactionsLockingModuleMeta(schema.module),
);
return Promise.all(modules);
};
/**
* Retireve the transactions locking all module.
* @returns {ITransactionLockingMetaPOJO}
*/
public getTransactionsLockingAll =
(): Promise<ITransactionLockingMetaPOJO> => {
return this.getTransactionsLockingModuleMeta(
TransactionsLockingGroup.All,
);
};
/**
* Retrieve the transactions locking module meta.
* @param {number} tenantId -
* @param {TransactionsLockingGroup} module -
* @returns {ITransactionLockingMetaPOJO}
*/
public getTransactionsLockingModuleMeta = (
module: TransactionsLockingGroup,
): Promise<ITransactionLockingMetaPOJO> => {
const meta = this.transactionsLockingRepo.getTransactionsLocking(module);
return this.transformer.transform(
meta,
new TransactionsLockingMetaTransformer(),
{ module },
);
};
/**
* Retrieve transactions locking list.
* @returns {Promise<ITransactionsLockingListPOJO>}
*/
public getTransactionsLockingList =
async (): Promise<ITransactionsLockingListPOJO> => {
// Retrieve the current transactions locking type.
const lockingType =
this.transactionsLockingRepo.getTransactionsLockingType();
const all = await this.getTransactionsLockingAll();
const modules = await this.getTransactionsLockingModules();
return {
lockingType,
all,
modules,
};
};
}

View File

@@ -0,0 +1,83 @@
import { get } from 'lodash';
import { getTransactionsLockingSchemaMeta } from '../constants';
import { Transformer } from '@/modules/Transformer/Transformer';
import { TransactionsLockingGroup } from '../types/TransactionsLocking.types';
export class TransactionsLockingMetaTransformer extends Transformer {
/**
* Include these attributes to sale credit note object.
* @returns {Array}
*/
public includeAttributes = (): string[] => {
return [
'module',
'formattedModule',
'description',
'formattedLockToDate',
'formattedUnlockFromDate',
'formattedUnlockToDate',
];
};
/**
* Module slug.
* @returns {string}
*/
protected module = () => {
return this.options.module;
};
/**
* Formatted module name.
* @returns {string}
*/
protected formattedModule = () => {
return this.options.module === TransactionsLockingGroup.All
? this.context.i18n.t('transactions_locking.module.all_transactions')
: this.context.i18n.t(
get(
getTransactionsLockingSchemaMeta(this.options.module),
'formattedModule',
),
);
};
/**
* Module description.
* @returns {string}
*/
protected description = () => {
return this.options.module === TransactionsLockingGroup.All
? ''
: this.context.i18n.t(
get(
getTransactionsLockingSchemaMeta(this.options.module),
'description',
),
);
};
/**
* Formatted unlock to date.
* @returns {string}
*/
protected formattedUnlockToDate = (item) => {
return item.unlockToDate ? this.formatDate(item.unlockToDate) : '';
};
/**
* Formatted unlock from date.
* @returns {string}
*/
protected formattedUnlockFromDate = (item) => {
return item.unlockFromDate ? this.formatDate(item.unlockFromDate) : '';
};
/**
* Formatted lock to date.
* @returns {string}
*/
protected formattedLockToDate = (item) => {
return item.lockToDate ? this.formatDate(item.lockToDate) : '';
};
}