Cronjob SalaryPeriod

This commit is contained in:
Bright 2024-02-28 13:16:01 +07:00
parent b93fb6c1e5
commit 382866d1cd
2 changed files with 56 additions and 1 deletions

View file

@ -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,

View file

@ -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
}
});
}
}
}