From e31e98b316fd32393ba9eb9114fd63e5ee84acbd Mon Sep 17 00:00:00 2001 From: Bright Date: Thu, 6 Mar 2025 13:00:02 +0700 Subject: [PATCH 1/8] job --- src/app.ts | 15 ++++++++++++++- src/controllers/ProfileSalaryController.ts | 4 ++-- 2 files changed, 16 insertions(+), 3 deletions(-) diff --git a/src/app.ts b/src/app.ts index fbd6df0d..5c10c6ef 100644 --- a/src/app.ts +++ b/src/app.ts @@ -12,7 +12,7 @@ import { RegisterRoutes } from "./routes"; import { OrganizationController } from "./controllers/OrganizationController"; import logMiddleware from "./middlewares/logs"; import { CommandController } from "./controllers/CommandController"; - +import { ProfileSalaryController } from "./controllers/ProfileSalaryController"; import { WebSocketServer } from "ws"; import http from "http"; @@ -85,6 +85,19 @@ async function main() { } }); + const cronTime_Tenure = "0 0 * * *"; + cron.schedule(cronTime_Tenure, async () => { + try { + const profileSalaryController = new ProfileSalaryController(); + await profileSalaryController.cronjobTenurePositionOfficer(); + await profileSalaryController.cronjobTenureLevelOfficer(); + await profileSalaryController.cronjobTenurePositionEmployee(); + await profileSalaryController.cronjobTenureLevelEmployee(); + } catch (error) { + console.error("Error executing function from controller:", error); + } + }); + // app.listen(APP_PORT, APP_HOST, () => console.log(`Listening on: http://localhost:${APP_PORT}`)); // app.listen( // APP_PORT, diff --git a/src/controllers/ProfileSalaryController.ts b/src/controllers/ProfileSalaryController.ts index 29d4dd9b..b0d128ba 100644 --- a/src/controllers/ProfileSalaryController.ts +++ b/src/controllers/ProfileSalaryController.ts @@ -86,7 +86,7 @@ export class ProfileSalaryController extends Controller { await this.positionEmployeeRepo.clear(); const profile = await this.profileEmployeeRepo.find(); for await (const x of profile) { - const position = await AppDataSource.query("CALL GetProfileSalaryPosition(?)", [x.id]); + const position = await AppDataSource.query("CALL GetProfileEmployeeSalaryPosition(?)", [x.id]); const _position = position.length > 0 ? position[0] : []; const mapPosition = _position.length > 1 @@ -176,7 +176,7 @@ export class ProfileSalaryController extends Controller { await this.levelEmployeeRepo.clear(); const profile = await this.profileEmployeeRepo.find({ relations: ["posLevel"] }); for await (const x of profile) { - const positionLevel = await AppDataSource.query("CALL GetProfileSalaryLevel(?)", [x.id]); + const positionLevel = await AppDataSource.query("CALL GetProfileEmployeeSalaryLevel(?)", [x.id]); const _positionLevel = positionLevel.length > 0 ? positionLevel[0] : []; const mapPositionLevel = _positionLevel.length > 1 From 7d3847ac0b00896219ca0a55d41d705bcb544f9e Mon Sep 17 00:00:00 2001 From: AdisakKanthawilang Date: Thu, 6 Mar 2025 13:47:40 +0700 Subject: [PATCH 2/8] webSocket test --- src/app.ts | 12 ++++++++++++ src/controllers/ReportController.ts | 2 +- 2 files changed, 13 insertions(+), 1 deletion(-) diff --git a/src/app.ts b/src/app.ts index fbd6df0d..c8551023 100644 --- a/src/app.ts +++ b/src/app.ts @@ -126,6 +126,18 @@ async function main() { }); }); + wss.clients.forEach((client: any) => { + if (client.readyState === WebSocket.OPEN) { + const message = ""; + console.log("📤 Sending data to client:", message); + client.send(message, (err:any) => { + if (err) { + console.error("❌ Error sending message:", err); + } + }); + } + }); + // ตั้งค่า Express routes app.get('/', (req, res) => { res.send('Hello from Express!'); diff --git a/src/controllers/ReportController.ts b/src/controllers/ReportController.ts index a33ac005..8e2f2ad4 100644 --- a/src/controllers/ReportController.ts +++ b/src/controllers/ReportController.ts @@ -3554,7 +3554,7 @@ export class ReportController extends Controller { // } // }); - // return new HttpSuccess({ template: "report2", reportName: "report2", data: { data } }); + return new HttpSuccess({ template: "report2", reportName: "report2", data: { data } }); } /** From 92de3059b2d5ee0d189b84498445cb8c29b8131a Mon Sep 17 00:00:00 2001 From: AdisakKanthawilang Date: Thu, 6 Mar 2025 13:59:58 +0700 Subject: [PATCH 3/8] webSocker test --- src/controllers/ReportController.ts | 8 ++++---- 1 file changed, 4 insertions(+), 4 deletions(-) diff --git a/src/controllers/ReportController.ts b/src/controllers/ReportController.ts index 8e2f2ad4..8030578d 100644 --- a/src/controllers/ReportController.ts +++ b/src/controllers/ReportController.ts @@ -32,8 +32,8 @@ 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") @@ -3542,7 +3542,7 @@ export class ReportController extends Controller { } // console.log(">>",data); - // wss.clients.forEach((client: any) => { + 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); @@ -3552,7 +3552,7 @@ export class ReportController extends Controller { // } // }); // } - // }); + }); return new HttpSuccess({ template: "report2", reportName: "report2", data: { data } }); } From a828693d2e83284c097052483de68bdcef98e10b Mon Sep 17 00:00:00 2001 From: AdisakKanthawilang Date: Thu, 6 Mar 2025 14:08:22 +0700 Subject: [PATCH 4/8] reverse --- src/app.ts | 2 +- src/controllers/ReportController.ts | 8 ++++---- 2 files changed, 5 insertions(+), 5 deletions(-) diff --git a/src/app.ts b/src/app.ts index 42d39d9d..ef51cf64 100644 --- a/src/app.ts +++ b/src/app.ts @@ -151,7 +151,7 @@ async function main() { } }); - // ตั้งค่า Express routes + // ตั้งค่า Express routes app.get('/', (req, res) => { res.send('Hello from Express!'); }); diff --git a/src/controllers/ReportController.ts b/src/controllers/ReportController.ts index 8030578d..8e2f2ad4 100644 --- a/src/controllers/ReportController.ts +++ b/src/controllers/ReportController.ts @@ -32,8 +32,8 @@ 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") @@ -3542,7 +3542,7 @@ export class ReportController extends Controller { } // console.log(">>",data); - wss.clients.forEach((client: any) => { + // 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); @@ -3552,7 +3552,7 @@ export class ReportController extends Controller { // } // }); // } - }); + // }); return new HttpSuccess({ template: "report2", reportName: "report2", data: { data } }); } From 0f639826c352b91dde87e7904ac89582b7642ff1 Mon Sep 17 00:00:00 2001 From: AdisakKanthawilang Date: Thu, 6 Mar 2025 14:41:41 +0700 Subject: [PATCH 5/8] =?UTF-8?q?webSocket=20=E0=B8=AA=E0=B8=B2=E0=B8=98?= =?UTF-8?q?=E0=B8=B899?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- src/app.ts | 26 +++++++------------------- src/controllers/ReportController.ts | 22 ++++------------------ src/services/webSocket.ts | 16 ++++++++++++++++ 3 files changed, 27 insertions(+), 37 deletions(-) create mode 100644 src/services/webSocket.ts diff --git a/src/app.ts b/src/app.ts index ef51cf64..1fcfdb76 100644 --- a/src/app.ts +++ b/src/app.ts @@ -38,14 +38,14 @@ async function main() { app.use("/", express.static("static")); app.use("/api-docs", swaggerUi.serve, swaggerUi.setup(swaggerDocument)); - app.use((req, res, next) => { - console.log(`Request received at: ${req.url}`); - next(); - }); + // app.use((req, res, next) => { + // console.log(`Request received at: ${req.url}`); + // next(); + // }); - app.get('/api', (req, res) => { - res.send('API route is working!'); - }); + // app.get('/api', (req, res) => { + // res.send('API route is working!'); + // }); RegisterRoutes(app); @@ -139,18 +139,6 @@ async function main() { }); }); - wss.clients.forEach((client: any) => { - if (client.readyState === WebSocket.OPEN) { - const message = ""; - console.log("📤 Sending data to client:", message); - client.send(message, (err:any) => { - if (err) { - console.error("❌ Error sending message:", err); - } - }); - } - }); - // ตั้งค่า Express routes app.get('/', (req, res) => { res.send('Hello from Express!'); diff --git a/src/controllers/ReportController.ts b/src/controllers/ReportController.ts index 8e2f2ad4..d1847536 100644 --- a/src/controllers/ReportController.ts +++ b/src/controllers/ReportController.ts @@ -31,9 +31,7 @@ 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 { sendWebSocket } from "../services/webSocket"; @Route("api/v1/org/report") @Tags("Report") @@ -3540,21 +3538,9 @@ 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); - // } - // }); - // } - // }); - - return new HttpSuccess({ template: "report2", reportName: "report2", data: { data } }); + const metaData = { template: "report2", reportName: "report2", data: { data } }; + sendWebSocket(metaData) + // return new HttpSuccess({ template: "report2", reportName: "report2", data: { data } }); } /** diff --git a/src/services/webSocket.ts b/src/services/webSocket.ts new file mode 100644 index 00000000..32ea70af --- /dev/null +++ b/src/services/webSocket.ts @@ -0,0 +1,16 @@ +import { WebSocket } from "ws"; +import { wss } from "../app"; + +export async function sendWebSocket(data:any){ + wss.clients.forEach((client: any) => { + if (client.readyState === WebSocket.OPEN) { + const message = JSON.stringify(data); + console.log("📤 Sending data to client:", message); + client.send(message, (err:any) => { + if (err) { + console.error("❌ Error sending message:", err); + } + }); + } + }); +} From 42cb907cfcf76f6a3d6cf9f215a4a0068c627551 Mon Sep 17 00:00:00 2001 From: AdisakKanthawilang Date: Thu, 6 Mar 2025 14:55:02 +0700 Subject: [PATCH 6/8] ws test --- src/app.ts | 5 +---- src/services/webSocket.ts | 6 ++++-- 2 files changed, 5 insertions(+), 6 deletions(-) diff --git a/src/app.ts b/src/app.ts index 1fcfdb76..8ea1260c 100644 --- a/src/app.ts +++ b/src/app.ts @@ -13,12 +13,9 @@ import { OrganizationController } from "./controllers/OrganizationController"; import logMiddleware from "./middlewares/logs"; import { CommandController } from "./controllers/CommandController"; import { ProfileSalaryController } from "./controllers/ProfileSalaryController"; -import { WebSocketServer } from "ws"; import http from "http"; +import { wss } from "../src/services/webSocket"; -export const wss = new WebSocketServer({ noServer: true, - path: "/api/v1/org/socket", - }); async function main() { await AppDataSource.initialize(); diff --git a/src/services/webSocket.ts b/src/services/webSocket.ts index 32ea70af..ff8ee3e1 100644 --- a/src/services/webSocket.ts +++ b/src/services/webSocket.ts @@ -1,5 +1,7 @@ -import { WebSocket } from "ws"; -import { wss } from "../app"; +import { WebSocket, WebSocketServer } from "ws"; +export const wss = new WebSocketServer({ noServer: true, + path: "/api/v1/org/socket", + }); export async function sendWebSocket(data:any){ wss.clients.forEach((client: any) => { From 4de905972c69d0fbec1d55a8b1eed945f28ce80c Mon Sep 17 00:00:00 2001 From: AdisakKanthawilang Date: Thu, 6 Mar 2025 15:04:13 +0700 Subject: [PATCH 7/8] ws test --- src/app.ts | 6 ++++-- src/controllers/ReportController.ts | 2 +- src/services/webSocket.ts | 6 ++---- 3 files changed, 7 insertions(+), 7 deletions(-) diff --git a/src/app.ts b/src/app.ts index 8ea1260c..8c9cad7f 100644 --- a/src/app.ts +++ b/src/app.ts @@ -13,9 +13,11 @@ import { OrganizationController } from "./controllers/OrganizationController"; import logMiddleware from "./middlewares/logs"; import { CommandController } from "./controllers/CommandController"; import { ProfileSalaryController } from "./controllers/ProfileSalaryController"; +import { WebSocketServer } from "ws"; import http from "http"; -import { wss } from "../src/services/webSocket"; - +export const wss = new WebSocketServer({ noServer: true, + path: "/api/v1/org/socket", + }); async function main() { await AppDataSource.initialize(); diff --git a/src/controllers/ReportController.ts b/src/controllers/ReportController.ts index d1847536..1a575842 100644 --- a/src/controllers/ReportController.ts +++ b/src/controllers/ReportController.ts @@ -3540,7 +3540,7 @@ export class ReportController extends Controller { } const metaData = { template: "report2", reportName: "report2", data: { data } }; sendWebSocket(metaData) - // return new HttpSuccess({ template: "report2", reportName: "report2", data: { data } }); + return new HttpSuccess(); } /** diff --git a/src/services/webSocket.ts b/src/services/webSocket.ts index ff8ee3e1..32ea70af 100644 --- a/src/services/webSocket.ts +++ b/src/services/webSocket.ts @@ -1,7 +1,5 @@ -import { WebSocket, WebSocketServer } from "ws"; -export const wss = new WebSocketServer({ noServer: true, - path: "/api/v1/org/socket", - }); +import { WebSocket } from "ws"; +import { wss } from "../app"; export async function sendWebSocket(data:any){ wss.clients.forEach((client: any) => { From 3a126130cdadcfc0aa16aeaa22e88d85e56cf573 Mon Sep 17 00:00:00 2001 From: AdisakKanthawilang Date: Thu, 6 Mar 2025 15:13:01 +0700 Subject: [PATCH 8/8] reverse --- src/controllers/ReportController.ts | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/src/controllers/ReportController.ts b/src/controllers/ReportController.ts index 1a575842..737eafcc 100644 --- a/src/controllers/ReportController.ts +++ b/src/controllers/ReportController.ts @@ -3539,8 +3539,8 @@ export class ReportController extends Controller { } } const metaData = { template: "report2", reportName: "report2", data: { data } }; - sendWebSocket(metaData) - return new HttpSuccess(); + // sendWebSocket(metaData) + return new HttpSuccess({ template: "report2", reportName: "report2", data: { data } }); } /**