Files
permit-api/cron/notificationCleanup.js

27 lines
618 B
JavaScript

import mongoose from "mongoose";
const notifModel = mongoose.model(
"notification",
new mongoose.Schema(
{
createdAt: Date,
},
{ strict: false }
)
);
(async () => {
console.log(
new Date().toISOString() + " Started notification cleanup script..."
);
await mongoose.connect(process.env.DB_URI);
const fifthDay = new Date(Date.now() - 3600 * 24 * 15 * 1000);
const result = await notifModel.deleteMany({ createdAt: { $lt: fifthDay } });
console.log(`Deleted ${result.deletedCount} notifications`);
await mongoose.connection.close();
})().catch((err) => console.log(err));