hrms-api-org/src/app.ts

117 lines
3.7 KiB
TypeScript
Raw Normal View History

2024-01-24 11:39:00 +07:00
import "dotenv/config";
import "reflect-metadata";
import cors from "cors";
import express from "express";
import swaggerUi from "swagger-ui-express";
import swaggerDocument from "./swagger.json";
2024-02-07 15:45:13 +07:00
import * as cron from "node-cron";
2024-10-16 11:55:45 +07:00
import { init as rabbitmqInit } from "./services/rabbitmq";
2024-01-24 11:39:00 +07:00
import error from "./middlewares/error";
2024-01-25 10:52:44 +07:00
import { AppDataSource } from "./database/data-source";
2024-01-24 11:39:00 +07:00
import { RegisterRoutes } from "./routes";
2024-02-07 15:45:13 +07:00
import { OrganizationController } from "./controllers/OrganizationController";
import logMiddleware from "./middlewares/logs";
2024-10-11 11:05:31 +07:00
import { CommandController } from "./controllers/CommandController";
2025-03-06 13:00:02 +07:00
import { ProfileSalaryController } from "./controllers/ProfileSalaryController";
2025-10-22 23:20:18 +07:00
import { DateSerializer } from "./interfaces/date-serializer";
2025-03-06 18:25:27 +07:00
2025-03-31 16:59:14 +07:00
import { initWebSocket } from "./services/webSocket";
2025-03-06 16:33:47 +07:00
2024-01-24 11:39:00 +07:00
async function main() {
2024-01-25 10:52:44 +07:00
await AppDataSource.initialize();
2025-03-11 12:50:11 +07:00
2025-10-22 23:20:18 +07:00
// Setup custom Date serialization for local timezone
DateSerializer.setupDateSerialization();
2025-03-31 16:59:14 +07:00
initWebSocket();
2025-03-11 12:50:11 +07:00
2024-01-24 11:39:00 +07:00
const app = express();
2024-01-26 16:05:18 +07:00
app.use(
cors({
origin: "*",
}),
);
2025-07-22 21:21:21 +07:00
app.use(express.json({ limit: "50mb" }));
2025-07-07 10:08:30 +07:00
app.use(express.urlencoded({ extended: true, limit: "50mb" }));
2025-03-14 13:26:37 +07:00
app.use(logMiddleware);
2024-01-24 11:39:00 +07:00
app.use("/", express.static("static"));
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);
2024-02-07 15:45:13 +07:00
2025-07-08 23:27:41 +07:00
const cronTime = "0 0 3 * * *"; // ตั้งเวลาทุกวันเวลา 08:00:00
// const cronTime = "*/10 * * * * *";
2024-02-07 15:45:13 +07:00
cron.schedule(cronTime, async () => {
try {
const orgController = new OrganizationController();
await orgController.cronjobRevision();
} catch (error) {
console.error("Error executing function from controller:", error);
}
});
2024-01-24 11:39:00 +07:00
2025-07-08 23:27:41 +07:00
const cronTime_command = "0 0 2 * * *";
2024-10-11 11:05:31 +07:00
// const cronTime_command = "*/10 * * * * *";
cron.schedule(cronTime_command, async () => {
try {
const commandController = new CommandController();
await commandController.cronjobCommand();
} catch (error) {
console.error("Error executing function from controller:", error);
}
});
2025-03-06 16:33:47 +07:00
2025-11-05 10:07:22 +07:00
const cronTime_Oct = "0 0 1 10 *";
cron.schedule(cronTime_Oct, async () => {
try {
const commandController = new CommandController();
await commandController.cronjobUpdateRetirementStatus();
} catch (error) {
console.error("Error executing function from controller:", error);
}
});
2025-03-06 16:33:47 +07:00
2025-07-08 23:27:41 +07:00
const cronTime_Tenure = "0 0 0 * * *";
2025-03-06 13:00:02 +07:00
cron.schedule(cronTime_Tenure, async () => {
try {
const profileSalaryController = new ProfileSalaryController();
await profileSalaryController.cronjobTenurePositionOfficer();
await profileSalaryController.cronjobTenureLevelOfficer();
await profileSalaryController.cronjobTenureExecutivePositionOfficer();
2025-03-06 13:00:02 +07:00
await profileSalaryController.cronjobTenurePositionEmployee();
await profileSalaryController.cronjobTenureLevelEmployee();
await profileSalaryController.Registry();
await profileSalaryController.RegistryEmployee();
2025-03-06 13:00:02 +07:00
} catch (error) {
console.error("Error executing function from controller:", error);
}
});
2024-01-24 11:39:00 +07:00
// app.listen(APP_PORT, APP_HOST, () => console.log(`Listening on: http://localhost:${APP_PORT}`));
2025-03-07 16:29:28 +07:00
app.listen(
APP_PORT,
APP_HOST,
() => (
console.log(`[APP] Application is running on: http://localhost:${APP_PORT}`),
console.log(`[APP] Swagger on: http://localhost:${APP_PORT}/api-docs`)
),
);
2024-10-11 11:05:31 +07:00
async function runMessageQueue() {
try {
await rabbitmqInit();
} catch (e) {
console.log(e);
setTimeout(runMessageQueue, 1000);
}
}
2024-10-16 11:55:45 +07:00
runMessageQueue();
2024-01-24 11:39:00 +07:00
}
main();