edit log
This commit is contained in:
parent
ba00ce14d2
commit
f6089472ba
1 changed files with 44 additions and 15 deletions
|
|
@ -1,6 +1,9 @@
|
|||
import { NextFunction, Request, Response } from "express";
|
||||
import { Client } from "@elastic/elasticsearch";
|
||||
import permission from "../interfaces/permission";
|
||||
import { AppDataSource } from "../database/data-source";
|
||||
import { PosMaster } from "../entities/PosMaster";
|
||||
import { OrgRevision } from "../entities/OrgRevision";
|
||||
import { Profile } from "../entities/Profile";
|
||||
|
||||
if (!process.env.ELASTICSEARCH_INDEX) {
|
||||
throw new Error("Require ELASTICSEARCH_INDEX to store log.");
|
||||
|
|
@ -19,48 +22,74 @@ const LOG_LEVEL_MAP: Record<string, number> = {
|
|||
const elasticsearch = new Client({
|
||||
node: `${process.env.ELASTICSEARCH_PROTOCOL}://${process.env.ELASTICSEARCH_HOST}:${process.env.ELASTICSEARCH_PORT}`,
|
||||
});
|
||||
|
||||
async function logMiddleware(req: Request, res: Response, next: NextFunction) {
|
||||
if (!req.url.startsWith("/api/")) return next();
|
||||
|
||||
let data: any;
|
||||
const repoPosmaster = AppDataSource.getRepository(PosMaster);
|
||||
const repoProfile = AppDataSource.getRepository(Profile);
|
||||
const repoRevision = AppDataSource.getRepository(OrgRevision);
|
||||
|
||||
const originalJson = res.json;
|
||||
|
||||
res.json = function (v: any) {
|
||||
data = v;
|
||||
return originalJson.call(this, v);
|
||||
};
|
||||
|
||||
const timestamp = new Date().toISOString();
|
||||
const start = performance.now();
|
||||
|
||||
req.app.locals.logData = {};
|
||||
|
||||
const revision = await repoRevision.findOne({
|
||||
where: {
|
||||
orgRevisionIsCurrent: true,
|
||||
orgRevisionIsDraft: false,
|
||||
},
|
||||
});
|
||||
|
||||
res.on("finish", async () => {
|
||||
try {
|
||||
if (!req.url.startsWith("/api/")) return;
|
||||
let system = "organization";
|
||||
if (req.url.startsWith("/api/v1/org/keycloak/log/sso")) system = "inout";
|
||||
if (req.url.startsWith("/api/v1/org/metadata/")) system = "master";
|
||||
if (req.url.startsWith("/api/v1/org/pos/position/")) system = "master";
|
||||
if (req.url.startsWith("/api/v1/org/pos/type/")) system = "master";
|
||||
if (req.url.startsWith("/api/v1/org/employee/pos/position/")) system = "master";
|
||||
if (req.url.startsWith("/api/v1/org/employee/pos/type/")) system = "master";
|
||||
if (req.url.startsWith("/api/v1/org/auth/authRoleAttr/")) system = "admin";
|
||||
if (req.url.startsWith("/api/v1/org/auth/authRole/")) system = "admin";
|
||||
// if (req.url.startsWith("/api/v1/org/keycloak")) system = "admin";
|
||||
if (req.url.startsWith("/api/v1/org/pos/admin/master/list")) system = "admin";
|
||||
if (req.url.startsWith("/api/v1/org/super-admin/{id}")) system = "admin";
|
||||
if (req.url.startsWith("/api/v1/org/permission-org/")) system = "admin";
|
||||
if (req.url.startsWith("/api/v1/org/pos/assign/")) system = "admin";
|
||||
if (req.url.startsWith("/api/v1/org/profile/")) system = "registry";
|
||||
if (req.url.startsWith("/api/v1/org/profile-employee/")) system = "registry";
|
||||
if (req.url.startsWith("/api/v1/org/profile-temp/")) system = "registry";
|
||||
|
||||
const level = LOG_LEVEL_MAP[process.env.LOG_LEVEL ?? "debug"] || 4;
|
||||
const profileByKeycloak = await repoProfile.findOne({
|
||||
where: { keycloak: req.app.locals.logData.userId },
|
||||
});
|
||||
const rootId = await repoPosmaster.findOne({
|
||||
where: {
|
||||
current_holderId: profileByKeycloak?.id,
|
||||
orgRevisionId: revision?.id,
|
||||
},
|
||||
select: ["orgRootId"],
|
||||
});
|
||||
|
||||
if (level === 1 && res.statusCode < 500) return;
|
||||
if (level === 2 && res.statusCode < 400) return;
|
||||
if (level === 3 && res.statusCode < 200) return;
|
||||
|
||||
const token = req.headers["authorization"];
|
||||
let rootId = null;
|
||||
|
||||
try {
|
||||
rootId = token
|
||||
? await new permission().checkOrg(token, req.app.locals.logData.userId)
|
||||
: null;
|
||||
} catch (err) {
|
||||
console.warn("Error fetching rootId:", err);
|
||||
}
|
||||
|
||||
const obj = {
|
||||
logType: res.statusCode >= 500 ? "error" : res.statusCode >= 400 ? "warning" : "info",
|
||||
ip: req.ip,
|
||||
rootId: rootId?.orgRootId ?? null,
|
||||
systemName: "evaluation",
|
||||
systemName: system,
|
||||
startTimeStamp: timestamp,
|
||||
endTimeStamp: new Date().toISOString(),
|
||||
processTime: performance.now() - start,
|
||||
|
|
|
|||
Loading…
Add table
Add a link
Reference in a new issue