From 1abf623924bcef77afb8644fcfe3d4e16e33f968 Mon Sep 17 00:00:00 2001 From: AdisakKanthawilang Date: Wed, 7 Feb 2024 15:45:13 +0700 Subject: [PATCH 1/2] cron job Revision --- src/app.ts | 18 ++++++++---- src/controllers/OrganizationController.ts | 35 +++++++++++++++++++++++ src/controllers/PositionController.ts | 7 ++--- 3 files changed, 50 insertions(+), 10 deletions(-) diff --git a/src/app.ts b/src/app.ts index 16f0ae21..f7237f7d 100644 --- a/src/app.ts +++ b/src/app.ts @@ -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( diff --git a/src/controllers/OrganizationController.ts b/src/controllers/OrganizationController.ts index 33177b72..02d4d290 100644 --- a/src/controllers/OrganizationController.ts +++ b/src/controllers/OrganizationController.ts @@ -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(); + } } diff --git a/src/controllers/PositionController.ts b/src/controllers/PositionController.ts index cef27dcb..2c1857e4 100644 --- a/src/controllers/PositionController.ts +++ b/src/controllers/PositionController.ts @@ -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"); From 8bbc6e95556c1db708537e6da80d4a80b90ac130 Mon Sep 17 00:00:00 2001 From: AdisakKanthawilang Date: Wed, 7 Feb 2024 16:36:55 +0700 Subject: [PATCH 2/2] =?UTF-8?q?or=20null=20entity=20=E0=B8=97=E0=B8=B1?= =?UTF-8?q?=E0=B9=89=E0=B8=87=E0=B8=AB=E0=B8=A1=E0=B8=94=20=E0=B8=A1?= =?UTF-8?q?=E0=B8=B5=204=20controller=20=E0=B8=84=E0=B8=B7=E0=B8=AD=20(Pos?= =?UTF-8?q?Master,Position,PosDict=20=E0=B9=81=E0=B8=A5=E0=B8=B0=20Profile?= =?UTF-8?q?)?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- src/entities/Position.ts | 2 +- src/entities/Profile.ts | 4 ++-- 2 files changed, 3 insertions(+), 3 deletions(-) diff --git a/src/entities/Position.ts b/src/entities/Position.ts index 85c336c3..90630067 100644 --- a/src/entities/Position.ts +++ b/src/entities/Position.ts @@ -41,7 +41,7 @@ export class Position extends EntityBase { comment: "ตำแหน่งทางการบริหาร", default: null, }) - posExecutiveId: string; + posExecutiveId: string | null; @Column({ nullable: true, diff --git a/src/entities/Profile.ts b/src/entities/Profile.ts index 9741cb11..10794bb1 100644 --- a/src/entities/Profile.ts +++ b/src/entities/Profile.ts @@ -51,14 +51,14 @@ export class Profile extends EntityBase { length: 40, comment: "ไอดีระดับตำแหน่ง", }) - posLevelId: string; + posLevelId: string | null; @Column({ nullable: true, length: 40, comment: "ไอดีประเภทตำแหน่", }) - posTypeId: string; + posTypeId: string | null; // @Column({ // nullable: true,