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)) {
|
||||
|
||||
Reference in New Issue
Block a user