Files
Zumri-Backend/cron/notificationCleaning.cron.js
Sathira Sri Sathara b6b345f245 feat: Implement Phase 2 cross-cutting services with email and notification enhancements
- Refactor email verification and password reset utilities to use new email service.
- Introduce email delivery queue and notification delivery model for better tracking.
- Enhance file validation and storage services for improved security and ownership management.
- Add cron job for cleaning inactive notifications with retention policy.
- Update document worker to handle document generation and storage more efficiently.
- Implement logging improvements in activity and log workers.
- Create comprehensive documentation for new API endpoints and services.
- Add unit tests for file validation and notification policies to ensure robustness.
2026-09-03 14:22:34 +05:30

18 lines
1.0 KiB
JavaScript

const cron = require("node-cron");
const { Op } = require("sequelize");
const { sequelize, notification, userNotification } = require("../app/models");
module.exports = function startCleanInactiveNotificationsCron() {
const task = cron.schedule("0 0 * * *", async () => {
const cutoff = new Date(Date.now() - Number(process.env.NOTIFICATION_RETENTION_DAYS || 90) * 86400000);
for (;;) {
const rows = await notification.findAll({ where: { isActive: false, updatedAt: { [Op.lt]: cutoff } }, attributes: ["notification_id"], limit: 500, order: [["updatedAt", "ASC"]] });
if (!rows.length) break;
const ids = rows.map((row) => row.notification_id);
await sequelize.transaction(async (transaction) => { await userNotification.destroy({ where: { notification_id: { [Op.in]: ids } }, transaction }); await notification.destroy({ where: { notification_id: { [Op.in]: ids }, isActive: false }, transaction }); });
if (rows.length < 500) break;
}
}, { scheduled: false });
task.start(); return task;
};