committed
This commit is contained in:
@@ -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 };
|
||||
|
||||
@@ -0,0 +1,16 @@
|
||||
const Notification = require('../models/notification');
|
||||
|
||||
async function checkDuplication(notification) {
|
||||
const oneHourAgo = new Date(Date.now() - 60 * 60 * 1000);
|
||||
const similarNotification = await Notification.findOne({
|
||||
userId: notification.userId,
|
||||
message: notification.message,
|
||||
createdAt: { $gte: oneHourAgo },
|
||||
status: 'sent'
|
||||
});
|
||||
|
||||
return !!similarNotification;
|
||||
}
|
||||
|
||||
module.exports = { checkDuplication };
|
||||
|
||||
@@ -0,0 +1,26 @@
|
||||
const User = require('../models/user');
|
||||
|
||||
function isInQuietHours(user, currentTime) {
|
||||
if (!user.preferences.quietHours) {
|
||||
return false;
|
||||
}
|
||||
|
||||
const { start, end } = user.preferences.quietHours;
|
||||
const [startHour, startMinute] = start.split(':').map(Number);
|
||||
const [endHour, endMinute] = end.split(':').map(Number);
|
||||
|
||||
const startTime = new Date(currentTime);
|
||||
startTime.setHours(startHour, startMinute, 0);
|
||||
|
||||
const endTime = new Date(currentTime);
|
||||
endTime.setHours(endHour, endMinute, 0);
|
||||
|
||||
if (endTime < startTime) {
|
||||
endTime.setDate(endTime.getDate() + 1);
|
||||
}
|
||||
|
||||
return currentTime >= startTime && currentTime < endTime;
|
||||
}
|
||||
|
||||
module.exports = { isInQuietHours };
|
||||
|
||||
@@ -0,0 +1,21 @@
|
||||
const User = require('../models/user');
|
||||
const Notification = require('../models/notification');
|
||||
|
||||
async function checkThrottling(userId) {
|
||||
const user = await User.findOne({ userId });
|
||||
if (!user) {
|
||||
throw new Error('User not found');
|
||||
}
|
||||
|
||||
const oneHourAgo = new Date(Date.now() - 60 * 60 * 1000);
|
||||
const recentNotifications = await Notification.countDocuments({
|
||||
userId,
|
||||
createdAt: { $gte: oneHourAgo },
|
||||
status: 'sent'
|
||||
});
|
||||
|
||||
return recentNotifications < user.preferences.notificationLimit;
|
||||
}
|
||||
|
||||
module.exports = { checkThrottling };
|
||||
|
||||
@@ -0,0 +1,11 @@
|
||||
const Notification = require('../models/notification');
|
||||
|
||||
async function getUrgentAlerts() {
|
||||
return Notification.find({
|
||||
priority: 'high',
|
||||
status: 'pending'
|
||||
}).sort({ createdAt: 1 });
|
||||
}
|
||||
|
||||
module.exports = { getUrgentAlerts };
|
||||
|
||||
Reference in New Issue
Block a user