From 56aad7013035cc109904c49803c109296c21fc95 Mon Sep 17 00:00:00 2001 From: Abdul Rahman Reza Date: Sun, 1 Dec 2024 01:15:44 +0700 Subject: [PATCH] update advance filter user --- src/controllers/user.ts | 31 +++++++++++++++++++++---------- 1 file changed, 21 insertions(+), 10 deletions(-) diff --git a/src/controllers/user.ts b/src/controllers/user.ts index 640e650..822eed8 100644 --- a/src/controllers/user.ts +++ b/src/controllers/user.ts @@ -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 = 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);