import { Service, Inject } from 'typedi'; import { isNull } from 'lodash'; import HasTenancyService from '@/services/Tenancy/TenancyService'; import { TenantMetadata } from '@/system/models'; import { Transformer } from './Transformer'; @Service() export class TransformerInjectable { @Inject() private tenancy: HasTenancyService; /** * Retrieves the application context of all tenant transformers. * @param {number} tenantId * @returns {} */ async getApplicationContext(tenantId: number) { const i18n = this.tenancy.i18n(tenantId); const organization = await TenantMetadata.query().findOne({ tenantId }); return { organization, i18n, }; } /** * Retrieves the given tenatn date format. * @param {number} tenantId * @returns {string} */ async getTenantDateFormat(tenantId: number) { const metadata = await TenantMetadata.query().findOne('tenantId', tenantId); return metadata.dateFormat; } /** * Transformes the given transformer after inject the tenant context. * @param {number} tenantId * @param {Record | Record[]} object * @param {Transformer} transformer * @param {Record} options * @returns {Record} */ async transform( tenantId: number, object: Record | Record[], transformer: Transformer, options?: Record ) { if (!isNull(tenantId)) { const context = await this.getApplicationContext(tenantId); transformer.setContext(context); const dateFormat = await this.getTenantDateFormat(tenantId); transformer.setDateFormat(dateFormat); } transformer.setOptions(options); return transformer.work(object); } }