From d397ea94bbd3565d9d866173fce00a27bb35cda4 Mon Sep 17 00:00:00 2001 From: Abdul Rahman Reza Date: Tue, 14 Jan 2025 15:10:16 +0700 Subject: [PATCH] fixing export activity log --- src/controllers/user_activity.ts | 66 ++++++++++++++++++++------------ 1 file changed, 41 insertions(+), 25 deletions(-) diff --git a/src/controllers/user_activity.ts b/src/controllers/user_activity.ts index 61ba786..c296866 100644 --- a/src/controllers/user_activity.ts +++ b/src/controllers/user_activity.ts @@ -12,6 +12,7 @@ import * as fastcsv from 'fast-csv'; import dayjs from "dayjs"; import { UserActivity } from "entity"; import exceljs from "exceljs"; +import { ActivityTypeReverse } from "entity/src/types/action"; const log: Logger = new Logger({ name: '[UserActivityController]', type: 'pretty' }); @@ -96,9 +97,6 @@ export class UserActivityController { whereVal['id_user'] = req.auth.data.id; } - // const res_count = userRepository.createQueryBuilder() - // .leftJoinAndMapOne('u.username', User, 'u', 'u.id = id_user') - // .where(whereAttr, whereVal); const res_count = userRepository.createQueryBuilder('user_activity') .leftJoinAndSelect("user_activity.application", "application") .leftJoinAndSelect("user_activity.user", "user") @@ -106,17 +104,6 @@ export class UserActivityController { const res_list = userRepository.createQueryBuilder('user_activity') .leftJoinAndSelect("user_activity.application", "application") .leftJoinAndSelect("user_activity.user", "user") - // .select( - // 'id,\n\ - // id_user,\n\ - // module,\n\ - // description,\n\ - // action,\n\ - // url,\n\ - // application,\n\ - // created_at') - // .addSelect('(SELECT u.username FROM users u WHERE u.id = id_user)', 'username') - // .leftJoinAndMapOne('u.username', User, 'u', 'u.id = id_user') .where(whereAttr, whereVal) .orderBy(param.order_field, param.order_direction) .offset(offset) @@ -213,7 +200,8 @@ export class UserActivityController { { header: "Description", key: "description", width: 25 }, { header: "Action", key: "action", width: 10 }, { header: "Url", key: "url", width: 25 }, - { header: "Created At", key: "created_at", width: 15 } + { header: "Created At", key: "created_at", width: 15 }, + { header: "By", key: "by", width: 20 } ]; const limit = 50; @@ -221,24 +209,52 @@ export class UserActivityController { const fetchAndWrite = async (page: number) => { const offset = (page - 1) * limit - const data = await repo.createQueryBuilder() + // const data = await repo.createQueryBuilder() + // .where(whereAttr, whereVal) + // .select([ + // 'module', + // 'description', + // 'action', + // 'url', + // 'created_at']) + // .addSelect('(SELECT u.username FROM users u WHERE u.id = id_user)', 'username') + // .orderBy(param.order_field, param.order_direction) + // .offset(offset) + // .limit(limit).getRawMany(); + + const data = await repo.createQueryBuilder('user_activity') + .leftJoinAndSelect("user_activity.application", "application") + .leftJoinAndSelect("user_activity.user", "user") .where(whereAttr, whereVal) - .select([ - 'module', - 'description', - 'action', - 'url', - 'created_at']) - .addSelect('(SELECT u.username FROM users u WHERE u.id = id_user)', 'username') .orderBy(param.order_field, param.order_direction) .offset(offset) - .limit(limit).getRawMany(); + .limit(param.limit).getMany(); if (CommonHelper.countObject(data) === 0) { sheet.commit(); workbook.commit(); } else { - data.forEach((item: any) => sheet.addRow(item).commit()); + interface ds { + module: string, + description: string + action: string + url: any + created_at: Date + by: string + } + + data.forEach((item: UserActivity) => { + const di: ds = { + module: item.module, + description: item.description, + action: ActivityTypeReverse[item.action], + created_at: item.created_at, + by: item.user.username, + url: item.url + } + + sheet.addRow(di).commit() + }); if (CommonHelper.countObject(data) == limit) { fetchAndWrite(page + 1);