wip
This commit is contained in:
@@ -5,6 +5,7 @@
|
||||
*/
|
||||
|
||||
import type { AsyncUseCase } from '@gridpilot/shared/application';
|
||||
import type { ILogger } from '../../../shared/src/logging/ILogger';
|
||||
import type { Notification } from '../../domain/entities/Notification';
|
||||
import type { INotificationRepository } from '../../domain/repositories/INotificationRepository';
|
||||
|
||||
@@ -16,15 +17,27 @@ export interface UnreadNotificationsResult {
|
||||
export class GetUnreadNotificationsUseCase implements AsyncUseCase<string, UnreadNotificationsResult> {
|
||||
constructor(
|
||||
private readonly notificationRepository: INotificationRepository,
|
||||
private readonly logger: ILogger,
|
||||
) {}
|
||||
|
||||
async execute(recipientId: string): Promise<UnreadNotificationsResult> {
|
||||
const notifications = await this.notificationRepository.findUnreadByRecipientId(recipientId);
|
||||
|
||||
return {
|
||||
notifications,
|
||||
totalCount: notifications.length,
|
||||
};
|
||||
this.logger.debug(`Attempting to retrieve unread notifications for recipient ID: ${recipientId}`);
|
||||
try {
|
||||
const notifications = await this.notificationRepository.findUnreadByRecipientId(recipientId);
|
||||
this.logger.info(`Successfully retrieved ${notifications.length} unread notifications for recipient ID: ${recipientId}`);
|
||||
|
||||
if (notifications.length === 0) {
|
||||
this.logger.warn(`No unread notifications found for recipient ID: ${recipientId}`);
|
||||
}
|
||||
|
||||
return {
|
||||
notifications,
|
||||
totalCount: notifications.length,
|
||||
};
|
||||
} catch (error) {
|
||||
this.logger.error(`Failed to retrieve unread notifications for recipient ID: ${recipientId}`, error);
|
||||
throw error;
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
@@ -7,6 +7,7 @@
|
||||
import type { AsyncUseCase } from '@gridpilot/shared/application';
|
||||
import type { INotificationRepository } from '../../domain/repositories/INotificationRepository';
|
||||
import { NotificationDomainError } from '../../domain/errors/NotificationDomainError';
|
||||
import type { ILogger } from '../../../shared/src/logging/ILogger';
|
||||
|
||||
export interface MarkNotificationReadCommand {
|
||||
notificationId: string;
|
||||
@@ -16,25 +17,36 @@ export interface MarkNotificationReadCommand {
|
||||
export class MarkNotificationReadUseCase implements AsyncUseCase<MarkNotificationReadCommand, void> {
|
||||
constructor(
|
||||
private readonly notificationRepository: INotificationRepository,
|
||||
private readonly logger: ILogger,
|
||||
) {}
|
||||
|
||||
async execute(command: MarkNotificationReadCommand): Promise<void> {
|
||||
const notification = await this.notificationRepository.findById(command.notificationId);
|
||||
|
||||
if (!notification) {
|
||||
throw new NotificationDomainError('Notification not found');
|
||||
}
|
||||
this.logger.debug(`Attempting to mark notification ${command.notificationId} as read for recipient ${command.recipientId}`);
|
||||
try {
|
||||
const notification = await this.notificationRepository.findById(command.notificationId);
|
||||
|
||||
if (!notification) {
|
||||
this.logger.warn(`Notification not found for ID: ${command.notificationId}`);
|
||||
throw new NotificationDomainError('Notification not found');
|
||||
}
|
||||
|
||||
if (notification.recipientId !== command.recipientId) {
|
||||
throw new NotificationDomainError('Cannot mark another user\'s notification as read');
|
||||
}
|
||||
if (notification.recipientId !== command.recipientId) {
|
||||
this.logger.warn(`Unauthorized attempt to mark notification ${command.notificationId}. Recipient ID mismatch.`);
|
||||
throw new NotificationDomainError('Cannot mark another user\'s notification as read');
|
||||
}
|
||||
|
||||
if (!notification.isUnread()) {
|
||||
return; // Already read, nothing to do
|
||||
}
|
||||
if (!notification.isUnread()) {
|
||||
this.logger.info(`Notification ${command.notificationId} is already read. Skipping update.`);
|
||||
return; // Already read, nothing to do
|
||||
}
|
||||
|
||||
const updatedNotification = notification.markAsRead();
|
||||
await this.notificationRepository.update(updatedNotification);
|
||||
const updatedNotification = notification.markAsRead();
|
||||
await this.notificationRepository.update(updatedNotification);
|
||||
this.logger.info(`Notification ${command.notificationId} successfully marked as read.`);
|
||||
} catch (error) {
|
||||
this.logger.error(`Failed to mark notification ${command.notificationId} as read: ${error.message}`);
|
||||
throw error;
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
@@ -5,6 +5,7 @@
|
||||
*/
|
||||
|
||||
import type { AsyncUseCase } from '@gridpilot/shared/application';
|
||||
import type { ILogger } from '@gridpilot/shared/logging/ILogger';
|
||||
import { NotificationPreference } from '../../domain/entities/NotificationPreference';
|
||||
import type { ChannelPreference, TypePreference } from '../../domain/entities/NotificationPreference';
|
||||
import type { INotificationPreferenceRepository } from '../../domain/repositories/INotificationPreferenceRepository';
|
||||
@@ -17,10 +18,19 @@ import { NotificationDomainError } from '../../domain/errors/NotificationDomainE
|
||||
export class GetNotificationPreferencesQuery implements AsyncUseCase<string, NotificationPreference> {
|
||||
constructor(
|
||||
private readonly preferenceRepository: INotificationPreferenceRepository,
|
||||
private readonly logger: ILogger,
|
||||
) {}
|
||||
|
||||
async execute(driverId: string): Promise<NotificationPreference> {
|
||||
return this.preferenceRepository.getOrCreateDefault(driverId);
|
||||
this.logger.debug(`Fetching notification preferences for driver: ${driverId}`);
|
||||
try {
|
||||
const preferences = await this.preferenceRepository.getOrCreateDefault(driverId);
|
||||
this.logger.info(`Successfully fetched preferences for driver: ${driverId}`);
|
||||
return preferences;
|
||||
} catch (error) {
|
||||
this.logger.error(`Failed to fetch preferences for driver: ${driverId}`, error);
|
||||
throw error;
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
@@ -36,12 +46,20 @@ export interface UpdateChannelPreferenceCommand {
|
||||
export class UpdateChannelPreferenceUseCase implements AsyncUseCase<UpdateChannelPreferenceCommand, void> {
|
||||
constructor(
|
||||
private readonly preferenceRepository: INotificationPreferenceRepository,
|
||||
private readonly logger: ILogger,
|
||||
) {}
|
||||
|
||||
async execute(command: UpdateChannelPreferenceCommand): Promise<void> {
|
||||
const preferences = await this.preferenceRepository.getOrCreateDefault(command.driverId);
|
||||
const updated = preferences.updateChannel(command.channel, command.preference);
|
||||
await this.preferenceRepository.save(updated);
|
||||
this.logger.debug(`Updating channel preference for driver: ${command.driverId}, channel: ${command.channel}, preference: ${command.preference}`);
|
||||
try {
|
||||
const preferences = await this.preferenceRepository.getOrCreateDefault(command.driverId);
|
||||
const updated = preferences.updateChannel(command.channel, command.preference);
|
||||
await this.preferenceRepository.save(updated);
|
||||
this.logger.info(`Successfully updated channel preference for driver: ${command.driverId}`);
|
||||
} catch (error) {
|
||||
this.logger.error(`Failed to update channel preference for driver: ${command.driverId}, channel: ${command.channel}`, error);
|
||||
throw error;
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
@@ -57,12 +75,20 @@ export interface UpdateTypePreferenceCommand {
|
||||
export class UpdateTypePreferenceUseCase implements AsyncUseCase<UpdateTypePreferenceCommand, void> {
|
||||
constructor(
|
||||
private readonly preferenceRepository: INotificationPreferenceRepository,
|
||||
private readonly logger: ILogger,
|
||||
) {}
|
||||
|
||||
async execute(command: UpdateTypePreferenceCommand): Promise<void> {
|
||||
const preferences = await this.preferenceRepository.getOrCreateDefault(command.driverId);
|
||||
const updated = preferences.updateTypePreference(command.type, command.preference);
|
||||
await this.preferenceRepository.save(updated);
|
||||
this.logger.debug(`Updating type preference for driver: ${command.driverId}, type: ${command.type}, preference: ${command.preference}`);
|
||||
try {
|
||||
const preferences = await this.preferenceRepository.getOrCreateDefault(command.driverId);
|
||||
const updated = preferences.updateTypePreference(command.type, command.preference);
|
||||
await this.preferenceRepository.save(updated);
|
||||
this.logger.info(`Successfully updated type preference for driver: ${command.driverId}`);
|
||||
} catch (error) {
|
||||
this.logger.error(`Failed to update type preference for driver: ${command.driverId}, type: ${command.type}`, error);
|
||||
throw error;
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
@@ -78,20 +104,30 @@ export interface UpdateQuietHoursCommand {
|
||||
export class UpdateQuietHoursUseCase implements AsyncUseCase<UpdateQuietHoursCommand, void> {
|
||||
constructor(
|
||||
private readonly preferenceRepository: INotificationPreferenceRepository,
|
||||
private readonly logger: ILogger,
|
||||
) {}
|
||||
|
||||
async execute(command: UpdateQuietHoursCommand): Promise<void> {
|
||||
// Validate hours if provided
|
||||
if (command.startHour !== undefined && (command.startHour < 0 || command.startHour > 23)) {
|
||||
throw new NotificationDomainError('Start hour must be between 0 and 23');
|
||||
}
|
||||
if (command.endHour !== undefined && (command.endHour < 0 || command.endHour > 23)) {
|
||||
throw new NotificationDomainError('End hour must be between 0 and 23');
|
||||
}
|
||||
this.logger.debug(`Updating quiet hours for driver: ${command.driverId}, startHour: ${command.startHour}, endHour: ${command.endHour}`);
|
||||
try {
|
||||
// Validate hours if provided
|
||||
if (command.startHour !== undefined && (command.startHour < 0 || command.startHour > 23)) {
|
||||
this.logger.warn(`Invalid start hour provided for driver: ${command.driverId}. startHour: ${command.startHour}`);
|
||||
throw new NotificationDomainError('Start hour must be between 0 and 23');
|
||||
}
|
||||
if (command.endHour !== undefined && (command.endHour < 0 || command.endHour > 23)) {
|
||||
this.logger.warn(`Invalid end hour provided for driver: ${command.driverId}. endHour: ${command.endHour}`);
|
||||
throw new NotificationDomainError('End hour must be between 0 and 23');
|
||||
}
|
||||
|
||||
const preferences = await this.preferenceRepository.getOrCreateDefault(command.driverId);
|
||||
const updated = preferences.updateQuietHours(command.startHour, command.endHour);
|
||||
await this.preferenceRepository.save(updated);
|
||||
const preferences = await this.preferenceRepository.getOrCreateDefault(command.driverId);
|
||||
const updated = preferences.updateQuietHours(command.startHour, command.endHour);
|
||||
await this.preferenceRepository.save(updated);
|
||||
this.logger.info(`Successfully updated quiet hours for driver: ${command.driverId}`);
|
||||
} catch (error) {
|
||||
this.logger.error(`Failed to update quiet hours for driver: ${command.driverId}`, error);
|
||||
throw error;
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
@@ -7,6 +7,7 @@
|
||||
|
||||
import { v4 as uuid } from 'uuid';
|
||||
import type { AsyncUseCase } from '@gridpilot/shared/application';
|
||||
import type { ILogger } from '../../../shared/src/logging/ILogger';
|
||||
import { Notification } from '../../domain/entities/Notification';
|
||||
import type { NotificationData } from '../../domain/entities/Notification';
|
||||
import type { INotificationRepository } from '../../domain/repositories/INotificationRepository';
|
||||
@@ -48,11 +49,17 @@ export class SendNotificationUseCase implements AsyncUseCase<SendNotificationCom
|
||||
private readonly notificationRepository: INotificationRepository,
|
||||
private readonly preferenceRepository: INotificationPreferenceRepository,
|
||||
private readonly gatewayRegistry: INotificationGatewayRegistry,
|
||||
) {}
|
||||
private readonly logger: ILogger,
|
||||
) {
|
||||
this.logger.debug('SendNotificationUseCase initialized.');
|
||||
}
|
||||
|
||||
async execute(command: SendNotificationCommand): Promise<SendNotificationResult> {
|
||||
// Get recipient's preferences
|
||||
const preferences = await this.preferenceRepository.getOrCreateDefault(command.recipientId);
|
||||
this.logger.debug('Executing SendNotificationUseCase', { command });
|
||||
try {
|
||||
// Get recipient's preferences
|
||||
this.logger.debug('Checking notification preferences.', { type: command.type, recipientId: command.recipientId });
|
||||
const preferences = await this.preferenceRepository.getOrCreateDefault(command.recipientId);
|
||||
|
||||
// Check if this notification type is enabled
|
||||
if (!preferences.isTypeEnabled(command.type)) {
|
||||
|
||||
@@ -6,36 +6,79 @@
|
||||
|
||||
import { NotificationPreference } from '../../domain/entities/NotificationPreference';
|
||||
import type { INotificationPreferenceRepository } from '../../domain/repositories/INotificationPreferenceRepository';
|
||||
import type { ILogger } from '@gridpilot/shared/logging/ILogger';
|
||||
|
||||
export class InMemoryNotificationPreferenceRepository implements INotificationPreferenceRepository {
|
||||
private preferences: Map<string, NotificationPreference> = new Map();
|
||||
private readonly logger: ILogger;
|
||||
|
||||
constructor(initialPreferences: NotificationPreference[] = []) {
|
||||
constructor(logger: ILogger, initialPreferences: NotificationPreference[] = []) {
|
||||
this.logger = logger;
|
||||
this.logger.info('InMemoryNotificationPreferenceRepository initialized.');
|
||||
initialPreferences.forEach(pref => {
|
||||
this.preferences.set(pref.driverId, pref);
|
||||
this.logger.debug(`Seeded preference for driver: ${pref.driverId}`);
|
||||
});
|
||||
}
|
||||
|
||||
async findByDriverId(driverId: string): Promise<NotificationPreference | null> {
|
||||
return this.preferences.get(driverId) || null;
|
||||
this.logger.debug(`Finding notification preference for driver: ${driverId}`);
|
||||
try {
|
||||
const preference = this.preferences.get(driverId) || null;
|
||||
if (preference) {
|
||||
this.logger.info(`Found preference for driver: ${driverId}`);
|
||||
} else {
|
||||
this.logger.warn(`Preference not found for driver: ${driverId}`);
|
||||
}
|
||||
return preference;
|
||||
} catch (error) {
|
||||
this.logger.error(`Error finding preference for driver ${driverId}:`, error);
|
||||
throw error;
|
||||
}
|
||||
}
|
||||
|
||||
async save(preference: NotificationPreference): Promise<void> {
|
||||
this.preferences.set(preference.driverId, preference);
|
||||
this.logger.debug(`Saving notification preference for driver: ${preference.driverId}`);
|
||||
try {
|
||||
this.preferences.set(preference.driverId, preference);
|
||||
this.logger.info(`Preference for driver ${preference.driverId} saved successfully.`);
|
||||
} catch (error) {
|
||||
this.logger.error(`Error saving preference for driver ${preference.driverId}:`, error);
|
||||
throw error;
|
||||
}
|
||||
}
|
||||
|
||||
async delete(driverId: string): Promise<void> {
|
||||
this.preferences.delete(driverId);
|
||||
this.logger.debug(`Deleting notification preference for driver: ${driverId}`);
|
||||
try {
|
||||
if (this.preferences.delete(driverId)) {
|
||||
this.logger.info(`Preference for driver ${driverId} deleted successfully.`);
|
||||
} else {
|
||||
this.logger.warn(`Preference for driver ${driverId} not found for deletion.`);
|
||||
}
|
||||
} catch (error) {
|
||||
this.logger.error(`Error deleting preference for driver ${driverId}:`, error);
|
||||
throw error;
|
||||
}
|
||||
}
|
||||
|
||||
async getOrCreateDefault(driverId: string): Promise<NotificationPreference> {
|
||||
const existing = this.preferences.get(driverId);
|
||||
if (existing) {
|
||||
return existing;
|
||||
}
|
||||
this.logger.debug(`Getting or creating default notification preference for driver: ${driverId}`);
|
||||
try {
|
||||
const existing = this.preferences.get(driverId);
|
||||
if (existing) {
|
||||
this.logger.debug(`Existing preference found for driver: ${driverId}.`);
|
||||
return existing;
|
||||
}
|
||||
|
||||
const defaultPreference = NotificationPreference.createDefault(driverId);
|
||||
this.preferences.set(driverId, defaultPreference);
|
||||
return defaultPreference;
|
||||
this.logger.info(`Creating default preference for driver: ${driverId}.`);
|
||||
const defaultPreference = NotificationPreference.createDefault(driverId);
|
||||
this.preferences.set(driverId, defaultPreference);
|
||||
this.logger.info(`Default preference created and saved for driver: ${driverId}.`);
|
||||
return defaultPreference;
|
||||
} catch (error) {
|
||||
this.logger.error(`Error getting or creating default preference for driver ${driverId}:`, error);
|
||||
throw error;
|
||||
}
|
||||
}
|
||||
}
|
||||
@@ -7,77 +7,169 @@
|
||||
import { Notification } from '../../domain/entities/Notification';
|
||||
import type { INotificationRepository } from '../../domain/repositories/INotificationRepository';
|
||||
import type { NotificationType } from '../../domain/types/NotificationTypes';
|
||||
import type { ILogger } from '@gridpilot/shared/logging/ILogger';
|
||||
|
||||
export class InMemoryNotificationRepository implements INotificationRepository {
|
||||
private notifications: Map<string, Notification> = new Map();
|
||||
private readonly logger: ILogger;
|
||||
|
||||
constructor(initialNotifications: Notification[] = []) {
|
||||
constructor(logger: ILogger, initialNotifications: Notification[] = []) {
|
||||
this.logger = logger;
|
||||
this.logger.info('InMemoryNotificationRepository initialized.');
|
||||
initialNotifications.forEach(notification => {
|
||||
this.notifications.set(notification.id, notification);
|
||||
this.logger.debug(`Seeded notification: ${notification.id}`);
|
||||
});
|
||||
}
|
||||
|
||||
async findById(id: string): Promise<Notification | null> {
|
||||
return this.notifications.get(id) || null;
|
||||
this.logger.debug(`Finding notification by ID: ${id}`);
|
||||
try {
|
||||
const notification = this.notifications.get(id) || null;
|
||||
if (notification) {
|
||||
this.logger.info(`Found notification with ID: ${id}`);
|
||||
} else {
|
||||
this.logger.warn(`Notification with ID ${id} not found.`);
|
||||
}
|
||||
return notification;
|
||||
} catch (error) {
|
||||
this.logger.error(`Error finding notification by ID ${id}:`, error);
|
||||
throw error;
|
||||
}
|
||||
}
|
||||
|
||||
async findByRecipientId(recipientId: string): Promise<Notification[]> {
|
||||
return Array.from(this.notifications.values())
|
||||
.filter(n => n.recipientId === recipientId)
|
||||
.sort((a, b) => b.createdAt.getTime() - a.createdAt.getTime());
|
||||
this.logger.debug(`Finding notifications for recipient ID: ${recipientId}`);
|
||||
try {
|
||||
const notifications = Array.from(this.notifications.values())
|
||||
.filter(n => n.recipientId === recipientId)
|
||||
.sort((a, b) => b.createdAt.getTime() - a.createdAt.getTime());
|
||||
this.logger.info(`Found ${notifications.length} notifications for recipient ID: ${recipientId}.`);
|
||||
return notifications;
|
||||
} catch (error) {
|
||||
this.logger.error(`Error finding notifications for recipient ID ${recipientId}:`, error);
|
||||
throw error;
|
||||
}
|
||||
}
|
||||
|
||||
async findUnreadByRecipientId(recipientId: string): Promise<Notification[]> {
|
||||
return Array.from(this.notifications.values())
|
||||
.filter(n => n.recipientId === recipientId && n.isUnread())
|
||||
.sort((a, b) => b.createdAt.getTime() - a.createdAt.getTime());
|
||||
this.logger.debug(`Finding unread notifications for recipient ID: ${recipientId}`);
|
||||
try {
|
||||
const notifications = Array.from(this.notifications.values())
|
||||
.filter(n => n.recipientId === recipientId && n.isUnread())
|
||||
.sort((a, b) => b.createdAt.getTime() - a.createdAt.getTime());
|
||||
this.logger.info(`Found ${notifications.length} unread notifications for recipient ID: ${recipientId}.`);
|
||||
return notifications;
|
||||
} catch (error) {
|
||||
this.logger.error(`Error finding unread notifications for recipient ID ${recipientId}:`, error);
|
||||
throw error;
|
||||
}
|
||||
}
|
||||
|
||||
async findByRecipientIdAndType(recipientId: string, type: NotificationType): Promise<Notification[]> {
|
||||
return Array.from(this.notifications.values())
|
||||
.filter(n => n.recipientId === recipientId && n.type === type)
|
||||
.sort((a, b) => b.createdAt.getTime() - a.createdAt.getTime());
|
||||
this.logger.debug(`Finding notifications for recipient ID: ${recipientId}, type: ${type}`);
|
||||
try {
|
||||
const notifications = Array.from(this.notifications.values())
|
||||
.filter(n => n.recipientId === recipientId && n.type === type)
|
||||
.sort((a, b) => b.createdAt.getTime() - a.createdAt.getTime());
|
||||
this.logger.info(`Found ${notifications.length} notifications for recipient ID: ${recipientId}, type: ${type}.`);
|
||||
return notifications;
|
||||
} catch (error) {
|
||||
this.logger.error(`Error finding notifications for recipient ID ${recipientId}, type ${type}:`, error);
|
||||
throw error;
|
||||
}
|
||||
}
|
||||
|
||||
async countUnreadByRecipientId(recipientId: string): Promise<number> {
|
||||
return Array.from(this.notifications.values())
|
||||
.filter(n => n.recipientId === recipientId && n.isUnread())
|
||||
.length;
|
||||
this.logger.debug(`Counting unread notifications for recipient ID: ${recipientId}`);
|
||||
try {
|
||||
const count = Array.from(this.notifications.values())
|
||||
.filter(n => n.recipientId === recipientId && n.isUnread())
|
||||
.length;
|
||||
this.logger.info(`Counted ${count} unread notifications for recipient ID: ${recipientId}.`);
|
||||
return count;
|
||||
} catch (error) {
|
||||
this.logger.error(`Error counting unread notifications for recipient ID ${recipientId}:`, error);
|
||||
throw error;
|
||||
}
|
||||
}
|
||||
|
||||
async create(notification: Notification): Promise<void> {
|
||||
if (this.notifications.has(notification.id)) {
|
||||
throw new Error(`Notification with ID ${notification.id} already exists`);
|
||||
this.logger.debug(`Creating notification: ${notification.id}`);
|
||||
try {
|
||||
if (this.notifications.has(notification.id)) {
|
||||
this.logger.warn(`Notification with ID ${notification.id} already exists. Throwing error.`);
|
||||
throw new Error(`Notification with ID ${notification.id} already exists`);
|
||||
}
|
||||
this.notifications.set(notification.id, notification);
|
||||
this.logger.info(`Notification ${notification.id} created successfully.`);
|
||||
} catch (error) {
|
||||
this.logger.error(`Error creating notification ${notification.id}:`, error);
|
||||
throw error;
|
||||
}
|
||||
this.notifications.set(notification.id, notification);
|
||||
}
|
||||
|
||||
async update(notification: Notification): Promise<void> {
|
||||
if (!this.notifications.has(notification.id)) {
|
||||
throw new Error(`Notification with ID ${notification.id} not found`);
|
||||
this.logger.debug(`Updating notification: ${notification.id}`);
|
||||
try {
|
||||
if (!this.notifications.has(notification.id)) {
|
||||
this.logger.warn(`Notification with ID ${notification.id} not found for update. Throwing error.`);
|
||||
throw new Error(`Notification with ID ${notification.id} not found`);
|
||||
}
|
||||
this.notifications.set(notification.id, notification);
|
||||
this.logger.info(`Notification ${notification.id} updated successfully.`);
|
||||
} catch (error) {
|
||||
this.logger.error(`Error updating notification ${notification.id}:`, error);
|
||||
throw error;
|
||||
}
|
||||
this.notifications.set(notification.id, notification);
|
||||
}
|
||||
|
||||
async delete(id: string): Promise<void> {
|
||||
this.notifications.delete(id);
|
||||
this.logger.debug(`Deleting notification: ${id}`);
|
||||
try {
|
||||
if (this.notifications.delete(id)) {
|
||||
this.logger.info(`Notification ${id} deleted successfully.`);
|
||||
} else {
|
||||
this.logger.warn(`Notification with ID ${id} not found for deletion.`);
|
||||
}
|
||||
} catch (error) {
|
||||
this.logger.error(`Error deleting notification ${id}:`, error);
|
||||
throw error;
|
||||
}
|
||||
}
|
||||
|
||||
async deleteAllByRecipientId(recipientId: string): Promise<void> {
|
||||
const toDelete = Array.from(this.notifications.values())
|
||||
.filter(n => n.recipientId === recipientId)
|
||||
.map(n => n.id);
|
||||
|
||||
toDelete.forEach(id => this.notifications.delete(id));
|
||||
this.logger.debug(`Deleting all notifications for recipient ID: ${recipientId}`);
|
||||
try {
|
||||
const initialCount = this.notifications.size;
|
||||
const toDelete = Array.from(this.notifications.values())
|
||||
.filter(n => n.recipientId === recipientId)
|
||||
.map(n => n.id);
|
||||
|
||||
toDelete.forEach(id => this.notifications.delete(id));
|
||||
this.logger.info(`Deleted ${toDelete.length} notifications for recipient ID: ${recipientId}.`);
|
||||
} catch (error) {
|
||||
this.logger.error(`Error deleting all notifications for recipient ID ${recipientId}:`, error);
|
||||
throw error;
|
||||
}
|
||||
}
|
||||
|
||||
async markAllAsReadByRecipientId(recipientId: string): Promise<void> {
|
||||
const toUpdate = Array.from(this.notifications.values())
|
||||
.filter(n => n.recipientId === recipientId && n.isUnread());
|
||||
|
||||
toUpdate.forEach(n => {
|
||||
const updated = n.markAsRead();
|
||||
this.notifications.set(updated.id, updated);
|
||||
});
|
||||
this.logger.debug(`Marking all notifications as read for recipient ID: ${recipientId}`);
|
||||
try {
|
||||
const toUpdate = Array.from(this.notifications.values())
|
||||
.filter(n => n.recipientId === recipientId && n.isUnread());
|
||||
|
||||
this.logger.info(`Found ${toUpdate.length} unread notifications to mark as read for recipient ID: ${recipientId}.`);
|
||||
|
||||
toUpdate.forEach(n => {
|
||||
const updated = n.markAsRead();
|
||||
this.notifications.set(updated.id, updated);
|
||||
});
|
||||
this.logger.info(`Marked ${toUpdate.length} notifications as read for recipient ID: ${recipientId}.`);
|
||||
} catch (error) {
|
||||
this.logger.error(`Error marking all notifications as read for recipient ID ${recipientId}:`, error);
|
||||
throw error;
|
||||
}
|
||||
}
|
||||
}
|
||||
Reference in New Issue
Block a user