tududi/backend/modules/notifications/service.js
Chris 542be2c1e9
Fix bug 366 (#764)
* Optimize DB

* Clean up names

* fixup! Clean up names

* fixup! fixup! Clean up names
2026-01-07 18:18:07 +02:00

65 lines
2.1 KiB
JavaScript

'use strict';
const notificationsRepository = require('./repository');
const { NotFoundError } = require('../../shared/errors');
class NotificationsService {
async getAll(userId, options) {
const { limit = 10, offset = 0, includeRead = 'true', type } = options;
return notificationsRepository.getUserNotifications(userId, {
limit: parseInt(limit),
offset: parseInt(offset),
includeRead: includeRead === 'true',
type: type || null,
});
}
async getUnreadCount(userId) {
const count = await notificationsRepository.getUnreadCount(userId);
return { count };
}
async markAsRead(userId, notificationId) {
const notification = await notificationsRepository.findByIdAndUser(
notificationId,
userId
);
if (!notification) {
throw new NotFoundError('Notification not found');
}
await notification.markAsRead();
return { notification, message: 'Notification marked as read' };
}
async markAsUnread(userId, notificationId) {
const notification = await notificationsRepository.findByIdAndUser(
notificationId,
userId
);
if (!notification) {
throw new NotFoundError('Notification not found');
}
await notification.markAsUnread();
return { notification, message: 'Notification marked as unread' };
}
async markAllAsRead(userId) {
const [count] = await notificationsRepository.markAllAsRead(userId);
return { count, message: `Marked ${count} notifications as read` };
}
async dismiss(userId, notificationId) {
const notification = await notificationsRepository.findByIdAndUser(
notificationId,
userId,
{ dismissed_at: null }
);
if (!notification) {
throw new NotFoundError('Notification not found');
}
await notification.dismiss();
return { message: 'Notification dismissed successfully' };
}
}
module.exports = new NotificationsService();