add new api

This commit is contained in:
2024-12-02 13:47:19 +07:00
parent 4da4baafe6
commit e0010d2324
15 changed files with 2988 additions and 51 deletions

View File

@ -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 };
}
}