feat: complete day 4
This commit is contained in:
@@ -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);
|
||||
}
|
||||
})();
|
||||
Reference in New Issue
Block a user