add new api
This commit is contained in:
@ -1,4 +1,5 @@
|
||||
import { Logger, ILogObj } from 'tslog';
|
||||
import { validate } from 'uuid';
|
||||
|
||||
const log: Logger<ILogObj> = new Logger({ name: '[CommonHelper]', type: 'pretty' });
|
||||
|
||||
@ -63,4 +64,104 @@ export default class CommonHelper {
|
||||
|
||||
return '';
|
||||
}
|
||||
|
||||
static handleFilter(param: { filter: any, col_any_eq: string[], col_any_like: string[], additional_where?: string }): any {
|
||||
const whereAttrPre = [];
|
||||
const whereVal: any = {};
|
||||
|
||||
if (param.filter) {
|
||||
try {
|
||||
param.filter = JSON.parse(param.filter);
|
||||
|
||||
let filter_any = false;
|
||||
let filter_any_val = '';
|
||||
|
||||
for (let p in param.filter) {
|
||||
let f = String(param.filter[p])
|
||||
|
||||
if (p != 'any') {
|
||||
if (!validate(f)) {
|
||||
whereAttrPre.push("LOWER(" + p + ") = :" + p);
|
||||
f = f.toLowerCase();
|
||||
} else {
|
||||
whereAttrPre.push(p + " = :" + p);
|
||||
}
|
||||
|
||||
whereVal[p] = f;
|
||||
} else {
|
||||
filter_any = true;
|
||||
filter_any_val = f;
|
||||
}
|
||||
}
|
||||
|
||||
if (filter_any) {
|
||||
let wtp = []
|
||||
if (param.col_any_eq.length > 0) {
|
||||
const eqr = [];
|
||||
for (let c of param.col_any_eq) {
|
||||
eqr.push('LOWER(' + c + ') = :filter_eq');
|
||||
}
|
||||
const eq = '(' + eqr.join(' or ') + ')';
|
||||
|
||||
wtp.push(eq);
|
||||
}
|
||||
if (param.col_any_like.length > 0) {
|
||||
const liker = [];
|
||||
for (let c of param.col_any_like) {
|
||||
liker.push(c + ' like :filter_like');
|
||||
}
|
||||
|
||||
const like = '(' + liker.join(' or ') + ')';
|
||||
|
||||
wtp.push(like);
|
||||
}
|
||||
|
||||
whereAttrPre.push('(' + wtp.join(' or ') + ')');
|
||||
|
||||
if (param.col_any_eq.length > 0) {
|
||||
whereVal['filter_eq'] = filter_any_val;
|
||||
}
|
||||
if (param.col_any_like.length > 0) {
|
||||
whereVal['filter_like'] = '%' + filter_any_val + '%';
|
||||
}
|
||||
}
|
||||
} catch (e: any) {
|
||||
let wtp = []
|
||||
if (param.col_any_eq.length > 0) {
|
||||
const eqr = [];
|
||||
for (let c of param.col_any_eq) {
|
||||
eqr.push('LOWER(' + c + ') = :filter_eq');
|
||||
}
|
||||
const eq = '(' + eqr.join(' or ') + ')';
|
||||
|
||||
wtp.push(eq);
|
||||
}
|
||||
if (param.col_any_like.length > 0) {
|
||||
const liker = [];
|
||||
for (let c of param.col_any_like) {
|
||||
liker.push(c + ' like :filter_like');
|
||||
}
|
||||
|
||||
const like = '(' + liker.join(' or ') + ')';
|
||||
|
||||
wtp.push(like);
|
||||
}
|
||||
|
||||
whereAttrPre.push('(' + wtp.join(' or ') + ')');
|
||||
|
||||
if (param.col_any_eq.length > 0) {
|
||||
whereVal['filter_eq'] = param.filter.toLowerCase();
|
||||
}
|
||||
if (param.col_any_like.length > 0) {
|
||||
whereVal['filter_like'] = '%' + param.filter + '%';
|
||||
}
|
||||
|
||||
}
|
||||
}
|
||||
|
||||
const whereAttr = whereAttrPre.join(' and ') + (whereAttrPre.length > 0 && param.additional_where != '' ? ' and ' : '') + param.additional_where;
|
||||
|
||||
|
||||
return { whereAttr, whereVal };
|
||||
}
|
||||
}
|
||||
@ -1,7 +1,7 @@
|
||||
import config from 'config';
|
||||
import { DataSource } from "typeorm";
|
||||
import { ILogObj, Logger } from 'tslog';
|
||||
import { Branch, Menu, Product } from 'entity'
|
||||
import { Administrativu, Aldeia, Branch, ClassEconomi, District, IncomeTier, Menu, Munisipo, Product, Suco } from 'entity'
|
||||
|
||||
export class OrmHelper {
|
||||
static DB: DataSource = null
|
||||
@ -20,7 +20,7 @@ export class OrmHelper {
|
||||
database: String(config.get("database.database")),
|
||||
synchronize: true,
|
||||
logging: true,
|
||||
entities: [Product, Branch, Menu],
|
||||
entities: [Product, Branch, Menu, Administrativu, Aldeia, ClassEconomi, District, IncomeTier, Munisipo, Suco],
|
||||
subscribers: [],
|
||||
migrations: [],
|
||||
})
|
||||
|
||||
Reference in New Issue
Block a user