update advance filter user
This commit is contained in:
@ -10,6 +10,7 @@ import { Paging } from "entity";
|
||||
import CommonHelper from "../helpers/common";
|
||||
import * as fastcsv from 'fast-csv';
|
||||
import dayjs from "dayjs";
|
||||
import { validate } from 'uuid';
|
||||
|
||||
const log: Logger<ILogObj> = new Logger({ name: '[UserController]', type: 'pretty' });
|
||||
|
||||
@ -57,7 +58,7 @@ export class UserController {
|
||||
|
||||
try {
|
||||
const schema = Joi.object().keys({
|
||||
filter: Joi.array().allow('').optional().label('Filter'),
|
||||
filter: Joi.any().allow('').optional().label('Filter'),
|
||||
page: Joi.number().required().min(1).label('Page'),
|
||||
limit: Joi.number().required().min(1).label('Limit'),
|
||||
with_deleted: Joi.bool().required().label('With Deleted'),
|
||||
@ -75,31 +76,41 @@ export class UserController {
|
||||
let whereAttr = [];
|
||||
let whereVal: any = {};
|
||||
|
||||
if (param.filter && param.filter.length > 0) {
|
||||
if (param.filter) {
|
||||
param.filter = JSON.parse(param.filter);
|
||||
|
||||
let filter_any = false;
|
||||
let filter_any_val = '';
|
||||
|
||||
for (let p in param.filter) {
|
||||
if (p != 'any') {
|
||||
let f = param.filter[p];
|
||||
let f = String(param.filter[p])
|
||||
|
||||
if (p != 'any') {
|
||||
if (!validate(f)) {
|
||||
whereAttr.push("LOWER(" + p + ") = :" + p);
|
||||
f = f.toLowerCase();
|
||||
} else {
|
||||
whereAttr.push(p + " = :" + p);
|
||||
}
|
||||
|
||||
whereAttr.push(p + " = :" + p);
|
||||
whereVal[p] = f;
|
||||
} else {
|
||||
filter_any = true;
|
||||
filter_any_val = f;
|
||||
}
|
||||
}
|
||||
|
||||
if (filter_any) {
|
||||
whereAttr.push('name like :filter_like or username like :filter_like or email = :filter ');
|
||||
whereVal['filter'] = param.filter
|
||||
whereVal['filter_like'] = '%' + param.filter + '%'
|
||||
whereAttr.push('(name like :filter_like or username like :filter_like or email = :filter) ');
|
||||
whereVal['filter'] = filter_any_val
|
||||
whereVal['filter_like'] = '%' + filter_any_val + '%'
|
||||
}
|
||||
}
|
||||
|
||||
const res_count = userRepository.createQueryBuilder()
|
||||
.where(whereAttr, whereVal);
|
||||
.where(whereAttr.join(' and '), whereVal);
|
||||
const res_list = userRepository.createQueryBuilder()
|
||||
.where(whereAttr, whereVal)
|
||||
.where(whereAttr.join(' and '), whereVal)
|
||||
.orderBy(param.order_field, param.order_direction)
|
||||
.offset(offset)
|
||||
.limit(param.limit);
|
||||
|
||||
Reference in New Issue
Block a user