This commit is contained in:
2025-12-09 22:22:06 +01:00
parent e34a11ae7c
commit 3adf2e5e94
62 changed files with 6079 additions and 998 deletions

View File

@@ -0,0 +1,26 @@
/**
* Notifications Application Layer
*
* Exports all use cases, queries, and ports.
*/
// Use Cases
export * from './use-cases/SendNotificationUseCase';
export * from './use-cases/MarkNotificationReadUseCase';
export * from './use-cases/GetUnreadNotificationsQuery';
export * from './use-cases/NotificationPreferencesUseCases';
// Ports
export * from './ports/INotificationGateway';
// Re-export domain types for convenience
export type { Notification, NotificationProps, NotificationStatus, NotificationData } from '../domain/entities/Notification';
export type { NotificationPreference, NotificationPreferenceProps, ChannelPreference, TypePreference } from '../domain/entities/NotificationPreference';
export type { NotificationType } from '../domain/value-objects/NotificationType';
export type { NotificationChannel } from '../domain/value-objects/NotificationChannel';
export { getNotificationTypeTitle, getNotificationTypePriority } from '../domain/value-objects/NotificationType';
export { getChannelDisplayName, isExternalChannel, DEFAULT_ENABLED_CHANNELS, ALL_CHANNELS } from '../domain/value-objects/NotificationChannel';
// Re-export repository interfaces
export type { INotificationRepository } from '../domain/repositories/INotificationRepository';
export type { INotificationPreferenceRepository } from '../domain/repositories/INotificationPreferenceRepository';

View File

@@ -0,0 +1,68 @@
/**
* Application Port: INotificationGateway
*
* Defines the contract for sending notifications through external channels.
* Implementations (adapters) handle the actual delivery mechanism.
*/
import type { Notification } from '../../domain/entities/Notification';
import type { NotificationChannel } from '../../domain/value-objects/NotificationChannel';
export interface NotificationDeliveryResult {
success: boolean;
channel: NotificationChannel;
/** External message ID (e.g., Discord message ID, email ID) */
externalId?: string;
/** Error message if delivery failed */
error?: string;
/** Timestamp of delivery attempt */
attemptedAt: Date;
}
export interface INotificationGateway {
/**
* Send a notification through this gateway's channel
*/
send(notification: Notification): Promise<NotificationDeliveryResult>;
/**
* Check if this gateway supports the given channel
*/
supportsChannel(channel: NotificationChannel): boolean;
/**
* Check if the gateway is configured and ready to send
*/
isConfigured(): boolean;
/**
* Get the channel this gateway handles
*/
getChannel(): NotificationChannel;
}
/**
* Registry for notification gateways
* Allows routing notifications to the appropriate gateway based on channel
*/
export interface INotificationGatewayRegistry {
/**
* Register a gateway for a channel
*/
register(gateway: INotificationGateway): void;
/**
* Get gateway for a specific channel
*/
getGateway(channel: NotificationChannel): INotificationGateway | null;
/**
* Get all registered gateways
*/
getAllGateways(): INotificationGateway[];
/**
* Send notification through appropriate gateway
*/
send(notification: Notification): Promise<NotificationDeliveryResult>;
}

View File

@@ -0,0 +1,89 @@
/**
* Application Query: GetUnreadNotificationsQuery
*
* Retrieves unread notifications for a recipient.
*/
import type { Notification } from '../../domain/entities/Notification';
import type { INotificationRepository } from '../../domain/repositories/INotificationRepository';
export interface UnreadNotificationsResult {
notifications: Notification[];
totalCount: number;
}
export class GetUnreadNotificationsQuery {
constructor(
private readonly notificationRepository: INotificationRepository,
) {}
async execute(recipientId: string): Promise<UnreadNotificationsResult> {
const notifications = await this.notificationRepository.findUnreadByRecipientId(recipientId);
return {
notifications,
totalCount: notifications.length,
};
}
}
/**
* Application Query: GetNotificationsQuery
*
* Retrieves all notifications for a recipient with optional filtering.
*/
export interface GetNotificationsOptions {
includeRead?: boolean;
includeDismissed?: boolean;
limit?: number;
offset?: number;
}
export class GetNotificationsQuery {
constructor(
private readonly notificationRepository: INotificationRepository,
) {}
async execute(recipientId: string, options: GetNotificationsOptions = {}): Promise<Notification[]> {
const allNotifications = await this.notificationRepository.findByRecipientId(recipientId);
let filtered = allNotifications;
// Filter by status
if (!options.includeRead && !options.includeDismissed) {
filtered = filtered.filter(n => n.isUnread());
} else if (!options.includeDismissed) {
filtered = filtered.filter(n => !n.isDismissed());
} else if (!options.includeRead) {
filtered = filtered.filter(n => n.isUnread() || n.isDismissed());
}
// Sort by creation date (newest first)
filtered.sort((a, b) => b.createdAt.getTime() - a.createdAt.getTime());
// Apply pagination
if (options.offset !== undefined) {
filtered = filtered.slice(options.offset);
}
if (options.limit !== undefined) {
filtered = filtered.slice(0, options.limit);
}
return filtered;
}
}
/**
* Application Query: GetUnreadCountQuery
*
* Gets the count of unread notifications for a recipient.
*/
export class GetUnreadCountQuery {
constructor(
private readonly notificationRepository: INotificationRepository,
) {}
async execute(recipientId: string): Promise<number> {
return this.notificationRepository.countUnreadByRecipientId(recipientId);
}
}

View File

@@ -0,0 +1,87 @@
/**
* Application Use Case: MarkNotificationReadUseCase
*
* Marks a notification as read.
*/
import type { INotificationRepository } from '../../domain/repositories/INotificationRepository';
export interface MarkNotificationReadCommand {
notificationId: string;
recipientId: string; // For validation
}
export class MarkNotificationReadUseCase {
constructor(
private readonly notificationRepository: INotificationRepository,
) {}
async execute(command: MarkNotificationReadCommand): Promise<void> {
const notification = await this.notificationRepository.findById(command.notificationId);
if (!notification) {
throw new Error('Notification not found');
}
if (notification.recipientId !== command.recipientId) {
throw new Error('Cannot mark another user\'s notification as read');
}
if (!notification.isUnread()) {
return; // Already read, nothing to do
}
const updatedNotification = notification.markAsRead();
await this.notificationRepository.update(updatedNotification);
}
}
/**
* Application Use Case: MarkAllNotificationsReadUseCase
*
* Marks all notifications as read for a recipient.
*/
export class MarkAllNotificationsReadUseCase {
constructor(
private readonly notificationRepository: INotificationRepository,
) {}
async execute(recipientId: string): Promise<void> {
await this.notificationRepository.markAllAsReadByRecipientId(recipientId);
}
}
/**
* Application Use Case: DismissNotificationUseCase
*
* Dismisses a notification.
*/
export interface DismissNotificationCommand {
notificationId: string;
recipientId: string;
}
export class DismissNotificationUseCase {
constructor(
private readonly notificationRepository: INotificationRepository,
) {}
async execute(command: DismissNotificationCommand): Promise<void> {
const notification = await this.notificationRepository.findById(command.notificationId);
if (!notification) {
throw new Error('Notification not found');
}
if (notification.recipientId !== command.recipientId) {
throw new Error('Cannot dismiss another user\'s notification');
}
if (notification.isDismissed()) {
return; // Already dismissed
}
const updatedNotification = notification.dismiss();
await this.notificationRepository.update(updatedNotification);
}
}

View File

@@ -0,0 +1,120 @@
/**
* Application Use Cases: Notification Preferences
*
* Manages user notification preferences.
*/
import { NotificationPreference } from '../../domain/entities/NotificationPreference';
import type { ChannelPreference, TypePreference } from '../../domain/entities/NotificationPreference';
import type { INotificationPreferenceRepository } from '../../domain/repositories/INotificationPreferenceRepository';
import type { NotificationType } from '../../domain/value-objects/NotificationType';
import type { NotificationChannel } from '../../domain/value-objects/NotificationChannel';
/**
* Query: GetNotificationPreferencesQuery
*/
export class GetNotificationPreferencesQuery {
constructor(
private readonly preferenceRepository: INotificationPreferenceRepository,
) {}
async execute(driverId: string): Promise<NotificationPreference> {
return this.preferenceRepository.getOrCreateDefault(driverId);
}
}
/**
* Use Case: UpdateChannelPreferenceUseCase
*/
export interface UpdateChannelPreferenceCommand {
driverId: string;
channel: NotificationChannel;
preference: ChannelPreference;
}
export class UpdateChannelPreferenceUseCase {
constructor(
private readonly preferenceRepository: INotificationPreferenceRepository,
) {}
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);
}
}
/**
* Use Case: UpdateTypePreferenceUseCase
*/
export interface UpdateTypePreferenceCommand {
driverId: string;
type: NotificationType;
preference: TypePreference;
}
export class UpdateTypePreferenceUseCase {
constructor(
private readonly preferenceRepository: INotificationPreferenceRepository,
) {}
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);
}
}
/**
* Use Case: UpdateQuietHoursUseCase
*/
export interface UpdateQuietHoursCommand {
driverId: string;
startHour: number | undefined;
endHour: number | undefined;
}
export class UpdateQuietHoursUseCase {
constructor(
private readonly preferenceRepository: INotificationPreferenceRepository,
) {}
async execute(command: UpdateQuietHoursCommand): Promise<void> {
// Validate hours if provided
if (command.startHour !== undefined && (command.startHour < 0 || command.startHour > 23)) {
throw new Error('Start hour must be between 0 and 23');
}
if (command.endHour !== undefined && (command.endHour < 0 || command.endHour > 23)) {
throw new Error('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);
}
}
/**
* Use Case: SetDigestModeUseCase
*/
export interface SetDigestModeCommand {
driverId: string;
enabled: boolean;
frequencyHours?: number;
}
export class SetDigestModeUseCase {
constructor(
private readonly preferenceRepository: INotificationPreferenceRepository,
) {}
async execute(command: SetDigestModeCommand): Promise<void> {
if (command.frequencyHours !== undefined && command.frequencyHours < 1) {
throw new Error('Digest frequency must be at least 1 hour');
}
const preferences = await this.preferenceRepository.getOrCreateDefault(command.driverId);
const updated = preferences.setDigestMode(command.enabled, command.frequencyHours);
await this.preferenceRepository.save(updated);
}
}

View File

@@ -0,0 +1,119 @@
/**
* Application Use Case: SendNotificationUseCase
*
* Sends a notification to a recipient through the appropriate channels
* based on their preferences.
*/
import { v4 as uuid } from 'uuid';
import { Notification } from '../../domain/entities/Notification';
import type { NotificationData } from '../../domain/entities/Notification';
import type { INotificationRepository } from '../../domain/repositories/INotificationRepository';
import type { INotificationPreferenceRepository } from '../../domain/repositories/INotificationPreferenceRepository';
import type { INotificationGatewayRegistry, NotificationDeliveryResult } from '../ports/INotificationGateway';
import type { NotificationType } from '../../domain/value-objects/NotificationType';
import type { NotificationChannel } from '../../domain/value-objects/NotificationChannel';
export interface SendNotificationCommand {
recipientId: string;
type: NotificationType;
title: string;
body: string;
data?: NotificationData;
actionUrl?: string;
/** Override channels (skip preference check) */
forceChannels?: NotificationChannel[];
}
export interface SendNotificationResult {
/** The created notification */
notification: Notification;
/** Delivery results for each channel */
deliveryResults: NotificationDeliveryResult[];
}
export class SendNotificationUseCase {
constructor(
private readonly notificationRepository: INotificationRepository,
private readonly preferenceRepository: INotificationPreferenceRepository,
private readonly gatewayRegistry: INotificationGatewayRegistry,
) {}
async execute(command: SendNotificationCommand): Promise<SendNotificationResult> {
// Get recipient's preferences
const preferences = await this.preferenceRepository.getOrCreateDefault(command.recipientId);
// Check if this notification type is enabled
if (!preferences.isTypeEnabled(command.type)) {
// User has disabled this type - create but don't deliver
const notification = Notification.create({
id: uuid(),
recipientId: command.recipientId,
type: command.type,
title: command.title,
body: command.body,
channel: 'in_app',
data: command.data,
actionUrl: command.actionUrl,
status: 'dismissed', // Auto-dismiss since user doesn't want these
});
await this.notificationRepository.create(notification);
return {
notification,
deliveryResults: [],
};
}
// Determine which channels to use
const channels = command.forceChannels ?? preferences.getEnabledChannelsForType(command.type);
// Check quiet hours (skip external channels during quiet hours)
const effectiveChannels = preferences.isInQuietHours()
? channels.filter(ch => ch === 'in_app')
: channels;
// Ensure at least in_app is used
if (!effectiveChannels.includes('in_app')) {
effectiveChannels.unshift('in_app');
}
const deliveryResults: NotificationDeliveryResult[] = [];
let primaryNotification: Notification | null = null;
// Send through each channel
for (const channel of effectiveChannels) {
const notification = Notification.create({
id: uuid(),
recipientId: command.recipientId,
type: command.type,
title: command.title,
body: command.body,
channel,
data: command.data,
actionUrl: command.actionUrl,
});
// Save to repository (in_app channel) or attempt delivery (external channels)
if (channel === 'in_app') {
await this.notificationRepository.create(notification);
primaryNotification = notification;
deliveryResults.push({
success: true,
channel,
attemptedAt: new Date(),
});
} else {
// Attempt external delivery
const result = await this.gatewayRegistry.send(notification);
deliveryResults.push(result);
}
}
return {
notification: primaryNotification!,
deliveryResults,
};
}
}