refactor: items services to Nestjs

This commit is contained in:
Ahmed Bouhuolia
2024-12-15 13:04:41 +02:00
parent 70211980aa
commit 0a112c5655
20 changed files with 897 additions and 18 deletions

View File

@@ -0,0 +1,59 @@
import { I18nService } from 'nestjs-i18n';
import { Transformer } from './Transformer';
import { Injectable } from '@nestjs/common';
import { TenancyContext } from '../Tenancy/TenancyContext.service';
import { TransformerContext } from './Transformer.types';
@Injectable()
export class TransformerInjectable {
constructor(
private readonly tenancyContext: TenancyContext,
private readonly i18n: I18nService,
) {}
/**
* Retrieves the application context of all tenant transformers.
* @returns {TransformerContext}
*/
async getApplicationContext(): Promise<TransformerContext> {
const tenant = await this.tenancyContext.getTenant(true);
const organization = tenant.metadata;
return {
organization,
i18n: this.i18n,
exportAls: {},
};
}
/**
* Retrieves the given tenatn date format.
* @returns {string}
*/
async getTenantDateFormat() {
const tenant = await this.tenancyContext.getTenant(true);
return tenant.metadata.dateFormat;
}
/**
* Transformes the given transformer after inject the tenant context.
* @param {Record<string, any> | Record<string, any>[]} object
* @param {Transformer} transformer
* @param {Record<string, any>} options
* @returns {Record<string, any>}
*/
async transform(
object: Record<string, any> | Record<string, any>[],
transformer: Transformer,
options?: Record<string, any>,
) {
const context = await this.getApplicationContext();
transformer.setContext(context);
const dateFormat = await this.getTenantDateFormat();
transformer.setDateFormat(dateFormat);
transformer.setOptions(options);
return transformer.work(object);
}
}