checkpoint

This commit is contained in:
AdisakKanthawilang 2024-10-25 13:01:27 +07:00
parent 3f3ec27abd
commit 0c7ad61da7
3 changed files with 430 additions and 9 deletions

View file

@ -3,6 +3,7 @@ import { Client } from "@elastic/elasticsearch";
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.");
@ -27,6 +28,7 @@ async function logMiddleware(req: Request, res: Response, next: NextFunction) {
let data: any;
const repoPosmaster = AppDataSource.getRepository(PosMaster);
const repoProfile = AppDataSource.getRepository(Profile);
const repoRevision = AppDataSource.getRepository(OrgRevision);
const originalJson = res.json;
@ -41,14 +43,13 @@ async function logMiddleware(req: Request, res: Response, next: NextFunction) {
req.app.locals.logData = {};
const revision = await repoRevision.findOne({
where: { orgRevisionIsCurrent: true , orgRevisionIsDraft: false },
where: {
orgRevisionIsCurrent: true ,
orgRevisionIsDraft: false
},
})
const rootId = await repoPosmaster.findOne({
where: { current_holderId: req.app.locals.logData.userId, orgRevisionId: revision?.id },
});
res.on("finish", () => {
res.on("finish", async() => {
if (!req.url.startsWith("/api/")) return;
let system = "organization";
if (req.url.startsWith("/api/v1/org/metadata/")) system = "master";
@ -68,7 +69,17 @@ async function logMiddleware(req: Request, res: Response, next: NextFunction) {
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;
@ -89,7 +100,6 @@ async function logMiddleware(req: Request, res: Response, next: NextFunction) {
output: (level === 4 && JSON.stringify(data, null, 2)) || undefined,
...req.app.locals.logData,
};
elasticsearch.index({
index: ELASTICSEARCH_INDEX,
document: obj,