From 32bb7e3b79ebd3901365b1952d1b7191dd28355c Mon Sep 17 00:00:00 2001 From: waruneeauy Date: Tue, 4 Mar 2025 17:51:57 +0700 Subject: [PATCH 1/4] updated condition position & salary --- src/controllers/ProfileSalaryController.ts | 24 +++++++++++++++++++--- 1 file changed, 21 insertions(+), 3 deletions(-) diff --git a/src/controllers/ProfileSalaryController.ts b/src/controllers/ProfileSalaryController.ts index b6d95842..5780e4fc 100644 --- a/src/controllers/ProfileSalaryController.ts +++ b/src/controllers/ProfileSalaryController.ts @@ -19,7 +19,7 @@ import HttpError from "../interfaces/http-error"; import { ProfileSalaryHistory } from "../entities/ProfileSalaryHistory"; import { RequestWithUser } from "../middlewares/user"; import { Profile } from "../entities/Profile"; -import { LessThan, MoreThan } from "typeorm"; +import { In, LessThan, MoreThan } from "typeorm"; import permission from "../interfaces/permission"; import { setLogDataDiff } from "../interfaces/utils"; @Route("api/v1/org/profile/salary") @@ -62,7 +62,7 @@ export class ProfileSalaryController extends Controller { if (_workflow == false) await new permission().PermissionOrgUserGet(req, "SYS_REGISTRY_OFFICER", profileId); const record = await this.salaryRepo.find({ - where: { profileId: profileId }, + where: { profileId: profileId, commandCode: In(["5", "6", "7"]) }, order: { order: "ASC" }, }); return new HttpSuccess(record); @@ -74,7 +74,25 @@ export class ProfileSalaryController extends Controller { if (_workflow == false) await new permission().PermissionOrgUserGet(req, "SYS_REGISTRY_OFFICER", profileId); const record = await this.salaryRepo.find({ - where: { profileId: profileId }, + where: { + profileId: profileId, + commandCode: In([ + "0", + "9", + "1", + "2", + "3", + "4", + "8", + "10", + "11", + "12", + "13", + "14", + "15", + "16", + ]), + }, order: { order: "ASC" }, }); return new HttpSuccess(record); From d3565376043939bc41c0eb63c01801338b7c4498 Mon Sep 17 00:00:00 2001 From: AdisakKanthawilang Date: Tue, 4 Mar 2025 18:05:56 +0700 Subject: [PATCH 2/4] websocket test --- package-lock.json | 31 +++++++++++++++++++++++++++++ package.json | 2 ++ src/app.ts | 29 +++++++++++++++++++++++++++ src/controllers/ReportController.ts | 19 +++++++++++++++++- 4 files changed, 80 insertions(+), 1 deletion(-) diff --git a/package-lock.json b/package-lock.json index bc87192d..ae0f1399 100644 --- a/package-lock.json +++ b/package-lock.json @@ -32,6 +32,7 @@ "tsoa": "^6.0.1", "typeorm": "^0.3.19", "typeorm-cli": "^1.0.7", + "ws": "^8.18.1", "xlsx": "^0.20.2" }, "devDependencies": { @@ -41,6 +42,7 @@ "@types/node": "^20.11.5", "@types/node-cron": "^3.0.11", "@types/swagger-ui-express": "^4.1.6", + "@types/ws": "^8.5.14", "nodemon": "^3.0.3", "prettier": "^3.2.2", "ts-node": "^10.9.2", @@ -563,6 +565,15 @@ "@types/serve-static": "*" } }, + "node_modules/@types/ws": { + "version": "8.5.14", + "resolved": "https://registry.npmjs.org/@types/ws/-/ws-8.5.14.tgz", + "integrity": "sha512-bd/YFLW+URhBzMXurx7lWByOu+xzU9+kb3RboOteXYDfW+tr+JZa99OyNmPINEGB/ahzKrEuc8rcv4gnpJmxTw==", + "dev": true, + "dependencies": { + "@types/node": "*" + } + }, "node_modules/@types/zen-observable": { "version": "0.8.3", "resolved": "https://registry.npmjs.org/@types/zen-observable/-/zen-observable-0.8.3.tgz", @@ -5389,6 +5400,26 @@ "resolved": "https://registry.npmjs.org/wrappy/-/wrappy-1.0.2.tgz", "integrity": "sha512-l4Sp/DRseor9wL6EvV2+TuQn63dMkPjZ/sp9XkghTEbV9KlPS1xUsZ3u7/IQO4wxtcFB4bgpQPRcR3QCvezPcQ==" }, + "node_modules/ws": { + "version": "8.18.1", + "resolved": "https://registry.npmjs.org/ws/-/ws-8.18.1.tgz", + "integrity": "sha512-RKW2aJZMXeMxVpnZ6bck+RswznaxmzdULiBr6KY7XkTnW8uvt0iT9H5DkHUChXrc+uurzwa0rVI16n/Xzjdz1w==", + "engines": { + "node": ">=10.0.0" + }, + "peerDependencies": { + "bufferutil": "^4.0.1", + "utf-8-validate": ">=5.0.2" + }, + "peerDependenciesMeta": { + "bufferutil": { + "optional": true + }, + "utf-8-validate": { + "optional": true + } + } + }, "node_modules/xlsx": { "version": "0.20.2", "resolved": "https://cdn.sheetjs.com/xlsx-0.20.2/xlsx-0.20.2.tgz", diff --git a/package.json b/package.json index 8fbb1317..424b45ba 100644 --- a/package.json +++ b/package.json @@ -22,6 +22,7 @@ "@types/node": "^20.11.5", "@types/node-cron": "^3.0.11", "@types/swagger-ui-express": "^4.1.6", + "@types/ws": "^8.5.14", "nodemon": "^3.0.3", "prettier": "^3.2.2", "ts-node": "^10.9.2", @@ -51,6 +52,7 @@ "tsoa": "^6.0.1", "typeorm": "^0.3.19", "typeorm-cli": "^1.0.7", + "ws": "^8.18.1", "xlsx": "^0.20.2" } } diff --git a/src/app.ts b/src/app.ts index 36df20ea..a1506e2c 100644 --- a/src/app.ts +++ b/src/app.ts @@ -12,6 +12,9 @@ import { RegisterRoutes } from "./routes"; import { OrganizationController } from "./controllers/OrganizationController"; import logMiddleware from "./middlewares/logs"; import { CommandController } from "./controllers/CommandController"; +import { WebSocketServer } from "ws"; +import http from "http"; +export const wss = new WebSocketServer({ noServer: true }); async function main() { await AppDataSource.initialize(); @@ -86,6 +89,32 @@ async function main() { } runMessageQueue(); + + // สร้างเซิร์ฟเวอร์ HTTP + const server = http.createServer(app); + + // การจัดการคำขออัปเกรดจาก HTTP เป็น WebSocket + server.on("upgrade", (request:any, socket:any, head:any) => { + console.log("🔹 Handling upgrade request..."); + wss.handleUpgrade(request, socket, head, (ws:any) => { + console.log("🔹 WebSocket connection established"); + wss.emit("connection", ws, request); + }); + }); + + wss.on("connection", (ws:any) => { + console.log("✅ Client connected to WebSocket"); + + ws.on("close", () => { + console.log("❌ Client disconnected"); + }); + }); + + // เริ่มเซิร์ฟเวอร์ที่พอร์ต 5000 + server.listen(APP_PORT, () => { + console.log("✅ HTTP Server is listening on port 5000"); + }); + } main(); diff --git a/src/controllers/ReportController.ts b/src/controllers/ReportController.ts index 77a8a2ec..3d8b1661 100644 --- a/src/controllers/ReportController.ts +++ b/src/controllers/ReportController.ts @@ -31,6 +31,9 @@ import { Profile } from "../entities/Profile"; import { viewRegistryOfficer } from "../entities/view/viewRegistryOfficer"; import { viewRegistryEmployee } from "../entities/view/viewRegistryEmployee"; import { EmployeeTempPosMaster } from "../entities/EmployeeTempPosMaster"; +import { WebSocket } from "ws"; +import { wss } from "../app"; + @Route("api/v1/org/report") @Tags("Report") @Security("bearerAuth") @@ -3514,7 +3517,21 @@ export class ReportController extends Controller { } } } - return new HttpSuccess({ template: "report2", reportName: "report2", data: { data } }); + // console.log(">>",data); + + wss.clients.forEach((client: any) => { + if (client.readyState === WebSocket.OPEN) { + const message = JSON.stringify({ template: "report2", reportName: "report2", data: { data } }); + console.log("📤 Sending data to client:", message); + client.send(message, (err:any) => { + if (err) { + console.error("❌ Error sending message:", err); + } + }); + } + }); + + // return new HttpSuccess({ template: "report2", reportName: "report2", data: { data } }); } /** From 74644cc70c17b9f2d06e45fe6463d489b74a32d4 Mon Sep 17 00:00:00 2001 From: Bright Date: Tue, 4 Mar 2025 18:21:28 +0700 Subject: [PATCH 3/4] update report --- src/controllers/ReportController.ts | 227 +++++++++++++++++----------- 1 file changed, 138 insertions(+), 89 deletions(-) diff --git a/src/controllers/ReportController.ts b/src/controllers/ReportController.ts index b62a58a1..58388cf1 100644 --- a/src/controllers/ReportController.ts +++ b/src/controllers/ReportController.ts @@ -334,58 +334,82 @@ export class ReportController extends Controller { .orderBy(`registryOfficer.${sortBy}`, sort) .getManyAndCount(); - const mapData = lists.map((x) => ({ - profileId: x.profileId, - citizenId: x.citizenId, - prefix: x.prefix, - firstName: x.firstName, - lastName: x.lastName, - isProbation: x.isProbation, - isLeave: x.isLeave, - isRetirement: x.isRetirement, - leaveType: x.leaveType, - posMasterNo: x.posMasterNo, - orgRootId: x.orgRootId, - orgChild1Id: x.orgChild1Id, - orgChild2Id: x.orgChild2Id, - orgChild3Id: x.orgChild3Id, - orgChild4Id: x.orgChild4Id, - orgRootName: x.orgRootName, - orgChild1Name: x.orgChild1Name, - orgChild2Name: x.orgChild2Name, - orgChild3Name: x.orgChild3Name, - orgChild4Name: x.orgChild4Name, - org: x.org, - searchShortName: x.searchShortName, - posExecutiveName: x.posExecutiveName, - position: x.position, - posTypeName: x.posTypeName, - posLevelName: x.posLevelName, - gender: x.gender, - relationship: x.relationship, - dateAppoint: x.dateAppoint, - dateRetire: x.dateRetire, - dateRetireLaw: x.dateRetireLaw, - birthdate: x.birthdate, - degree: x.degree, - age: x.age, - currentPosition: null, - lengthPosition: null, - positionDate: { - Years: x.Years, - Months: x.Months, - Days: x.Days, - }, - posExcutiveDate: { - Years: x.posExecutiveYears, - Months: x.posExecutiveMonths, - Days: x.posExecutiveDays, - }, - posLevelDate: { - Years: x.levelYears, - Months: x.levelMonths, - Days: x.levelDays, - }, + const mapData = await Promise.all(lists.map(async(x) => { + const position = await AppDataSource.query("CALL GetProfileSalaryPosition(?)", [x.profileId]) + const _position = position.length > 0 ? position[0] : []; + const lastPosition = _position.length > 0 ? _position[_position.length - 1] : null; // last row + const mapPosition = lastPosition + ? [{ + Years: lastPosition.Years ? Math.floor(Number(lastPosition.Years)) : 0, + Months: lastPosition.Months ? Math.floor(Number(lastPosition.Months)) : 0, + Daysday: lastPosition.Days ? Math.floor(Number(lastPosition.Days)) : 0, + }] + : []; + const posLevel = await AppDataSource.query("CALL GetProfileSalaryLevel(?)", [x.profileId]) + const _posLevel = posLevel.length > 0 ? posLevel[0] : []; + const lastPosLevel= _posLevel.length > 0 ? _posLevel[_posLevel.length - 1] : null; + const mapPosLevel = lastPosLevel + ? [{ + Years: lastPosLevel.Years ? Math.floor(Number(lastPosLevel.Years)) : 0, + Months: lastPosLevel.Months ? Math.floor(Number(lastPosLevel.Months)) : 0, + Daysday: lastPosLevel.Days ? Math.floor(Number(lastPosLevel.Days)) : 0, + }] + : []; + return { + profileId: x.profileId, + citizenId: x.citizenId, + prefix: x.prefix, + firstName: x.firstName, + lastName: x.lastName, + isProbation: x.isProbation, + isLeave: x.isLeave, + isRetirement: x.isRetirement, + leaveType: x.leaveType, + posMasterNo: x.posMasterNo, + orgRootId: x.orgRootId, + orgChild1Id: x.orgChild1Id, + orgChild2Id: x.orgChild2Id, + orgChild3Id: x.orgChild3Id, + orgChild4Id: x.orgChild4Id, + orgRootName: x.orgRootName, + orgChild1Name: x.orgChild1Name, + orgChild2Name: x.orgChild2Name, + orgChild3Name: x.orgChild3Name, + orgChild4Name: x.orgChild4Name, + org: x.org, + searchShortName: x.searchShortName, + posExecutiveName: x.posExecutiveName, + position: x.position, + posTypeName: x.posTypeName, + posLevelName: x.posLevelName, + gender: x.gender, + relationship: x.relationship, + dateAppoint: x.dateAppoint, + dateRetire: x.dateRetire, + dateRetireLaw: x.dateRetireLaw, + birthdate: x.birthdate, + degree: x.degree, + age: x.age, + currentPosition: null, + lengthPosition: null, + // positionDate: { + // Years: x.Years, + // Months: x.Months, + // Days: x.Days, + // }, + // posExcutiveDate: { + // Years: x.posExecutiveYears, + // Months: x.posExecutiveMonths, + // Days: x.posExecutiveDays, + // }, + // posLevelDate: { + // Years: x.levelYears, + // Months: x.levelMonths, + // Days: x.levelDays, + // }, + positionDate: mapPosition, + posLevelDate: mapPosLevel + } })); return new HttpSuccess({ data: mapData, @@ -654,43 +678,68 @@ export class ReportController extends Controller { ) .orderBy(`registryEmployee.${sortBy}`, sort) .getManyAndCount(); - const mapData = lists.map((x) => ({ - profileId: x.profileEmployeeId, - citizenId: x.citizenId, - prefix: x.prefix, - firstName: x.firstName, - lastName: x.lastName, - isProbation: x.isProbation, - isLeave: x.isLeave, - isRetirement: x.isRetirement, - leaveType: x.leaveType, - posMasterNo: x.posMasterNo, - orgRootId: x.orgRootId, - orgChild1Id: x.orgChild1Id, - orgChild2Id: x.orgChild2Id, - orgChild3Id: x.orgChild3Id, - orgChild4Id: x.orgChild4Id, - orgRootName: x.orgRootName, - orgChild1Name: x.orgChild1Name, - orgChild2Name: x.orgChild2Name, - orgChild3Name: x.orgChild3Name, - orgChild4Name: x.orgChild4Name, - org: x.org, - searchShortName: x.searchShortName, - position: x.position, - posTypeName: x.posTypeName, - posLevelName: x.posLevelName, - gender: x.gender, - relationship: x.relationship, - dateAppoint: x.dateAppoint, - dateRetire: x.dateRetire, - dateRetireLaw: x.dateRetireLaw, - birthdate: x.birthdate, - degree: x.degree, - age: x.age, - currentPosition: null, - lengthPosition: null, - })); + + const mapData = await Promise.all(lists.map(async(x) => { + const position = await AppDataSource.query("CALL GetProfileEmployeeSalaryPosition(?)", [x.profileEmployeeId]) + const _position = position.length > 0 ? position[0] : []; + const lastPosition = _position.length > 0 ? _position[_position.length - 1] : null; + const mapPosition = lastPosition + ? [{ + Years: lastPosition.Years ? Math.floor(Number(lastPosition.Years)) : 0, + Months: lastPosition.Months ? Math.floor(Number(lastPosition.Months)) : 0, + Daysday: lastPosition.Days ? Math.floor(Number(lastPosition.Days)) : 0, + }] + : []; + const posLevel = await AppDataSource.query("CALL GetProfileEmployeeSalaryLevel(?)", [x.profileEmployeeId]) + const _posLevel = posLevel.length > 0 ? posLevel[0] : []; + const lastPosLevel= _posLevel.length > 0 ? _posLevel[_posLevel.length - 1] : null; + const mapPosLevel = lastPosLevel + ? [{ + Years: lastPosLevel.Years ? Math.floor(Number(lastPosLevel.Years)) : 0, + Months: lastPosLevel.Months ? Math.floor(Number(lastPosLevel.Months)) : 0, + Daysday: lastPosLevel.Days ? Math.floor(Number(lastPosLevel.Days)) : 0, + }] + : []; + return { + profileId: x.profileEmployeeId, + citizenId: x.citizenId, + prefix: x.prefix, + firstName: x.firstName, + lastName: x.lastName, + isProbation: x.isProbation, + isLeave: x.isLeave, + isRetirement: x.isRetirement, + leaveType: x.leaveType, + posMasterNo: x.posMasterNo, + orgRootId: x.orgRootId, + orgChild1Id: x.orgChild1Id, + orgChild2Id: x.orgChild2Id, + orgChild3Id: x.orgChild3Id, + orgChild4Id: x.orgChild4Id, + orgRootName: x.orgRootName, + orgChild1Name: x.orgChild1Name, + orgChild2Name: x.orgChild2Name, + orgChild3Name: x.orgChild3Name, + orgChild4Name: x.orgChild4Name, + org: x.org, + searchShortName: x.searchShortName, + position: x.position, + posTypeName: x.posTypeName, + posLevelName: x.posLevelName, + gender: x.gender, + relationship: x.relationship, + dateAppoint: x.dateAppoint, + dateRetire: x.dateRetire, + dateRetireLaw: x.dateRetireLaw, + birthdate: x.birthdate, + degree: x.degree, + age: x.age, + currentPosition: null, + lengthPosition: null, + positionDate: mapPosition, + posLevelDate: mapPosLevel + } + })); return new HttpSuccess({ data: mapData, total: total, From ad6d694c51b862c393955216a29e8999530ae4b7 Mon Sep 17 00:00:00 2001 From: AdisakKanthawilang Date: Tue, 4 Mar 2025 18:25:31 +0700 Subject: [PATCH 4/4] comment websocket --- src/app.ts | 46 ++++++++++++++--------------- src/controllers/ReportController.ts | 28 +++++++++--------- 2 files changed, 37 insertions(+), 37 deletions(-) diff --git a/src/app.ts b/src/app.ts index a1506e2c..9d14ce21 100644 --- a/src/app.ts +++ b/src/app.ts @@ -12,9 +12,9 @@ import { RegisterRoutes } from "./routes"; import { OrganizationController } from "./controllers/OrganizationController"; import logMiddleware from "./middlewares/logs"; import { CommandController } from "./controllers/CommandController"; -import { WebSocketServer } from "ws"; -import http from "http"; -export const wss = new WebSocketServer({ noServer: true }); +// import { WebSocketServer } from "ws"; +// import http from "http"; +// export const wss = new WebSocketServer({ noServer: true }); async function main() { await AppDataSource.initialize(); @@ -90,30 +90,30 @@ async function main() { runMessageQueue(); - // สร้างเซิร์ฟเวอร์ HTTP - const server = http.createServer(app); + // // สร้างเซิร์ฟเวอร์ HTTP + // const server = http.createServer(app); - // การจัดการคำขออัปเกรดจาก HTTP เป็น WebSocket - server.on("upgrade", (request:any, socket:any, head:any) => { - console.log("🔹 Handling upgrade request..."); - wss.handleUpgrade(request, socket, head, (ws:any) => { - console.log("🔹 WebSocket connection established"); - wss.emit("connection", ws, request); - }); - }); + // // การจัดการคำขออัปเกรดจาก HTTP เป็น WebSocket + // server.on("upgrade", (request:any, socket:any, head:any) => { + // console.log("🔹 Handling upgrade request..."); + // wss.handleUpgrade(request, socket, head, (ws:any) => { + // console.log("🔹 WebSocket connection established"); + // wss.emit("connection", ws, request); + // }); + // }); - wss.on("connection", (ws:any) => { - console.log("✅ Client connected to WebSocket"); + // wss.on("connection", (ws:any) => { + // console.log("✅ Client connected to WebSocket"); - ws.on("close", () => { - console.log("❌ Client disconnected"); - }); - }); + // ws.on("close", () => { + // console.log("❌ Client disconnected"); + // }); + // }); - // เริ่มเซิร์ฟเวอร์ที่พอร์ต 5000 - server.listen(APP_PORT, () => { - console.log("✅ HTTP Server is listening on port 5000"); - }); + // // เริ่มเซิร์ฟเวอร์ที่พอร์ต 5000 + // server.listen(APP_PORT, () => { + // console.log("✅ HTTP Server is listening on port 5000"); + // }); } diff --git a/src/controllers/ReportController.ts b/src/controllers/ReportController.ts index 6452fbfd..dbe9805f 100644 --- a/src/controllers/ReportController.ts +++ b/src/controllers/ReportController.ts @@ -31,8 +31,8 @@ import { Profile } from "../entities/Profile"; import { viewRegistryOfficer } from "../entities/view/viewRegistryOfficer"; import { viewRegistryEmployee } from "../entities/view/viewRegistryEmployee"; import { EmployeeTempPosMaster } from "../entities/EmployeeTempPosMaster"; -import { WebSocket } from "ws"; -import { wss } from "../app"; +// import { WebSocket } from "ws"; +// import { wss } from "../app"; @Route("api/v1/org/report") @Tags("Report") @@ -3516,19 +3516,19 @@ export class ReportController extends Controller { } // console.log(">>",data); - wss.clients.forEach((client: any) => { - if (client.readyState === WebSocket.OPEN) { - const message = JSON.stringify({ template: "report2", reportName: "report2", data: { data } }); - console.log("📤 Sending data to client:", message); - client.send(message, (err:any) => { - if (err) { - console.error("❌ Error sending message:", err); - } - }); - } - }); + // wss.clients.forEach((client: any) => { + // if (client.readyState === WebSocket.OPEN) { + // const message = JSON.stringify({ template: "report2", reportName: "report2", data: { data } }); + // console.log("📤 Sending data to client:", message); + // client.send(message, (err:any) => { + // if (err) { + // console.error("❌ Error sending message:", err); + // } + // }); + // } + // }); - // return new HttpSuccess({ template: "report2", reportName: "report2", data: { data } }); + return new HttpSuccess({ template: "report2", reportName: "report2", data: { data } }); } /**