committed

This commit is contained in:
2025-01-10 13:03:41 +05:30
parent 4ba496b8eb
commit 52f746308f
17 changed files with 1689 additions and 0 deletions
+34
View File
@@ -0,0 +1,34 @@
const Notification = require('../models/notification');
async function aggregateLowPriorityNotifications(userId) {
const oneHourFromNow = new Date(Date.now() + 60 * 60 * 1000);
const notifications = await Notification.find({
userId,
priority: 'low',
sendTime: { $lte: oneHourFromNow },
status: 'pending'
});
if (notifications.length > 1) {
const summary = `You have ${notifications.length}
${notifications.map(n => '- ' + n.message).join('\n')}`;
await Notification.updateMany(
{ _id: { $in: notifications.map(n => n._id) } },
{ $set: { status: 'aggregated' } }
);
return new Notification({
userId,
message: summary,
priority: 'low',
sendTime: new Date(),
status: 'pending'
});
}
return null;
}
module.exports = { aggregateLowPriorityNotifications };