From 382866d1cdc1858e3edfc59e9763fce7cdf53495 Mon Sep 17 00:00:00 2001 From: Bright Date: Wed, 28 Feb 2024 13:16:01 +0700 Subject: [PATCH] Cronjob SalaryPeriod --- src/app.ts | 12 +++++- src/controllers/SalaryPeriodController.ts | 45 +++++++++++++++++++++++ 2 files changed, 56 insertions(+), 1 deletion(-) diff --git a/src/app.ts b/src/app.ts index 6efbcf9..b0180b1 100644 --- a/src/app.ts +++ b/src/app.ts @@ -8,6 +8,7 @@ import * as cron from "node-cron"; import error from "./middlewares/error"; import { AppDataSource } from "./database/data-source"; import { RegisterRoutes } from "./routes"; +import { SalaryPeriodController } from "./controllers/SalaryPeriodController"; async function main() { await AppDataSource.initialize(); @@ -25,11 +26,20 @@ async function main() { app.use("/api-docs", swaggerUi.serve, swaggerUi.setup(swaggerDocument)); RegisterRoutes(app); - + app.use(error); const APP_HOST = process.env.APP_HOST || "0.0.0.0"; const APP_PORT = +(process.env.APP_PORT || 3000); + const cronTime = "0 1 * * *"; // ตั้งเวลาทุกวันเวลา 01:00:00 + cron.schedule(cronTime, async () => { + try { + const salaryPeriod = new SalaryPeriodController(); + await salaryPeriod.CronjobSalaryPeriod(); + } catch (error) { + console.error("Error executing function from controller:", error); + } + }); app.listen( APP_PORT, APP_HOST, diff --git a/src/controllers/SalaryPeriodController.ts b/src/controllers/SalaryPeriodController.ts index 83035d0..910d0b5 100644 --- a/src/controllers/SalaryPeriodController.ts +++ b/src/controllers/SalaryPeriodController.ts @@ -872,4 +872,49 @@ export class SalaryPeriodController extends Controller { return new HttpSuccess(); } + + /** + * Cronjob SalaryPeriod + */ + async CronjobSalaryPeriod() { + const current = new Date(); + let salaryPeriod: any + // console.log(current.getDate(), current.getMonth() , current.getFullYear()) + if(current.getDate() == 1 && current.getMonth() == 2){ + salaryPeriod = await this.salaryPeriodRepository.findOne({ + where: { + year: current.getFullYear(), + period: "MAR", + isActive: true + } + }); + } + else if(current.getDate() == 1 && current.getMonth() == 3){ + salaryPeriod = await this.salaryPeriodRepository.findOne({ + where: { + year: current.getFullYear(), + period: "APR", + isActive: true + } + }); + } + else if(current.getDate() == 1 && current.getMonth() == 8){ + salaryPeriod = await this.salaryPeriodRepository.findOne({ + where: { + year: current.getFullYear(), + period: "SEP", + isActive: true + } + }); + } + else if(current.getDate() == 1 && current.getMonth() == 9){ + salaryPeriod = await this.salaryPeriodRepository.findOne({ + where: { + year: current.getFullYear(), + period: "OCT", + isActive: true + } + }); + } + } }