Merge branch 'develop'
This commit is contained in:
commit
abd80b576e
5 changed files with 235 additions and 184 deletions
60
.github/workflows/release.yaml
vendored
60
.github/workflows/release.yaml
vendored
|
|
@ -1,5 +1,5 @@
|
||||||
name: release-test
|
name: release
|
||||||
run-name: release-test ${{ github.actor }}
|
run-name: release ${{ github.actor }}
|
||||||
on:
|
on:
|
||||||
push:
|
push:
|
||||||
tags:
|
tags:
|
||||||
|
|
@ -7,14 +7,13 @@ on:
|
||||||
workflow_dispatch:
|
workflow_dispatch:
|
||||||
env:
|
env:
|
||||||
REGISTRY: docker.frappet.com
|
REGISTRY: docker.frappet.com
|
||||||
# IMAGE_NAME: ehr/bma-ehr-node-service
|
|
||||||
IMAGE_NAME: ehr/bma-ehr-probation
|
IMAGE_NAME: ehr/bma-ehr-probation
|
||||||
DEPLOY_HOST: frappet.com
|
DEPLOY_HOST: frappet.com
|
||||||
# COMPOSE_PATH: /home/frappet/docker/bma-ehr
|
|
||||||
COMPOSE_PATH: /home/frappet/docker/bma/bma-ehr-probation
|
COMPOSE_PATH: /home/frappet/docker/bma/bma-ehr-probation
|
||||||
|
|
||||||
jobs:
|
jobs:
|
||||||
# act workflow_dispatch -W .github/workflows/release.yaml --input IMAGE_VER=test-v1 -s DOCKER_USER=sorawit -s DOCKER_PASS=P@ssword -s SSH_PASSWORD=P@ssw0rd
|
# act workflow_dispatch -W .github/workflows/release.yaml --input IMAGE_VER=test-v1 -s DOCKER_USER=sorawit -s DOCKER_PASS=P@ssword -s SSH_PASSWORD=P@ssw0rd
|
||||||
release-test:
|
release:
|
||||||
runs-on: ubuntu-latest
|
runs-on: ubuntu-latest
|
||||||
steps:
|
steps:
|
||||||
- uses: actions/checkout@v3
|
- uses: actions/checkout@v3
|
||||||
|
|
@ -68,21 +67,40 @@ jobs:
|
||||||
docker compose pull
|
docker compose pull
|
||||||
docker compose up -d
|
docker compose up -d
|
||||||
echo "${{ steps.gen_ver.outputs.image_ver }}"> success
|
echo "${{ steps.gen_ver.outputs.image_ver }}"> success
|
||||||
- uses: snow-actions/line-notify@v1.1.0
|
- name: Notify Discord Success
|
||||||
if: success()
|
if: success()
|
||||||
with:
|
run: |
|
||||||
access_token: ${{ secrets.TOKEN_LINE }}
|
curl -H "Content-Type: application/json" \
|
||||||
message: |
|
-X POST \
|
||||||
-Success✅✅✅
|
-d '{
|
||||||
Image: ${{env.IMAGE_NAME}}
|
"embeds": [{
|
||||||
Version: ${{ steps.gen_ver.outputs.IMAGE_VER }}
|
"title": "✅ Deployment Success!",
|
||||||
By: ${{github.actor}}
|
"description": "**Details:**\n- Image: `${{env.IMAGE_NAME}}`\n- Version: `${{ steps.gen_ver.outputs.image_ver }}`\n- Deployed by: `${{github.actor}}`",
|
||||||
- uses: snow-actions/line-notify@v1.1.0
|
"color": 3066993,
|
||||||
|
"footer": {
|
||||||
|
"text": "Release Notification",
|
||||||
|
"icon_url": "https://example.com/success-icon.png"
|
||||||
|
},
|
||||||
|
"timestamp": "'$(date -u +%Y-%m-%dT%H:%M:%SZ)'"
|
||||||
|
}]
|
||||||
|
}' \
|
||||||
|
${{ secrets.DISCORD_WEBHOOK }}
|
||||||
|
|
||||||
|
- name: Notify Discord Failure
|
||||||
if: failure()
|
if: failure()
|
||||||
with:
|
run: |
|
||||||
access_token: ${{ secrets.TOKEN_LINE }}
|
curl -H "Content-Type: application/json" \
|
||||||
message: |
|
-X POST \
|
||||||
-Failure❌❌❌
|
-d '{
|
||||||
Image: ${{env.IMAGE_NAME}}
|
"embeds": [{
|
||||||
Version: ${{ steps.gen_ver.outputs.IMAGE_VER }}
|
"title": "❌ Deployment Failed!",
|
||||||
By: ${{github.actor}}
|
"description": "**Details:**\n- Image: `${{env.IMAGE_NAME}}`\n- Version: `${{ steps.gen_ver.outputs.image_ver }}`\n- Attempted by: `${{github.actor}}`",
|
||||||
|
"color": 15158332,
|
||||||
|
"footer": {
|
||||||
|
"text": "Release Notification",
|
||||||
|
"icon_url": "https://example.com/failure-icon.png"
|
||||||
|
},
|
||||||
|
"timestamp": "'$(date -u +%Y-%m-%dT%H:%M:%SZ)'"
|
||||||
|
}]
|
||||||
|
}' \
|
||||||
|
${{ secrets.DISCORD_WEBHOOK }}
|
||||||
|
|
|
||||||
|
|
@ -184,6 +184,9 @@ export class PersonalController extends Controller {
|
||||||
personal_id: lists[i].personal_id,
|
personal_id: lists[i].personal_id,
|
||||||
ordering: i + 1,
|
ordering: i + 1,
|
||||||
name: lists[i].prefixName + lists[i].firstName + " " + lists[i].lastName,
|
name: lists[i].prefixName + lists[i].firstName + " " + lists[i].lastName,
|
||||||
|
prefixName: lists[i].prefixName,
|
||||||
|
firstName: lists[i].firstName,
|
||||||
|
lastName: lists[i].lastName,
|
||||||
idcard: lists[i].idcard,
|
idcard: lists[i].idcard,
|
||||||
position_line: lists[i].positionName,
|
position_line: lists[i].positionName,
|
||||||
position_level: lists[i].positionLevelName,
|
position_level: lists[i].positionLevelName,
|
||||||
|
|
|
||||||
|
|
@ -1,96 +1,86 @@
|
||||||
import {
|
import { Path } from "tsoa"
|
||||||
Controller,
|
import axios from "axios"
|
||||||
Request,
|
import { addLogSequence } from "./utils"
|
||||||
Get,
|
|
||||||
Post,
|
|
||||||
Put,
|
|
||||||
Delete,
|
|
||||||
Patch,
|
|
||||||
Route,
|
|
||||||
Security,
|
|
||||||
Tags,
|
|
||||||
Path,
|
|
||||||
} from "tsoa";
|
|
||||||
import axios from "axios";
|
|
||||||
import { addLogSequence } from "./utils";
|
|
||||||
|
|
||||||
class CallAPI {
|
class CallAPI {
|
||||||
//Get
|
//Get
|
||||||
public async GetData(request: any, @Path() path: any) {
|
public async GetData(request: any, @Path() path: any, log = true) {
|
||||||
const token = request.headers.authorization;
|
const token = "Bearer " + request.headers.authorization.replace("Bearer ", "")
|
||||||
const url = process.env.API_URL + path;
|
const url = process.env.API_URL + path
|
||||||
try {
|
try {
|
||||||
const response = await axios.get(url, {
|
const response = await axios.get(url, {
|
||||||
headers: {
|
headers: {
|
||||||
Authorization: `${token}`,
|
Authorization: `${token}`,
|
||||||
"Content-Type": "application/json",
|
"Content-Type": "application/json",
|
||||||
api_key: process.env.API_KEY,
|
api_key: process.env.API_KEY,
|
||||||
},
|
},
|
||||||
});
|
})
|
||||||
addLogSequence(request, {
|
if (log)
|
||||||
action: "request",
|
addLogSequence(request, {
|
||||||
status: "success",
|
action: "request",
|
||||||
description: "connected",
|
status: "success",
|
||||||
request: {
|
description: "connected",
|
||||||
method: "GET",
|
request: {
|
||||||
url: url,
|
method: "GET",
|
||||||
response: JSON.stringify(response.data.result),
|
url: url,
|
||||||
},
|
response: JSON.stringify(response.data.result),
|
||||||
});
|
},
|
||||||
return response.data.result;
|
})
|
||||||
} catch (error) {
|
return response.data.result
|
||||||
addLogSequence(request, {
|
} catch (error) {
|
||||||
action: "request",
|
if (log)
|
||||||
status: "error",
|
addLogSequence(request, {
|
||||||
description: "unconnected",
|
action: "request",
|
||||||
request: {
|
status: "error",
|
||||||
method: "GET",
|
description: "unconnected",
|
||||||
url: url,
|
request: {
|
||||||
response: JSON.stringify(error),
|
method: "GET",
|
||||||
},
|
url: url,
|
||||||
});
|
response: JSON.stringify(error),
|
||||||
throw error;
|
},
|
||||||
}
|
})
|
||||||
}
|
throw error
|
||||||
//Post
|
}
|
||||||
public async PostData(request: any, @Path() path: any, sendData: any) {
|
}
|
||||||
const token = request.headers.authorization;
|
//Post
|
||||||
const url = process.env.API_URL + path;
|
public async PostData(request: any, @Path() path: any, sendData: any) {
|
||||||
try {
|
const token = "Bearer " + request.headers.authorization.replace("Bearer ", "")
|
||||||
const response = await axios.post(url, sendData, {
|
const url = process.env.API_URL + path
|
||||||
headers: {
|
try {
|
||||||
Authorization: `${token}`,
|
const response = await axios.post(url, sendData, {
|
||||||
"Content-Type": "application/json",
|
headers: {
|
||||||
api_key: process.env.API_KEY,
|
Authorization: `${token}`,
|
||||||
},
|
"Content-Type": "application/json",
|
||||||
});
|
api_key: process.env.API_KEY,
|
||||||
addLogSequence(request, {
|
},
|
||||||
action: "request",
|
})
|
||||||
status: "success",
|
addLogSequence(request, {
|
||||||
description: "connected",
|
action: "request",
|
||||||
request: {
|
status: "success",
|
||||||
method: "POST",
|
description: "connected",
|
||||||
url: url,
|
request: {
|
||||||
payload: JSON.stringify(sendData),
|
method: "POST",
|
||||||
response: JSON.stringify(response.data.result),
|
url: url,
|
||||||
},
|
payload: JSON.stringify(sendData),
|
||||||
});
|
response: JSON.stringify(response.data.result),
|
||||||
return response.data.result;
|
},
|
||||||
} catch (error) {
|
})
|
||||||
addLogSequence(request, {
|
return response.data.result
|
||||||
action: "request",
|
} catch (error) {
|
||||||
status: "error",
|
addLogSequence(request, {
|
||||||
description: "unconnected",
|
action: "request",
|
||||||
request: {
|
status: "error",
|
||||||
method: "POST",
|
description: "unconnected",
|
||||||
url: url,
|
request: {
|
||||||
payload: JSON.stringify(sendData),
|
method: "POST",
|
||||||
response: JSON.stringify(error),
|
url: url,
|
||||||
},
|
payload: JSON.stringify(sendData),
|
||||||
});
|
response: JSON.stringify(error),
|
||||||
throw error;
|
},
|
||||||
}
|
})
|
||||||
}
|
throw error
|
||||||
|
}
|
||||||
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
export default CallAPI;
|
export default CallAPI
|
||||||
|
|
|
||||||
|
|
@ -3,8 +3,11 @@ import { RequestWithUser } from "../middlewares/user"
|
||||||
import CallAPI from "./call-api"
|
import CallAPI from "./call-api"
|
||||||
import HttpError from "./http-error"
|
import HttpError from "./http-error"
|
||||||
import HttpStatus from "./http-status"
|
import HttpStatus from "./http-status"
|
||||||
|
import { promisify } from "util"
|
||||||
|
|
||||||
class CheckAuth {
|
class CheckAuth {
|
||||||
|
private redis = require("redis")
|
||||||
|
|
||||||
public async Permission(req: RequestWithUser, system: string, action: string) {
|
public async Permission(req: RequestWithUser, system: string, action: string) {
|
||||||
if (req.headers.hasOwnProperty("api_key") && req.headers["api_key"] && req.headers["api_key"] == process.env.API_KEY) {
|
if (req.headers.hasOwnProperty("api_key") && req.headers["api_key"] && req.headers["api_key"] == process.env.API_KEY) {
|
||||||
return null
|
return null
|
||||||
|
|
@ -155,6 +158,41 @@ class CheckAuth {
|
||||||
return false
|
return false
|
||||||
})
|
})
|
||||||
}
|
}
|
||||||
|
public async checkOrg(token: any, keycloakId: string) {
|
||||||
|
const redisClient = await this.redis.createClient({
|
||||||
|
host: process.env.REDIS_HOST,
|
||||||
|
port: process.env.REDIS_PORT,
|
||||||
|
})
|
||||||
|
const getAsync = promisify(redisClient.get).bind(redisClient)
|
||||||
|
let reply = await getAsync("org_" + keycloakId)
|
||||||
|
if (reply != null) {
|
||||||
|
reply = JSON.parse(reply)
|
||||||
|
} else {
|
||||||
|
try {
|
||||||
|
if (!keycloakId) throw "Error calling API No KeycloakId"
|
||||||
|
const x = await new CallAPI().GetData(
|
||||||
|
{
|
||||||
|
headers: { authorization: token },
|
||||||
|
},
|
||||||
|
`/org/permission/checkOrg/${keycloakId}`,
|
||||||
|
false
|
||||||
|
)
|
||||||
|
|
||||||
|
const data = {
|
||||||
|
orgRootId: x.orgRootId,
|
||||||
|
orgChild1Id: x.orgChild1Id,
|
||||||
|
orgChild2Id: x.orgChild2Id,
|
||||||
|
orgChild3Id: x.orgChild3Id,
|
||||||
|
orgChild4Id: x.orgChild4Id,
|
||||||
|
}
|
||||||
|
|
||||||
|
return data
|
||||||
|
} catch (error) {
|
||||||
|
console.error("Error calling API:", error)
|
||||||
|
throw error
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
public async PermissionCreate(req: RequestWithUser, system: string) {
|
public async PermissionCreate(req: RequestWithUser, system: string) {
|
||||||
return await this.Permission(req, system, "CREATE")
|
return await this.Permission(req, system, "CREATE")
|
||||||
}
|
}
|
||||||
|
|
|
||||||
|
|
@ -1,79 +1,81 @@
|
||||||
import { NextFunction, Request, Response } from "express";
|
import { NextFunction, Request, Response } from "express"
|
||||||
import { Client } from "@elastic/elasticsearch";
|
import { Client } from "@elastic/elasticsearch"
|
||||||
|
import permission from "../interfaces/permission"
|
||||||
|
|
||||||
if (!process.env.ELASTICSEARCH_INDEX) {
|
if (!process.env.ELASTICSEARCH_INDEX) {
|
||||||
throw new Error("Require ELASTICSEARCH_INDEX to store log.");
|
throw new Error("Require ELASTICSEARCH_INDEX to store log.")
|
||||||
}
|
}
|
||||||
|
|
||||||
const ELASTICSEARCH_INDEX = process.env.ELASTICSEARCH_INDEX;
|
const ELASTICSEARCH_INDEX = process.env.ELASTICSEARCH_INDEX
|
||||||
|
|
||||||
const LOG_LEVEL_MAP: Record<string, number> = {
|
const LOG_LEVEL_MAP: Record<string, number> = {
|
||||||
debug: 4,
|
debug: 4,
|
||||||
info: 3,
|
info: 3,
|
||||||
warning: 2,
|
warning: 2,
|
||||||
error: 1,
|
error: 1,
|
||||||
none: 0,
|
none: 0,
|
||||||
};
|
|
||||||
|
|
||||||
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 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 = {};
|
|
||||||
|
|
||||||
res.on("finish", () => {
|
|
||||||
if (!req.url.startsWith("/api/")) return;
|
|
||||||
|
|
||||||
const level = LOG_LEVEL_MAP[process.env.LOG_LEVEL ?? "debug"] || 4;
|
|
||||||
|
|
||||||
if (level === 1 && res.statusCode < 500) return;
|
|
||||||
if (level === 2 && res.statusCode < 400) return;
|
|
||||||
if (level === 3 && res.statusCode < 200) return;
|
|
||||||
|
|
||||||
const obj = {
|
|
||||||
logType:
|
|
||||||
res.statusCode >= 500
|
|
||||||
? "error"
|
|
||||||
: res.statusCode >= 400
|
|
||||||
? "warning"
|
|
||||||
: "info",
|
|
||||||
ip: req.ip,
|
|
||||||
systemName: "probation",
|
|
||||||
startTimeStamp: timestamp,
|
|
||||||
endTimeStamp: new Date().toISOString(),
|
|
||||||
processTime: performance.now() - start,
|
|
||||||
host: req.hostname,
|
|
||||||
method: req.method,
|
|
||||||
endpoint: req.url,
|
|
||||||
responseCode: String(res.statusCode === 304 ? 200 : res.statusCode),
|
|
||||||
responseDescription: data?.message,
|
|
||||||
input: (level === 4 && JSON.stringify(req.body, null, 2)) || undefined,
|
|
||||||
output: (level === 4 && JSON.stringify(data, null, 2)) || undefined,
|
|
||||||
...req.app.locals.logData,
|
|
||||||
};
|
|
||||||
|
|
||||||
elasticsearch.index({
|
|
||||||
index: ELASTICSEARCH_INDEX,
|
|
||||||
document: obj,
|
|
||||||
});
|
|
||||||
});
|
|
||||||
|
|
||||||
return next();
|
|
||||||
}
|
}
|
||||||
|
|
||||||
export default logMiddleware;
|
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 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 = {}
|
||||||
|
|
||||||
|
res.on("finish", async () => {
|
||||||
|
if (!req.url.startsWith("/api/")) return
|
||||||
|
|
||||||
|
const level = LOG_LEVEL_MAP[process.env.LOG_LEVEL ?? "debug"] || 4
|
||||||
|
|
||||||
|
if (level === 1 && res.statusCode < 500) return
|
||||||
|
if (level === 2 && res.statusCode < 400) return
|
||||||
|
if (level === 3 && res.statusCode < 200) return
|
||||||
|
|
||||||
|
let token: any
|
||||||
|
token = req.headers["authorization"]
|
||||||
|
|
||||||
|
const rootId = await new permission().checkOrg(token, req.app.locals.logData.userId)
|
||||||
|
|
||||||
|
const obj = {
|
||||||
|
logType: res.statusCode >= 500 ? "error" : res.statusCode >= 400 ? "warning" : "info",
|
||||||
|
ip: req.ip,
|
||||||
|
rootId: rootId ? rootId.orgRootId : null,
|
||||||
|
systemName: "probation",
|
||||||
|
startTimeStamp: timestamp,
|
||||||
|
endTimeStamp: new Date().toISOString(),
|
||||||
|
processTime: performance.now() - start,
|
||||||
|
host: req.hostname,
|
||||||
|
method: req.method,
|
||||||
|
endpoint: req.url,
|
||||||
|
responseCode: String(res.statusCode === 304 ? 200 : res.statusCode),
|
||||||
|
responseDescription: data?.message,
|
||||||
|
input: (level === 4 && JSON.stringify(req.body, null, 2)) || undefined,
|
||||||
|
output: (level === 4 && JSON.stringify(data, null, 2)) || undefined,
|
||||||
|
...req.app.locals.logData,
|
||||||
|
}
|
||||||
|
|
||||||
|
elasticsearch.index({
|
||||||
|
index: ELASTICSEARCH_INDEX,
|
||||||
|
document: obj,
|
||||||
|
})
|
||||||
|
})
|
||||||
|
|
||||||
|
return next()
|
||||||
|
}
|
||||||
|
|
||||||
|
export default logMiddleware
|
||||||
|
|
|
||||||
Loading…
Add table
Add a link
Reference in a new issue