feat: api url for file and cron job

This commit is contained in:
elangptra 2024-11-20 08:11:01 +07:00
parent a4d90c6e80
commit 37b0a7681e
2 changed files with 31 additions and 12 deletions

View File

@ -2,6 +2,7 @@ import response from "../../response.js";
import bcrypt from "bcryptjs"; import bcrypt from "bcryptjs";
import jwt from "jsonwebtoken"; import jwt from "jsonwebtoken";
import nodemailer from "nodemailer"; import nodemailer from "nodemailer";
import moment from "moment-timezone";
import models from "../../models/index.js"; import models from "../../models/index.js";
const transporter = nodemailer.createTransport({ const transporter = nodemailer.createTransport({
@ -44,6 +45,7 @@ export const registerAdmin = async (req, res) => {
EMAIL: EMAIL, EMAIL: EMAIL,
PASSWORD: hashedPassword, PASSWORD: hashedPassword,
ROLE: "admin", ROLE: "admin",
IS_VALIDATED: 1,
}); });
const adminResponse = { const adminResponse = {
@ -51,6 +53,7 @@ export const registerAdmin = async (req, res) => {
NAME_USERS: newUser.NAME_USERS, NAME_USERS: newUser.NAME_USERS,
EMAIL: newUser.EMAIL, EMAIL: newUser.EMAIL,
ROLE: newUser.ROLE, ROLE: newUser.ROLE,
IS_VALIDATED: newUser.IS_VALIDATED,
}; };
response(200, adminResponse, "Admin registration successful", res); response(200, adminResponse, "Admin registration successful", res);
@ -119,10 +122,14 @@ export const registerTeacher = async (req, res) => {
await transaction.commit(); await transaction.commit();
const now = moment().tz("Asia/Jakarta");
const midnight = now.clone().endOf("day");
const secondsUntilMidnight = midnight.diff(now, "seconds");
const token = jwt.sign( const token = jwt.sign(
{ userId: newUser.ID }, { userId: newUser.ID },
process.env.VERIFY_TOKEN_SECRET, process.env.VERIFY_TOKEN_SECRET,
{ expiresIn: "1h" } { expiresIn: secondsUntilMidnight }
); );
const validationLink = `${process.env.CLIENT_URL}/validate-email?token=${token}`; const validationLink = `${process.env.CLIENT_URL}/validate-email?token=${token}`;
@ -240,7 +247,7 @@ export const registerTeacher = async (req, res) => {
</div> </div>
<div class="important-note"> <div class="important-note">
<p style="margin: 0;"><strong>Important:</strong> This verification link will expire in 1 hour. If you don't complete the verification within this time, you'll need to register again.</p> <p style="margin: 0;"><strong>Important:</strong> This verification link will expire at 12:00 AM WIB. If you don't complete the verification by this time, you'll need to register again.</p>
</div> </div>
<p>If you didn't create an account with SEALS, please ignore this email.</p> <p>If you didn't create an account with SEALS, please ignore this email.</p>
@ -335,10 +342,14 @@ export const registerStudent = async (req, res) => {
await transaction.commit(); await transaction.commit();
const now = moment().tz("Asia/Jakarta");
const midnight = now.clone().endOf("day");
const secondsUntilMidnight = midnight.diff(now, "seconds");
const token = jwt.sign( const token = jwt.sign(
{ userId: newUser.ID }, { userId: newUser.ID },
process.env.VERIFY_TOKEN_SECRET, process.env.VERIFY_TOKEN_SECRET,
{ expiresIn: "1h" } { expiresIn: secondsUntilMidnight }
); );
const validationLink = `${process.env.CLIENT_URL}/validate-email?token=${token}`; const validationLink = `${process.env.CLIENT_URL}/validate-email?token=${token}`;
@ -456,7 +467,7 @@ export const registerStudent = async (req, res) => {
</div> </div>
<div class="important-note"> <div class="important-note">
<p style="margin: 0;"><strong>Important:</strong> This verification link will expire in 1 hour. If you don't complete the verification within this time, you'll need to register again.</p> <p style="margin: 0;"><strong>Important:</strong> This verification link will expire at 12:00 AM WIB. If you don't complete the verification by this time, you'll need to register again.</p>
</div> </div>
<p>If you didn't create an account with SEALS, please ignore this email.</p> <p>If you didn't create an account with SEALS, please ignore this email.</p>
@ -699,7 +710,12 @@ export const loginUser = async (req, res) => {
} }
if (user.IS_VALIDATED !== 1) { if (user.IS_VALIDATED !== 1) {
return response(403, null, "User is not validated! Please verify your email first.", res); return response(
403,
null,
"User is not validated! Please verify your email first.",
res
);
} }
const validPassword = await bcrypt.compare(PASSWORD, user.PASSWORD); const validPassword = await bcrypt.compare(PASSWORD, user.PASSWORD);

View File

@ -6,6 +6,7 @@ import router from "./routes/index.js";
import cookieParser from "cookie-parser"; import cookieParser from "cookie-parser";
import promBundle from "express-prom-bundle"; import promBundle from "express-prom-bundle";
import cron from "node-cron"; import cron from "node-cron";
import moment from "moment-timezone";
import models from "./models/index.js"; import models from "./models/index.js";
dotenv.config(); dotenv.config();
@ -35,10 +36,13 @@ app.use(cookieParser());
app.use(express.json()); app.use(express.json());
app.use(express.urlencoded({ extended: true })); app.use(express.urlencoded({ extended: true }));
app.use(router); app.use(router);
app.use(express.static("public")); app.use("/api", express.static("public"));
// cron.schedule("0 17 * * *", async () => { ini kalau timezone servernya UTC
cron.schedule("0 0 * * *", async () => { cron.schedule("0 0 * * *", async () => {
const oneDayAgo = new Date(Date.now() - 24 * 60 * 60 * 1000); const now = moment().tz("Asia/Jakarta").format("YYYY-MM-DD HH:mm:ss");
console.log(`Cron job executed at ${now} (WIB)`);
const transaction = await models.db.transaction(); const transaction = await models.db.transaction();
try { try {
@ -46,7 +50,6 @@ cron.schedule("0 0 * * *", async () => {
attributes: ["ID"], attributes: ["ID"],
where: { where: {
IS_VALIDATED: 0, IS_VALIDATED: 0,
TIME_USERS: { [models.Sequelize.Op.lt]: oneDayAgo },
}, },
transaction, transaction,
}); });
@ -65,16 +68,14 @@ cron.schedule("0 0 * * *", async () => {
}); });
await models.User.destroy({ await models.User.destroy({
where: { where: { ID: { [models.Sequelize.Op.in]: unvalidatedUserIDs } },
ID: { [models.Sequelize.Op.in]: unvalidatedUserIDs },
},
transaction, transaction,
}); });
} }
await transaction.commit(); await transaction.commit();
console.log( console.log(
"Removed unvalidated users and their related data registered over 1 day ago." "Removed unvalidated users and their related data"
); );
} catch (error) { } catch (error) {
await transaction.rollback(); await transaction.rollback();
@ -83,6 +84,8 @@ cron.schedule("0 0 * * *", async () => {
error error
); );
} }
}, {
timezone: "Asia/Jakarta"
}); });
app.listen(process.env.APP_PORT, () => { app.listen(process.env.APP_PORT, () => {