This commit is contained in:
@ -5,7 +5,7 @@ import { OrmHelper } from "../helpers/orm"
|
||||
import Joi from "joi"
|
||||
import { ILogObj, Logger } from "tslog"
|
||||
import { Language } from "../langs/lang"
|
||||
import { ActivityTypeReverse, Application, Paging, User } from "entity"
|
||||
import { ActivityTypeReverse, Paging, User } from "entity"
|
||||
import CommonHelper from "../helpers/common"
|
||||
import { ActivityType } from "entity"
|
||||
import * as fastcsv from "fast-csv"
|
||||
@ -22,50 +22,50 @@ interface PagingActivity extends Paging {
|
||||
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'
|
||||
}
|
||||
#swagger.parameters['limit'] = {
|
||||
in: 'query',
|
||||
required: true,
|
||||
type: 'number'
|
||||
}
|
||||
#swagger.parameters['page'] = {
|
||||
in: 'query',
|
||||
required: true,
|
||||
type: 'number'
|
||||
}
|
||||
#swagger.parameters['with_deleted'] = {
|
||||
in: 'query',
|
||||
required: true,
|
||||
type: 'boolean'
|
||||
}
|
||||
#swagger.parameters['all_user'] = {
|
||||
in: 'query',
|
||||
required: true,
|
||||
type: 'boolean'
|
||||
}
|
||||
#swagger.parameters['order_field'] = {
|
||||
in: 'query',
|
||||
required: true,
|
||||
type: 'string'
|
||||
}
|
||||
#swagger.parameters['order_direction'] = {
|
||||
in: 'query',
|
||||
required: true,
|
||||
schema: {
|
||||
'@enum': ['ASC', 'DESC']
|
||||
}
|
||||
}
|
||||
#swagger.parameters['token'] = {
|
||||
in: 'query',
|
||||
required: true,
|
||||
type: 'string'
|
||||
}
|
||||
*/
|
||||
#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'
|
||||
}
|
||||
#swagger.parameters['limit'] = {
|
||||
in: 'query',
|
||||
required: true,
|
||||
type: 'number'
|
||||
}
|
||||
#swagger.parameters['page'] = {
|
||||
in: 'query',
|
||||
required: true,
|
||||
type: 'number'
|
||||
}
|
||||
#swagger.parameters['with_deleted'] = {
|
||||
in: 'query',
|
||||
required: true,
|
||||
type: 'boolean'
|
||||
}
|
||||
#swagger.parameters['all_user'] = {
|
||||
in: 'query',
|
||||
required: true,
|
||||
type: 'boolean'
|
||||
}
|
||||
#swagger.parameters['order_field'] = {
|
||||
in: 'query',
|
||||
required: true,
|
||||
type: 'string'
|
||||
}
|
||||
#swagger.parameters['order_direction'] = {
|
||||
in: 'query',
|
||||
required: true,
|
||||
schema: {
|
||||
'@enum': ['ASC', 'DESC']
|
||||
}
|
||||
}
|
||||
#swagger.parameters['token'] = {
|
||||
in: 'query',
|
||||
required: true,
|
||||
type: 'string'
|
||||
}
|
||||
*/
|
||||
|
||||
try {
|
||||
const schema = Joi.object().keys({
|
||||
@ -96,8 +96,28 @@ export class UserActivityController {
|
||||
whereVal["id_user"] = req.auth.data.id
|
||||
}
|
||||
|
||||
const res_count = userRepository.createQueryBuilder("user_activity").leftJoinAndSelect("user_activity.application", "application").leftJoinAndSelect("user_activity.user", "user").where(whereAttr, whereVal)
|
||||
const res_list = userRepository.createQueryBuilder("user_activity").leftJoinAndSelect("user_activity.application", "application").leftJoinAndSelect("user_activity.user", "user").where(whereAttr, whereVal).orderBy(param.order_field, param.order_direction).offset(offset).limit(param.limit)
|
||||
// Whitelist kolom yang boleh dipakai untuk order, sekaligus hindari ambiguous column
|
||||
const allowedOrderFields: Record<string, string> = {
|
||||
created_at: "user_activity.created_at",
|
||||
updated_at: "user_activity.updated_at",
|
||||
module: "user_activity.module",
|
||||
description: "user_activity.description",
|
||||
action: "user_activity.action",
|
||||
// tambahkan kolom lain sesuai kebutuhan
|
||||
}
|
||||
|
||||
const order_field = allowedOrderFields[param.order_field] ?? "user_activity.created_at"
|
||||
|
||||
const res_count = userRepository.createQueryBuilder("user_activity")
|
||||
// .leftJoinAndSelect("user_activity.application", "application")
|
||||
.leftJoinAndSelect("user_activity.user", "user").where(whereAttr, whereVal)
|
||||
|
||||
const res_list = userRepository.createQueryBuilder("user_activity")
|
||||
// .leftJoinAndSelect("user_activity.application", "application")
|
||||
.leftJoinAndSelect("user_activity.user", "user").where(whereAttr, whereVal)
|
||||
.orderBy(order_field, param.order_direction)
|
||||
.offset(offset)
|
||||
.limit(param.limit)
|
||||
|
||||
if (param.with_deleted) {
|
||||
res_count.withDeleted()
|
||||
@ -120,39 +140,39 @@ export class UserActivityController {
|
||||
|
||||
static async export(req: Request, res: Response, next: NextFunction): Promise<void | 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'
|
||||
}
|
||||
#swagger.parameters['filter'] = {
|
||||
in: 'query',
|
||||
type: 'string'
|
||||
}
|
||||
#swagger.parameters['all_user'] = {
|
||||
in: 'query',
|
||||
required: true,
|
||||
type: 'boolean'
|
||||
}
|
||||
#swagger.parameters['order_field'] = {
|
||||
in: 'query',
|
||||
required: true,
|
||||
type: 'string'
|
||||
}
|
||||
#swagger.parameters['order_direction'] = {
|
||||
in: 'query',
|
||||
required: true,
|
||||
schema: {
|
||||
'@enum': ['ASC', 'DESC']
|
||||
}
|
||||
}
|
||||
#swagger.parameters['token'] = {
|
||||
in: 'query',
|
||||
required: true,
|
||||
type: 'string'
|
||||
}
|
||||
*/
|
||||
#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'
|
||||
}
|
||||
#swagger.parameters['filter'] = {
|
||||
in: 'query',
|
||||
type: 'string'
|
||||
}
|
||||
#swagger.parameters['all_user'] = {
|
||||
in: 'query',
|
||||
required: true,
|
||||
type: 'boolean'
|
||||
}
|
||||
#swagger.parameters['order_field'] = {
|
||||
in: 'query',
|
||||
required: true,
|
||||
type: 'string'
|
||||
}
|
||||
#swagger.parameters['order_direction'] = {
|
||||
in: 'query',
|
||||
required: true,
|
||||
schema: {
|
||||
'@enum': ['ASC', 'DESC']
|
||||
}
|
||||
}
|
||||
#swagger.parameters['token'] = {
|
||||
in: 'query',
|
||||
required: true,
|
||||
type: 'string'
|
||||
}
|
||||
*/
|
||||
|
||||
try {
|
||||
const schema = Joi.object().keys({
|
||||
@ -260,23 +280,23 @@ export class UserActivityController {
|
||||
|
||||
static async create(req: Request, res: Response, next: NextFunction): Promise<Response> {
|
||||
/*
|
||||
#swagger.tags = ['User Activity']
|
||||
#swagger.security = [{
|
||||
"bearerAuth": []
|
||||
}]
|
||||
|
||||
#swagger.requestBody = {
|
||||
required: true,
|
||||
description: "Use this to fill action Create = C, View = V, Update = U, Delete = D, Restore = T",
|
||||
content: {
|
||||
"application/json": {
|
||||
schema: {
|
||||
$ref: "#/components/schemas/user_activity"
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
*/
|
||||
#swagger.tags = ['User Activity']
|
||||
#swagger.security = [{
|
||||
"bearerAuth": []
|
||||
}]
|
||||
|
||||
#swagger.requestBody = {
|
||||
required: true,
|
||||
description: "Use this to fill action Create = C, View = V, Update = U, Delete = D, Restore = T",
|
||||
content: {
|
||||
"application/json": {
|
||||
schema: {
|
||||
$ref: "#/components/schemas/user_activity"
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
*/
|
||||
|
||||
try {
|
||||
const schema = Joi.object().keys({
|
||||
@ -290,12 +310,12 @@ export class UserActivityController {
|
||||
.label("Action"),
|
||||
url: Joi.string().optional().allow("").label("URL"),
|
||||
data_body: Joi.string().optional().allow("").label("data_body"),
|
||||
application: Joi.string().required().label("Application"),
|
||||
// application: Joi.string().required().label("Application"),
|
||||
})
|
||||
|
||||
const param: UserActivity & { application: string; id_user: string } = await schema.validateAsync(req.body)
|
||||
|
||||
const repo_app = OrmHelper.DB.getRepository(Application)
|
||||
// const repo_app = OrmHelper.DB.getRepository(Application)
|
||||
const repo_user = OrmHelper.DB.getRepository(User)
|
||||
|
||||
const data = new UserActivity()
|
||||
@ -306,7 +326,7 @@ export class UserActivityController {
|
||||
data.action = param.action
|
||||
data.url = param.url
|
||||
data.data_body = param.data_body
|
||||
data.application = await repo_app.findOneBy({ id: param.application })
|
||||
// data.application = await repo_app.findOneBy({ id: param.application })
|
||||
|
||||
await OrmHelper.DB.manager.save(data)
|
||||
|
||||
|
||||
Reference in New Issue
Block a user