feat: complete day 4

This commit is contained in:
Ayobami
2025-07-11 20:19:55 +01:00
parent 2b58c1d8b0
commit 95edc56088
10 changed files with 509 additions and 49 deletions
+56
View File
@@ -0,0 +1,56 @@
const db = require("../models");
const { Op } = require("sequelize");
(async () => {
try {
const User = db.user;
const Email = db.email;
const EmailQueue = db.email_queue;
// 1. Get all active users
const users = await User.findAll({ where: { status: 1 } });
if (!users.length) {
console.log("No active users found.");
return;
}
// 2. Determine day and select emails
const today = new Date();
const day = today.getDay(); // 0=Sun, 1=Mon, ...
const isOddDay = day === 1 || day === 3 || day === 5; // Mon, Wed, Fri
const emailWhere = isOddDay
? { id: { [Op.mod]: [2, 1] } } // odd ids
: { id: { [Op.mod]: [2, 0] } }; // even ids
const emails = await Email.findAll({ where: emailWhere });
if (!emails.length) {
console.log("No emails found for today.");
return;
}
// 3. For each user and email, insert into email_queue
const tomorrow = new Date(today);
tomorrow.setDate(today.getDate() + 1);
const sendAt = tomorrow;
let count = 0;
for (const user of users) {
for (const email of emails) {
await EmailQueue.create({
email_id: email.id,
user_id: user.id,
status: 0, // not sent
created_at: today,
send_at: sendAt,
updated_at: today,
});
count++;
}
}
console.log(
`Inserted ${count} email_queue records for ${users.length} users and ${emails.length} emails.`
);
process.exit(0);
} catch (err) {
console.error("Error in email_queue cronjob:", err);
process.exit(1);
}
})();
+66
View File
@@ -0,0 +1,66 @@
const db = require("../models");
const nodemailer = require("nodemailer");
require("dotenv").config();
(async () => {
try {
const EmailQueue = db.email_queue;
const Email = db.email;
const User = db.user;
const today = new Date();
today.setHours(0, 0, 0, 0);
const tomorrow = new Date(today);
tomorrow.setDate(today.getDate() + 1);
// 1. Get all email_queue entries for today and not sent
const queues = await EmailQueue.findAll({
where: {
send_at: { $gte: today, $lt: tomorrow },
status: 0,
},
});
if (!queues.length) {
console.log("No emails to send today.");
return;
}
// 2. Setup nodemailer with Mailtrap
const transporter = nodemailer.createTransport({
host: process.env.MAILTRAP_HOST,
port: process.env.MAILTRAP_PORT,
auth: {
user: process.env.MAILTRAP_USER,
pass: process.env.MAILTRAP_PASS,
},
});
let sentCount = 0;
for (const queue of queues) {
// 3. Get user and email
const user = await User.findByPk(queue.user_id);
const email = await Email.findByPk(queue.email_id);
if (!user || !email) continue;
// 4. Replace template
let body = email.body
.replace(/\{\{\{NAME\}\}\}/g, user.name)
.replace(/\{\{\{EMAIL\}\}\}/g, user.email);
// 5. Send email
await transporter.sendMail({
from: "noreply@example.com",
to: user.email,
subject: email.subject,
html: body,
});
// 6. Mark as sent
queue.status = 1;
queue.updated_at = new Date();
await queue.save();
sentCount++;
}
console.log(`Sent ${sentCount} emails.`);
process.exit(0);
} catch (err) {
console.error("Error in email_sending cronjob:", err);
process.exit(1);
}
})();