diff --git a/src/app.ts b/src/app.ts index cd0edf5e..8e105830 100644 --- a/src/app.ts +++ b/src/app.ts @@ -13,18 +13,17 @@ 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"; export const wss = new WebSocketServer({ noServer: true, - path: "/api/v1/org/socket", + path: "/api/vi/org/socket", }); async function main() { await AppDataSource.initialize(); - + const app = express(); - // สร้างเซิร์ฟเวอร์ HTTP - const server = http.createServer(app); app.use( cors({ @@ -64,7 +63,7 @@ async function main() { console.error("Error executing function from controller:", error); } }); - + const cronTime_Oct = "0 0 1 10 *"; cron.schedule(cronTime_Oct, async () => { try { @@ -74,7 +73,7 @@ async function main() { console.error("Error executing function from controller:", error); } }); - + const cronTime_Tenure = "0 0 * * *"; cron.schedule(cronTime_Tenure, async () => { try { @@ -108,7 +107,9 @@ async function main() { runMessageQueue(); - // การจัดการคำขออัปเกรดจาก HTTP เป็น WebSocket + 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) => { @@ -129,17 +130,11 @@ async function main() { }); }); - // ตั้งค่า Express routes - app.get('/', (req, res) => { - res.send('Hello from Express!'); - }); - server.listen(APP_PORT, APP_HOST, () => { - console.log(`[APP] Application is running on: http://${APP_HOST}:${APP_PORT}`); - console.log(`[APP] Swagger on: http://${APP_HOST}:${APP_PORT}/api-docs`); + console.log(`[APP] Application is running on: http://localhost:${APP_PORT}`); + console.log(`[APP] Swagger on: http://localhost:${APP_PORT}/api-docs`); console.log("[APP] HTTP Server is listening on current port"); }); - } main(); diff --git a/src/controllers/PermissionController.ts b/src/controllers/PermissionController.ts index 77cc61d0..72b7d671 100644 --- a/src/controllers/PermissionController.ts +++ b/src/controllers/PermissionController.ts @@ -84,11 +84,11 @@ export class PermissionController extends Controller { throw new HttpError(HttpStatus.NOT_FOUND, "ไม่พบข้อมูลสิทธิ์"); } } - const getDetail = await this.authRoleRepo.findOne({ select: ["id", "roleName", "roleDescription"], where: { id: posMaster.authRoleId }, }); + if (!getDetail) { throw new HttpError(HttpStatus.NOT_FOUND, "ไม่พบข้อมูล"); } diff --git a/src/controllers/ReportController.ts b/src/controllers/ReportController.ts index a34a9a1e..13fabffb 100644 --- a/src/controllers/ReportController.ts +++ b/src/controllers/ReportController.ts @@ -31,6 +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 { sendWebSocket } from "../services/webSocket"; @Route("api/v1/org/report") @Tags("Report") @@ -1367,6 +1368,7 @@ export class ReportController extends Controller { } } } + return new HttpSuccess({ template: "report1", reportName: "report1", data: { data } }); } @@ -3535,7 +3537,13 @@ export class ReportController extends Controller { } } } +<<<<<<< HEAD +======= + // const metaData = { template: "report2", reportName: "report2", data: { data } }; + // sendWebSocket(metaData) + +>>>>>>> b24a900198a2e83d621975fe9d657157de8ffccf 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..23062aba --- /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); +// } +// }); +// } +// }); +// } \ No newline at end of file