first commit

This commit is contained in:
Isuru Bimsara
2026-08-14 22:46:02 +05:30
commit 81d0e65686
132 changed files with 24398 additions and 0 deletions
+98
View File
@@ -0,0 +1,98 @@
/**
* Copyright (c) 2026 Niolla
* All rights reserved.
*
* This source code is proprietary and confidential.
* Unauthorized copying, modification, distribution, or use
* of this file, via any medium, is strictly prohibited.
*/
// cron/cleanInactiveNotifications.cron.js
const cron = require("node-cron");
const {
sequelize,
notification,
userNotification,
} = require("../app/models");
function startCleanInactiveNotificationsCron() {
console.log("Inactive Notification Cleanup Cron Started");
const task = cron.schedule(
"0 0 * * *", // Run every day at midnight
async () => {
console.log(
`[CRON] Cleaning inactive notifications... ${new Date().toISOString()}`,
);
try {
const inactiveNotifications = await notification.findAll({
where: {
isActive: false,
},
attributes: ["notification_id"],
});
if (!inactiveNotifications.length) {
console.log("[CRON] No inactive notifications found.");
return;
}
const notificationIds = inactiveNotifications.map(
(item) => item.notification_id,
);
const transaction = await sequelize.transaction();
try {
// Delete user-notification relationships first
const deletedUserNotifications = await userNotification.destroy({
where: {
notification_id: notificationIds,
},
transaction,
});
// Delete inactive notifications
const deletedNotifications = await notification.destroy({
where: {
notification_id: notificationIds,
isActive: false,
},
transaction,
});
await transaction.commit();
console.log(
`[CRON] Cleanup completed. Deleted ${deletedNotifications} notifications and ${deletedUserNotifications} user notification records.`,
);
} catch (err) {
await transaction.rollback();
console.error(
"[CRON] Failed to clean inactive notifications:",
err,
);
}
} catch (err) {
console.error(
"[CRON] Error while checking inactive notifications:",
err,
);
}
},
{
scheduled: false,
},
);
task.start();
return task;
}
module.exports = startCleanInactiveNotificationsCron;