diff --git a/config/default.json b/config/default.json index 695e002..6fab56f 100644 --- a/config/default.json +++ b/config/default.json @@ -29,7 +29,7 @@ }, "auth": { "refresh_token_lifetime_day": 30, - "access_token_lifetime": 300, + "access_token_lifetime": 86400, "reset_password_url": "https://bri-dev.shiblysolution.id/reset-password/" }, "service": { diff --git a/config/stag.json b/config/stag.json index 86e54ee..369563d 100644 --- a/config/stag.json +++ b/config/stag.json @@ -29,7 +29,7 @@ }, "auth": { "refresh_token_lifetime_day": 30, - "access_token_lifetime": 300, + "access_token_lifetime": 86400, "reset_password_url": "https://bri-dev.shiblysolution.id/reset-password/" }, "service": { diff --git a/src/controllers/user.ts b/src/controllers/user.ts index 822eed8..2df7ecf 100644 --- a/src/controllers/user.ts +++ b/src/controllers/user.ts @@ -73,44 +73,17 @@ export class UserController { const offset = (param.page - 1) * param.limit - let whereAttr = []; - let whereVal: any = {}; - if (param.filter) { - 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)) { - whereAttr.push("LOWER(" + p + ") = :" + p); - f = f.toLowerCase(); - } else { - 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'] = filter_any_val - whereVal['filter_like'] = '%' + filter_any_val + '%' - } - } + const { whereAttr, whereVal } = CommonHelper.handleFilter({ + filter: param.filter, + col_any_eq: ['email'], + col_any_like: ['name', 'username'] + }); const res_count = userRepository.createQueryBuilder() - .where(whereAttr.join(' and '), whereVal); + .where(whereAttr, whereVal); const res_list = userRepository.createQueryBuilder() - .where(whereAttr.join(' and '), whereVal) + .where(whereAttr, whereVal) .orderBy(param.order_field, param.order_direction) .offset(offset) .limit(param.limit); diff --git a/src/helpers/common.ts b/src/helpers/common.ts index 3075b15..5468435 100644 --- a/src/helpers/common.ts +++ b/src/helpers/common.ts @@ -1,4 +1,5 @@ import { Logger, ILogObj } from 'tslog'; +import { validate } from 'uuid'; const log: Logger = new Logger({ name: '[CommonHelper]', type: 'pretty' }); @@ -91,4 +92,104 @@ export default class CommonHelper { return template } + + static handleFilter(param: { filter: any, col_any_eq: string[], col_any_like: 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 '); + + + return { whereAttr, whereVal }; + } } \ No newline at end of file