add user preference

This commit is contained in:
2024-12-02 13:47:17 +07:00
parent a93d4db18d
commit 2722ba8d95
5 changed files with 67 additions and 55 deletions

View File

@ -19,6 +19,7 @@ export class UserController {
/*
#swagger.tags = ['User']
#swagger.parameters['filter'] = {
description: 'Filter with 2 format : <ul><li>Simple format use text plaint will filter eq:email or like %name% or like %username%</li><li>Advance format using field existing {status:\'Y\', any:\'SAME AS PLAINT LOGIC\', etc...}</li></ul>',
in: 'query',
type: 'string'
}
@ -111,6 +112,7 @@ export class UserController {
/*
#swagger.tags = ['User']
#swagger.parameters['filter'] = {
description: 'Filter with 2 format : <ul><li>Simple format use text plaint will filter eq:email or like %name% or like %username%</li><li>Advance format using field existing {status:\'Y\', any:\'SAME AS PLAINT LOGIC\', etc...}</li></ul>',
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";

View File

@ -14,12 +14,16 @@ import { UserActivity } from "entity";
const log: Logger<ILogObj> = 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<Response> {
/*
#swagger.tags = ['User Activity']
#swagger.parameters['filter'] = {
description: 'Filter with 2 format : <ul><li>Simple format use text plaint will filter like %module% or like %description%</li><li>Advance format using field existing {action:\'C\', any:\'SAME AS PLAINT LOGIC\', etc...}</li></ul>',
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 : <ul><li>Simple format use text plaint will filter like %module% or like %description%</li><li>Advance format using field existing {action:\'C\', any:\'SAME AS PLAINT LOGIC\', etc...}</li></ul>',
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();

View File

@ -16,6 +16,7 @@ export class UserRefreshTokenController {
/*
#swagger.tags = ['User Refresh Token']
#swagger.parameters['filter'] = {
description: 'Filter with 2 format : <ul><li>Simple format use text plaint will filter like %name%</li><li>Advance format using field existing {name:\'admin\', any:\'SAME AS PLAINT LOGIC\', etc...}</li></ul>',
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()

View File

@ -18,6 +18,7 @@ export class UserRoleController {
/*
#swagger.tags = ['User Roles']
#swagger.parameters['filter'] = {
description: 'Filter with 2 format : <ul><li>Simple format use text plaint will filter like %name%</li><li>Advance format using field existing {status:\'Y\', any:\'SAME AS PLAINT LOGIC\', etc...}</li></ul>',
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 : <ul><li>Simple format use text plaint will filter like %name%</li><li>Advance format using field existing {status:\'Y\', any:\'SAME AS PLAINT LOGIC\', etc...}</li></ul>',
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";

View File

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