mirror of
https://github.com/bigcapitalhq/bigcapital.git
synced 2026-02-17 13:20:31 +00:00
refactor: items services to Nestjs
This commit is contained in:
@@ -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);
|
||||
}
|
||||
}
|
||||
Reference in New Issue
Block a user