mirror of
https://github.com/bigcapitalhq/bigcapital.git
synced 2026-02-15 20:30:33 +00:00
add server to monorepo.
This commit is contained in:
197
packages/server/src/lib/Transformer/Transformer.ts
Normal file
197
packages/server/src/lib/Transformer/Transformer.ts
Normal file
@@ -0,0 +1,197 @@
|
||||
import moment from 'moment';
|
||||
import * as R from 'ramda';
|
||||
import { includes, isFunction, isObject, isUndefined, omit } from 'lodash';
|
||||
import { formatNumber } from 'utils';
|
||||
|
||||
export class Transformer {
|
||||
public context: any;
|
||||
public options: Record<string, any>;
|
||||
|
||||
/**
|
||||
* Includeded attributes.
|
||||
* @returns {string[]}
|
||||
*/
|
||||
public includeAttributes = (): string[] => {
|
||||
return [];
|
||||
};
|
||||
|
||||
/**
|
||||
* Exclude attributes.
|
||||
* @returns {string[]}
|
||||
*/
|
||||
public excludeAttributes = (): string[] => {
|
||||
return [];
|
||||
};
|
||||
|
||||
/**
|
||||
* Detarmines whether to exclude all attributes except the include attributes.
|
||||
* @returns {boolean}
|
||||
*/
|
||||
public isExcludeAllAttributes = () => {
|
||||
return includes(this.excludeAttributes(), '*');
|
||||
};
|
||||
|
||||
/**
|
||||
*
|
||||
* @param object
|
||||
*/
|
||||
transform = (object: any) => {
|
||||
return object;
|
||||
};
|
||||
|
||||
/**
|
||||
*
|
||||
*/
|
||||
public work = (object: any) => {
|
||||
if (Array.isArray(object)) {
|
||||
return object.map(this.getTransformation);
|
||||
} else if (isObject(object)) {
|
||||
return this.getTransformation(object);
|
||||
}
|
||||
return object;
|
||||
};
|
||||
|
||||
/**
|
||||
* Transformes the given item to desired output.
|
||||
* @param item
|
||||
* @returns
|
||||
*/
|
||||
protected getTransformation = (item) => {
|
||||
const normlizedItem = this.normalizeModelItem(item);
|
||||
|
||||
return R.compose(
|
||||
this.transform,
|
||||
R.when(this.hasExcludeAttributes, this.excludeAttributesTransformed),
|
||||
this.includeAttributesTransformed
|
||||
)(normlizedItem);
|
||||
};
|
||||
|
||||
/**
|
||||
*
|
||||
* @param item
|
||||
* @returns
|
||||
*/
|
||||
protected normalizeModelItem = (item) => {
|
||||
return !isUndefined(item.toJSON) ? item.toJSON() : item;
|
||||
};
|
||||
|
||||
/**
|
||||
* Exclude attributes from the given item.
|
||||
*/
|
||||
protected excludeAttributesTransformed = (item) => {
|
||||
const exclude = this.excludeAttributes();
|
||||
|
||||
return omit(item, exclude);
|
||||
};
|
||||
|
||||
/**
|
||||
* Incldues virtual attributes.
|
||||
*/
|
||||
protected getIncludeAttributesTransformed = (item) => {
|
||||
const attributes = this.includeAttributes();
|
||||
|
||||
return attributes
|
||||
.filter(
|
||||
(attribute) =>
|
||||
isFunction(this[attribute]) || !isUndefined(item[attribute])
|
||||
)
|
||||
.reduce((acc, attribute: string) => {
|
||||
acc[attribute] = isFunction(this[attribute])
|
||||
? this[attribute](item)
|
||||
: item[attribute];
|
||||
|
||||
return acc;
|
||||
}, {});
|
||||
};
|
||||
|
||||
/**
|
||||
*
|
||||
* @param item
|
||||
* @returns
|
||||
*/
|
||||
protected includeAttributesTransformed = (item) => {
|
||||
const excludeAll = this.isExcludeAllAttributes();
|
||||
const virtualAttrs = this.getIncludeAttributesTransformed(item);
|
||||
|
||||
return {
|
||||
...(!excludeAll ? item : {}),
|
||||
...virtualAttrs,
|
||||
};
|
||||
};
|
||||
|
||||
/**
|
||||
*
|
||||
* @returns
|
||||
*/
|
||||
private hasExcludeAttributes = () => {
|
||||
return this.excludeAttributes().length > 0;
|
||||
};
|
||||
|
||||
/**
|
||||
*
|
||||
* @param date
|
||||
* @returns
|
||||
*/
|
||||
protected formatDate(date) {
|
||||
return date ? moment(date).format('YYYY/MM/DD') : '';
|
||||
}
|
||||
|
||||
/**
|
||||
*
|
||||
* @param number
|
||||
* @returns
|
||||
*/
|
||||
protected formatNumber(number) {
|
||||
return formatNumber(number, { money: false });
|
||||
}
|
||||
|
||||
/**
|
||||
*
|
||||
* @param money
|
||||
* @param options
|
||||
* @returns
|
||||
*/
|
||||
protected formatMoney(money, options?) {
|
||||
return formatNumber(money, {
|
||||
currencyCode: this.context.organization.baseCurrency,
|
||||
...options,
|
||||
});
|
||||
}
|
||||
|
||||
/**
|
||||
*
|
||||
* @param obj
|
||||
* @param transformer
|
||||
* @param options
|
||||
*/
|
||||
public item(
|
||||
obj: Record<string, any>,
|
||||
transformer: Transformer,
|
||||
options?: any
|
||||
) {
|
||||
transformer.setOptions(options);
|
||||
transformer.setContext(this.context);
|
||||
|
||||
return transformer.work(obj);
|
||||
}
|
||||
|
||||
/**
|
||||
* Sets custom options to the application.
|
||||
* @param {} options
|
||||
* @returns {Transformer}
|
||||
*/
|
||||
public setOptions(options) {
|
||||
this.options = options;
|
||||
return this;
|
||||
}
|
||||
|
||||
/**
|
||||
* Sets the application context to the application.
|
||||
* @param {} context
|
||||
* @returns {Transformer}
|
||||
*/
|
||||
public setContext(context) {
|
||||
this.context = context;
|
||||
return this;
|
||||
}
|
||||
}
|
||||
49
packages/server/src/lib/Transformer/TransformerInjectable.ts
Normal file
49
packages/server/src/lib/Transformer/TransformerInjectable.ts
Normal file
@@ -0,0 +1,49 @@
|
||||
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,
|
||||
};
|
||||
}
|
||||
|
||||
/**
|
||||
* Transformes the given transformer after inject the tenant context.
|
||||
* @param {number} tenantId
|
||||
* @param {Record<string, any> | Record<string, any>[]} object
|
||||
* @param {Transformer} transformer
|
||||
* @param {Record<string, any>} options
|
||||
* @returns {Record<string, any>}
|
||||
*/
|
||||
async transform(
|
||||
tenantId: number,
|
||||
object: Record<string, any> | Record<string, any>[],
|
||||
transformer: Transformer,
|
||||
options?: Record<string, any>
|
||||
) {
|
||||
if (!isNull(tenantId)) {
|
||||
const context = await this.getApplicationContext(tenantId);
|
||||
transformer.setContext(context);
|
||||
}
|
||||
transformer.setOptions(options);
|
||||
|
||||
return transformer.work(object);
|
||||
}
|
||||
}
|
||||
Reference in New Issue
Block a user