2024-09-13 13:03:35 +00:00
|
|
|
import express from "express";
|
|
|
|
|
import cors from "cors";
|
|
|
|
|
import dotenv from "dotenv";
|
2024-08-12 02:44:06 +00:00
|
|
|
import { testConnection } from "./database/db.js";
|
|
|
|
|
import router from "./routes/index.js";
|
2024-09-13 13:03:35 +00:00
|
|
|
import cookieParser from "cookie-parser";
|
2024-10-28 09:18:25 +00:00
|
|
|
import promBundle from "express-prom-bundle";
|
2024-11-07 02:18:27 +00:00
|
|
|
import cron from "node-cron";
|
2024-11-20 01:11:01 +00:00
|
|
|
import moment from "moment-timezone";
|
2024-11-07 02:18:27 +00:00
|
|
|
import models from "./models/index.js";
|
2024-08-12 02:44:06 +00:00
|
|
|
|
|
|
|
|
dotenv.config();
|
|
|
|
|
const app = express();
|
|
|
|
|
|
2024-10-28 09:18:25 +00:00
|
|
|
const metricsMiddleware = promBundle({
|
|
|
|
|
includeMethod: true,
|
|
|
|
|
includePath: true,
|
|
|
|
|
includeStatusCode: true,
|
|
|
|
|
promClient: {
|
|
|
|
|
collectDefaultMetrics: {},
|
|
|
|
|
},
|
|
|
|
|
});
|
|
|
|
|
|
|
|
|
|
app.use(metricsMiddleware);
|
|
|
|
|
|
2024-09-13 13:03:35 +00:00
|
|
|
const corsOptions = {
|
2024-10-28 09:18:25 +00:00
|
|
|
origin: `${process.env.CLIENT_URL}`,
|
|
|
|
|
methods: ["GET", "POST", "PUT", "DELETE", "OPTIONS"],
|
|
|
|
|
allowedHeaders: ["Content-Type", "Authorization"],
|
|
|
|
|
credentials: true,
|
2024-09-13 13:03:35 +00:00
|
|
|
};
|
|
|
|
|
|
|
|
|
|
app.use(cors(corsOptions));
|
|
|
|
|
|
2024-08-12 02:44:06 +00:00
|
|
|
app.use(cookieParser());
|
|
|
|
|
app.use(express.json());
|
2024-09-13 13:03:35 +00:00
|
|
|
app.use(express.urlencoded({ extended: true }));
|
2024-08-12 02:44:06 +00:00
|
|
|
app.use(router);
|
2024-11-20 12:16:07 +00:00
|
|
|
app.use("/api/media", express.static("public"));
|
2024-08-12 02:44:06 +00:00
|
|
|
|
2024-11-20 01:11:01 +00:00
|
|
|
// cron.schedule("0 17 * * *", async () => { ini kalau timezone servernya UTC
|
2024-11-07 02:18:27 +00:00
|
|
|
cron.schedule("0 0 * * *", async () => {
|
2024-11-20 01:11:01 +00:00
|
|
|
const now = moment().tz("Asia/Jakarta").format("YYYY-MM-DD HH:mm:ss");
|
|
|
|
|
console.log(`Cron job executed at ${now} (WIB)`);
|
|
|
|
|
|
2024-11-07 02:18:27 +00:00
|
|
|
const transaction = await models.db.transaction();
|
|
|
|
|
|
|
|
|
|
try {
|
|
|
|
|
const unvalidatedUsers = await models.User.findAll({
|
|
|
|
|
attributes: ["ID"],
|
|
|
|
|
where: {
|
|
|
|
|
IS_VALIDATED: 0,
|
|
|
|
|
},
|
|
|
|
|
transaction,
|
|
|
|
|
});
|
|
|
|
|
|
|
|
|
|
const unvalidatedUserIDs = unvalidatedUsers.map((user) => user.ID);
|
|
|
|
|
|
|
|
|
|
if (unvalidatedUserIDs.length > 0) {
|
|
|
|
|
await models.Student.destroy({
|
|
|
|
|
where: { ID: { [models.Sequelize.Op.in]: unvalidatedUserIDs } },
|
|
|
|
|
transaction,
|
|
|
|
|
});
|
|
|
|
|
|
|
|
|
|
await models.Teacher.destroy({
|
|
|
|
|
where: { ID: { [models.Sequelize.Op.in]: unvalidatedUserIDs } },
|
|
|
|
|
transaction,
|
|
|
|
|
});
|
|
|
|
|
|
|
|
|
|
await models.User.destroy({
|
2024-11-20 01:11:01 +00:00
|
|
|
where: { ID: { [models.Sequelize.Op.in]: unvalidatedUserIDs } },
|
2024-11-07 02:18:27 +00:00
|
|
|
transaction,
|
|
|
|
|
});
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
await transaction.commit();
|
|
|
|
|
console.log(
|
2024-11-20 01:11:01 +00:00
|
|
|
"Removed unvalidated users and their related data"
|
2024-11-07 02:18:27 +00:00
|
|
|
);
|
|
|
|
|
} catch (error) {
|
|
|
|
|
await transaction.rollback();
|
|
|
|
|
console.error(
|
|
|
|
|
"Failed to delete unvalidated users and related data:",
|
|
|
|
|
error
|
|
|
|
|
);
|
|
|
|
|
}
|
2024-11-20 01:11:01 +00:00
|
|
|
}, {
|
|
|
|
|
timezone: "Asia/Jakarta"
|
2024-11-07 02:18:27 +00:00
|
|
|
});
|
|
|
|
|
|
2024-08-12 02:44:06 +00:00
|
|
|
app.listen(process.env.APP_PORT, () => {
|
2024-09-13 13:03:35 +00:00
|
|
|
testConnection();
|
2024-10-28 09:18:25 +00:00
|
|
|
console.log(`Server running on port ${process.env.APP_PORT}`);
|
2024-09-13 13:03:35 +00:00
|
|
|
});
|