cron job Revision

This commit is contained in:
AdisakKanthawilang 2024-02-07 15:45:13 +07:00
parent 148fa2c81c
commit 1abf623924
3 changed files with 50 additions and 10 deletions

View file

@ -4,11 +4,11 @@ import cors from "cors";
import express from "express";
import swaggerUi from "swagger-ui-express";
import swaggerDocument from "./swagger.json";
import * as cron from 'node-cron';
import * as cron from "node-cron";
import error from "./middlewares/error";
import { AppDataSource } from "./database/data-source";
import { RegisterRoutes } from "./routes";
import { OrganizationController } from "./controllers/OrganizationController";
async function main() {
await AppDataSource.initialize();
@ -30,10 +30,16 @@ async function main() {
app.use(error);
const APP_HOST = process.env.APP_HOST || "0.0.0.0";
const APP_PORT = +(process.env.APP_PORT || 3000);
// cron.schedule('*/10 * * * * *', () => {
// console.log("cron job....")
// });
const cronTime = "0 0 * * * *"; // ตั้งเวลาทุกวันเวลา 00:00:00
cron.schedule(cronTime, async () => {
try {
const orgController = new OrganizationController();
await orgController.cronjobRevision();
} 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(

View file

@ -1320,4 +1320,39 @@ export class OrganizationController extends Controller {
await this.orgRevisionRepository.save(orgRevisionDraft);
return new HttpSuccess();
}
/**
* Cronjob
*/
async cronjobRevision() {
const today = new Date();
today.setHours(0, 0, 0, 0); // Set time to the beginning of the day
const orgRevisionPublish = await this.orgRevisionRepository
.createQueryBuilder("orgRevision")
.where("orgRevision.orgRevisionIsDraft = false")
.andWhere("orgRevision.orgRevisionIsCurrent = true")
.getOne();
const orgRevisionDraft = await this.orgRevisionRepository
.createQueryBuilder("orgRevision")
.where("orgRevision.orgRevisionIsDraft = true")
.andWhere("orgRevision.orgRevisionIsCurrent = false")
.andWhere("DATE(orgRevision.orgPublishDate) = :today", { today })
.getOne();
if (!orgRevisionDraft) {
return new HttpSuccess();
}
if (orgRevisionPublish) {
orgRevisionPublish.orgRevisionIsDraft = false;
orgRevisionPublish.orgRevisionIsCurrent = false;
await this.orgRevisionRepository.save(orgRevisionPublish);
}
orgRevisionDraft.orgRevisionIsCurrent = true;
orgRevisionDraft.orgRevisionIsDraft = false;
await this.orgRevisionRepository.save(orgRevisionDraft);
return new HttpSuccess();
}
}

View file

@ -99,14 +99,13 @@ export class PositionController extends Controller {
throw new HttpError(HttpStatusCode.NOT_FOUND, "ไม่พบข้อมูล PosLevelId");
}
if (requestBody.posExecutiveId == "") {
requestBody.posExecutiveId = null;
if (posDict.posExecutiveId == "") {
posDict.posExecutiveId = null;
}
if (requestBody.posExecutiveId != null) {
if (posDict.posExecutiveId != null) {
const checkPosExecutiveId = await this.posExecutiveRepository.findOne({
where: { id: requestBody.posExecutiveId },
where: { id: posDict.posExecutiveId },
});
if (!checkPosExecutiveId) {
throw new HttpError(HttpStatusCode.NOT_FOUND, "ไม่พบข้อมูล PosExecutiveId");