phx-react
Version:
PHX REACT
112 lines • 5.09 kB
JavaScript
;
Object.defineProperty(exports, "__esModule", { value: true });
exports.PHXUseAuditLog = void 0;
const tslib_1 = require("tslib");
const react_1 = require("react");
const getLoginInfo_1 = tslib_1.__importDefault(require("../../components/Func/getLoginInfo"));
const PHXGrpcClientV3_1 = require("../../components/Func/GRPC/PHXGrpcClientV3");
const query_1 = require("../../query/audit-log/query");
const axiosInstance_1 = require("../../axiosInstance");
const formatDate_1 = require("../../helpers/formatDate");
function groupAuditLogsByDate(logs) {
const map = new Map();
for (const log of logs) {
// format dd/MM/yyyy
const date = (0, formatDate_1.PHXFormatDateTime)(log.created_at, 'dd/mm/yyyy');
// format HH:mm
const time = (0, formatDate_1.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));
}
const PHXUseAuditLog = (url, moduleCode) => {
var _a, _b, _c, _d;
const userInfo = (0, getLoginInfo_1.default)();
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] = (0, react_1.useState)([]);
const [loadingFetch, setLoadingFetch] = (0, react_1.useState)(true);
const [loadingInsert, setLoadingInsert] = (0, react_1.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 (0, PHXGrpcClientV3_1.PHXClientQueryV3)({
query: query_1.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 axiosInstance_1.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) => axiosInstance_1.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 };
};
exports.PHXUseAuditLog = PHXUseAuditLog;
//# sourceMappingURL=use-audit-log.js.map