From 2722ba8d95b8e88cc0c7096efe77b7096f8dc9ae Mon Sep 17 00:00:00 2001 From: Abdul Rahman Reza Date: Mon, 2 Dec 2024 13:47:17 +0700 Subject: [PATCH] add user preference --- src/controllers/user.ts | 15 +++---- src/controllers/user_activity.ts | 61 +++++++++++++++++---------- src/controllers/user_refresh_token.ts | 16 +++---- src/controllers/user_roles.ts | 26 ++++++------ src/helpers/common.ts | 4 +- 5 files changed, 67 insertions(+), 55 deletions(-) diff --git a/src/controllers/user.ts b/src/controllers/user.ts index 2df7ecf..13dd41c 100644 --- a/src/controllers/user.ts +++ b/src/controllers/user.ts @@ -19,6 +19,7 @@ export class UserController { /* #swagger.tags = ['User'] #swagger.parameters['filter'] = { + description: 'Filter with 2 format : ', in: 'query', type: 'string' } @@ -111,6 +112,7 @@ export class UserController { /* #swagger.tags = ['User'] #swagger.parameters['filter'] = { + description: 'Filter with 2 format : ', in: 'query', type: 'string' } @@ -149,14 +151,11 @@ export class UserController { const userRepository = OrmHelper.DB.getRepository(User); - let whereAttr = ''; - let whereVal: any = {}; - - if (param.filter) { - whereAttr = 'name like :filter_like or username like :filter_like or email = :filter '; - whereVal['filter'] = param.filter - whereVal['filter_like'] = '%' + param.filter + '%' - } + const { whereAttr, whereVal } = CommonHelper.handleFilter({ + filter: param.filter, + col_any_eq: ['email'], + col_any_like: ['name', 'username'] + }); const filename = "user.csv"; diff --git a/src/controllers/user_activity.ts b/src/controllers/user_activity.ts index 61dda9c..50c8479 100644 --- a/src/controllers/user_activity.ts +++ b/src/controllers/user_activity.ts @@ -14,12 +14,16 @@ import { UserActivity } from "entity"; const log: Logger = new Logger({ name: '[UserActivityController]', type: 'pretty' }); +interface PagingActivity extends Paging { + all_user: boolean +} + export class UserActivityController { static async list(req: Request, res: Response, next: NextFunction): Promise { /* - #swagger.tags = ['User Activity'] #swagger.parameters['filter'] = { + description: 'Filter with 2 format :
  • Simple format use text plaint will filter like %module% or like %description%
  • Advance format using field existing {action:\'C\', any:\'SAME AS PLAINT LOGIC\', etc...}
', in: 'query', type: 'string' } @@ -38,6 +42,11 @@ export class UserActivityController { required: true, type: 'boolean' } + #swagger.parameters['all_user'] = { + in: 'query', + required: true, + type: 'boolean' + } #swagger.parameters['order_field'] = { in: 'query', required: true, @@ -63,32 +72,33 @@ export class UserActivityController { page: Joi.number().required().min(1).label('Page'), limit: Joi.number().required().min(1).label('Limit'), with_deleted: Joi.bool().required().label('With Deleted'), + all_user: Joi.bool().required().label('All User'), order_field: Joi.string().required().label('Order Field'), order_direction: Joi.string().allow('asc', 'desc').required().label('Order Direction'), token: Joi.string().required().label('Token'), }); - const param: Paging = await schema.validateAsync(req.query); + const param: PagingActivity = await schema.validateAsync(req.query); const userRepository = OrmHelper.DB.getRepository(UserActivity); const offset = (param.page - 1) * param.limit - let whereAttr = ''; - let whereVal: any = {}; + const { whereAttr, whereVal } = CommonHelper.handleFilter({ + filter: param.filter, + col_any_eq: [], + col_any_like: ['module', 'description'], + additional_where: !param.all_user ? 'id_user = :id_user' : '' + }); - if (param.filter) { - whereAttr = '(module like :filter_like or description like :filter_like) and '; - // whereVal['filter'] = param.filter - whereVal['filter_like'] = '%' + param.filter + '%' + if (!param.all_user) { + whereVal['id_user'] = req.auth.data.id; } - whereAttr += 'id_user = :id_user' - whereVal['id_user'] = req.auth.data.id; - const res_count = userRepository.createQueryBuilder() .where(whereAttr, whereVal); const res_list = userRepository.createQueryBuilder() + .addSelect('(select user.name from user user where user.id=User_role.id_id_user )', 'username') .where(whereAttr, whereVal) .orderBy(param.order_field, param.order_direction) .offset(offset) @@ -101,7 +111,7 @@ export class UserActivityController { const current_page = param.page; const total_count_data = await res_count.getCount(); - const list_data = await res_list.getMany(); + const list_data = await res_list.getRawMany() const count_data = CommonHelper.countObject(list_data); return ReturnHelper.successResponselist(res, 200, Language.lang.success_view, count_data, current_page, total_count_data, list_data); @@ -117,6 +127,7 @@ export class UserActivityController { /* #swagger.tags = ['User Activity'] #swagger.parameters['filter'] = { + description: 'Filter with 2 format :
  • Simple format use text plaint will filter like %module% or like %description%
  • Advance format using field existing {action:\'C\', any:\'SAME AS PLAINT LOGIC\', etc...}
', in: 'query', type: 'string' } @@ -124,6 +135,11 @@ export class UserActivityController { in: 'query', type: 'string' } + #swagger.parameters['all_user'] = { + in: 'query', + required: true, + type: 'boolean' + } #swagger.parameters['order_field'] = { in: 'query', required: true, @@ -146,27 +162,27 @@ export class UserActivityController { try { const schema = Joi.object().keys({ filter: Joi.string().allow('').optional().label('Filter'), + all_user: Joi.bool().required().label('All User'), order_field: Joi.string().required().label('Order Field'), order_direction: Joi.string().allow('asc', 'desc').required().label('Order Direction'), token: Joi.string().required().label('Token'), }); - const param: Paging = await schema.validateAsync(req.query); + const param: PagingActivity = await schema.validateAsync(req.query); const repo = OrmHelper.DB.getRepository(UserActivity); - let whereAttr = ''; - let whereVal: any = {}; + const { whereAttr, whereVal } = CommonHelper.handleFilter({ + filter: param.filter, + col_any_eq: [], + col_any_like: ['module', 'description'], + additional_where: !param.all_user ? 'id_user = :id_user' : '' + }); - if (param.filter) { - whereAttr = '(module like :filter_like or description like :filter_like) and '; - // whereVal['filter'] = param.filter - whereVal['filter_like'] = '%' + param.filter + '%' + if (!param.all_user) { + whereVal['id_user'] = req.auth.data.id; } - whereAttr += 'id_user = :id_user' - whereVal['id_user'] = req.auth.data.id; - const filename = "user_activity.csv"; res.setHeader('Content-Type', 'text/csv'); @@ -196,6 +212,7 @@ export class UserActivityController { 'action', 'url', 'created_at']) + .addSelect('(select user.name from user user where user.id=User_role.id_id_user )', 'username') .orderBy(param.order_field, param.order_direction) .offset(offset) .limit(limit).getRawMany(); diff --git a/src/controllers/user_refresh_token.ts b/src/controllers/user_refresh_token.ts index 74f8ed5..a9bd18b 100644 --- a/src/controllers/user_refresh_token.ts +++ b/src/controllers/user_refresh_token.ts @@ -16,6 +16,7 @@ export class UserRefreshTokenController { /* #swagger.tags = ['User Refresh Token'] #swagger.parameters['filter'] = { + description: 'Filter with 2 format :
  • Simple format use text plaint will filter like %name%
  • Advance format using field existing {name:\'admin\', any:\'SAME AS PLAINT LOGIC\', etc...}
', in: 'query', type: 'string' } @@ -64,16 +65,13 @@ export class UserRefreshTokenController { const offset = (param.page - 1) * param.limit - let whereAttr = ''; - let whereVal: any = {}; + const { whereAttr, whereVal } = CommonHelper.handleFilter({ + filter: param.filter, + col_any_eq: [], + col_any_like: ['device', 'platform'], + additional_where: 'id_user = :id_user' + }); - if (param.filter) { - whereAttr = '(name like :filter_like or username like :filter_like or email = :filter) and '; - whereVal['filter'] = param.filter - whereVal['filter_like'] = '%' + param.filter + '%' - } - - whereAttr += 'id_user = :id_user' whereVal['id_user'] = req.auth.data.id; const res_count = repo.createQueryBuilder() diff --git a/src/controllers/user_roles.ts b/src/controllers/user_roles.ts index 25543b3..302175f 100644 --- a/src/controllers/user_roles.ts +++ b/src/controllers/user_roles.ts @@ -18,6 +18,7 @@ export class UserRoleController { /* #swagger.tags = ['User Roles'] #swagger.parameters['filter'] = { + description: 'Filter with 2 format :
  • Simple format use text plaint will filter like %name%
  • Advance format using field existing {status:\'Y\', any:\'SAME AS PLAINT LOGIC\', etc...}
', in: 'query', type: 'string' } @@ -72,13 +73,11 @@ export class UserRoleController { const offset = (param.page - 1) * param.limit - let whereAttr = ''; - let whereVal: any = {}; - - if (param.filter) { - whereAttr = 'name like :filter_like'; - whereVal['filter_like'] = '%' + param.filter + '%' - } + const { whereAttr, whereVal } = CommonHelper.handleFilter({ + filter: param.filter, + col_any_eq: [], + col_any_like: ['name'] + }); const res_count = repo.createQueryBuilder() .where(whereAttr, whereVal); @@ -111,6 +110,7 @@ export class UserRoleController { /* #swagger.tags = ['User Roles'] #swagger.parameters['filter'] = { + description: 'Filter with 2 format :
  • Simple format use text plaint will filter like %name%
  • Advance format using field existing {status:\'Y\', any:\'SAME AS PLAINT LOGIC\', etc...}
', in: 'query', type: 'string' } @@ -149,13 +149,11 @@ export class UserRoleController { const repo = OrmHelper.DB.getRepository(UserRole); - let whereAttr = ''; - let whereVal: any = {}; - - if (param.filter) { - whereAttr = 'name like :filter_like'; - whereVal['filter_like'] = '%' + param.filter + '%' - } + const { whereAttr, whereVal } = CommonHelper.handleFilter({ + filter: param.filter, + col_any_eq: [], + col_any_like: ['name'] + }); const filename = "user_role.csv"; diff --git a/src/helpers/common.ts b/src/helpers/common.ts index 5468435..0db4c65 100644 --- a/src/helpers/common.ts +++ b/src/helpers/common.ts @@ -93,7 +93,7 @@ export default class CommonHelper { return template } - static handleFilter(param: { filter: any, col_any_eq: string[], col_any_like: string[] }): any { + static handleFilter(param: { filter: any, col_any_eq: string[], col_any_like: string[], additional_where?: string }): any { const whereAttrPre = []; const whereVal: any = {}; @@ -187,7 +187,7 @@ export default class CommonHelper { } } - const whereAttr = whereAttrPre.join(' and '); + const whereAttr = whereAttrPre.join(' and ') + (whereAttrPre.length > 0 && param.additional_where != '' ? ' and ' : '') + param.additional_where; return { whereAttr, whereVal };