UNPKG

phx-react

Version:

PHX REACT

107 lines 4.75 kB
import { useState } from 'react'; import PHXFuncGetLoggedInfo from '../../components/Func/getLoginInfo'; import { PHXClientQueryV3 } from '../../components/Func/GRPC/PHXGrpcClientV3'; import { GET_MODULE_ID } from '../../query/audit-log/query'; import { PHXAxiosInstance } from '../../axiosInstance'; import { PHXFormatDateTime } from '../../helpers/formatDate'; function groupAuditLogsByDate(logs) { const map = new Map(); for (const log of logs) { // format dd/MM/yyyy const date = PHXFormatDateTime(log.created_at, 'dd/mm/yyyy'); // format HH:mm const time = PHXFormatDateTime(log.created_at, 'hh:mm'); if (!map.has(date)) { map.set(date, { date, childs: [], }); } map.get(date).childs.push({ created_at: time, description: log.message, }); } // convert Map → Array + sort mới nhất trước return Array.from(map.values()).sort((a, b) => b.date.localeCompare(a.date)); } export const PHXUseAuditLog = (url, moduleCode) => { var _a, _b, _c, _d; const userInfo = PHXFuncGetLoggedInfo(); const schoolId = userInfo === null || userInfo === void 0 ? void 0 : userInfo.school_id; const userCode = (_c = (_a = userInfo === null || userInfo === void 0 ? void 0 : userInfo.user_code) !== null && _a !== void 0 ? _a : (_b = userInfo === null || userInfo === void 0 ? void 0 : userInfo.profile_staff) === null || _b === void 0 ? void 0 : _b.user_code) !== null && _c !== void 0 ? _c : (_d = userInfo === null || userInfo === void 0 ? void 0 : userInfo.profile_teacher) === null || _d === void 0 ? void 0 : _d.user_code; const fullName = userInfo === null || userInfo === void 0 ? void 0 : userInfo.full_name; const [dataAuditLog, setDataAuditLog] = useState([]); const [loadingFetch, setLoadingFetch] = useState(true); const [loadingInsert, setLoadingInsert] = useState(false); let cachedModuleId = null; const getModuleId = async () => { var _a, _b, _c; if (cachedModuleId !== null) return cachedModuleId; if (!schoolId) return null; try { const resInfo = await PHXClientQueryV3({ query: GET_MODULE_ID, variables: { school_id: schoolId, code: moduleCode, }, }); if (!((_c = (_b = (_a = resInfo === null || resInfo === void 0 ? void 0 : resInfo.data) === null || _a === void 0 ? void 0 : _a.setting_module_define) === null || _b === void 0 ? void 0 : _b[0]) === null || _c === void 0 ? void 0 : _c.id)) { return null; } cachedModuleId = resInfo.data.setting_module_define[0].id; return cachedModuleId; } catch (error) { return null; } }; const fetchAuditLogs = async () => { setLoadingFetch(true); try { const response = await PHXAxiosInstance.get('/audit-log/audit/logs', { params: { url }, }); const auditLogData = response.data || []; setDataAuditLog(auditLogData); return auditLogData; } catch (_a) { return []; } finally { setLoadingFetch(false); } }; const insertAuditLog = async (message, action = 'UPDATE', customMessage) => { setLoadingInsert(true); try { const moduleId = await getModuleId(); if (!moduleId) { return; } const mapMessage = (messages) => messages.map((singleMessage) => `Nhân viên ${fullName} ${userCode ? `(${userCode})` : ''} đã thực hiện hành động ${singleMessage}`.trim()); const messages = Array.isArray(message) ? mapMessage(message) : mapMessage([message]); const customMessages = Array.isArray(customMessage) ? customMessage : [customMessage]; const mes = customMessage ? customMessages : messages; const promises = mes.map((singleMessage) => PHXAxiosInstance.post(`${process.env.NEXT_PUBLIC_API_GATEWAY}/audit-log/audit/create`, { action, module_id: moduleId, message: singleMessage, url, })); await Promise.allSettled(promises); } catch (_a) { } finally { setLoadingInsert(false); } }; const groupedLogs = groupAuditLogsByDate(dataAuditLog); return { insertAuditLog, fetchAuditLogs, dataAuditLog, loadingFetch, loadingInsert, groupedLogs }; }; //# sourceMappingURL=use-audit-log.js.map