Files
gridpilot.gg/adapters/notifications/gateways/NotificationGatewayRegistry.ts
2025-12-15 18:49:10 +01:00

67 lines
2.0 KiB
TypeScript

/**
* Infrastructure: NotificationGatewayRegistry
*
* Manages notification gateways and routes notifications to appropriate channels.
*/
import type { Notification } from '../../domain/entities/Notification';
import type { NotificationChannel } from '../../domain/types/NotificationTypes';
import type {
INotificationGateway,
INotificationGatewayRegistry,
NotificationDeliveryResult
} from '../../application/ports/INotificationGateway';
export class NotificationGatewayRegistry implements INotificationGatewayRegistry {
private gateways: Map<NotificationChannel, INotificationGateway> = new Map();
constructor(initialGateways: INotificationGateway[] = []) {
initialGateways.forEach(gateway => this.register(gateway));
}
register(gateway: INotificationGateway): void {
const channel = gateway.getChannel();
this.gateways.set(channel, gateway);
}
getGateway(channel: NotificationChannel): INotificationGateway | null {
return this.gateways.get(channel) || null;
}
getAllGateways(): INotificationGateway[] {
return Array.from(this.gateways.values());
}
async send(notification: Notification): Promise<NotificationDeliveryResult> {
const gateway = this.gateways.get(notification.channel);
if (!gateway) {
return {
success: false,
channel: notification.channel,
error: `No gateway registered for channel: ${notification.channel}`,
attemptedAt: new Date(),
};
}
if (!gateway.isConfigured()) {
return {
success: false,
channel: notification.channel,
error: `Gateway for channel ${notification.channel} is not configured`,
attemptedAt: new Date(),
};
}
try {
return await gateway.send(notification);
} catch (error) {
return {
success: false,
channel: notification.channel,
error: error instanceof Error ? error.message : 'Unknown error during delivery',
attemptedAt: new Date(),
};
}
}
}