diff --git a/src/app.ts b/src/app.ts index fbd6df0d..8c9cad7f 100644 --- a/src/app.ts +++ b/src/app.ts @@ -12,10 +12,9 @@ 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"; - export const wss = new WebSocketServer({ noServer: true, path: "/api/v1/org/socket", }); @@ -38,14 +37,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); @@ -85,6 +84,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, @@ -126,7 +138,7 @@ async function main() { }); }); - // ตั้งค่า Express routes + // ตั้งค่า Express routes app.get('/', (req, res) => { res.send('Hello from Express!'); }); 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 diff --git a/src/controllers/ReportController.ts b/src/controllers/ReportController.ts index a33ac005..737eafcc 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); + } + }); + } + }); +}