mirror of
https://github.com/bigcapitalhq/bigcapital.git
synced 2026-02-19 06:10:31 +00:00
feat: invoice, estimate and receipt printing.
This commit is contained in:
64
server/src/lib/Transformer/Transformer.ts
Normal file
64
server/src/lib/Transformer/Transformer.ts
Normal file
@@ -0,0 +1,64 @@
|
||||
import moment from "moment";
|
||||
import { isEmpty, isObject, isUndefined } from 'lodash';
|
||||
|
||||
export class Transformer {
|
||||
/**
|
||||
*
|
||||
* @returns
|
||||
*/
|
||||
protected includeAttributes = (): string[] => {
|
||||
return [];
|
||||
}
|
||||
|
||||
/**
|
||||
*
|
||||
*/
|
||||
public transform = (object: any) => {
|
||||
if (Array.isArray(object)) {
|
||||
return object.map(this.getTransformation);
|
||||
} else if (isObject(object)) {
|
||||
return this.getTransformation(object);
|
||||
}
|
||||
return object;
|
||||
};
|
||||
|
||||
/**
|
||||
*
|
||||
* @param item
|
||||
* @returns
|
||||
*/
|
||||
protected getTransformation = (item) => {
|
||||
const attributes = this.getIncludeAttributesTransformed(item);
|
||||
|
||||
return {
|
||||
...!isUndefined(item.toJSON) ? item.toObject() : item,
|
||||
...attributes
|
||||
};
|
||||
};
|
||||
|
||||
/**
|
||||
*
|
||||
* @param item
|
||||
* @returns
|
||||
*/
|
||||
protected getIncludeAttributesTransformed = (item) => {
|
||||
const attributes = this.includeAttributes();
|
||||
|
||||
return attributes
|
||||
.filter((attribute) => !isUndefined(this[attribute]))
|
||||
.reduce((acc, attribute: string) => {
|
||||
acc[attribute] = this[attribute](item);
|
||||
|
||||
return acc;
|
||||
}, {});
|
||||
}
|
||||
|
||||
/**
|
||||
*
|
||||
* @param date
|
||||
* @returns
|
||||
*/
|
||||
protected formatDate(date) {
|
||||
return date ? moment(date).format('YYYY/MM/DD') : '';
|
||||
}
|
||||
}
|
||||
Reference in New Issue
Block a user