52 lines
1.7 KiB
TypeScript
52 lines
1.7 KiB
TypeScript
import type { NotificationDeliveryResult, NotificationGateway, NotificationGatewayRegistry } from '@core/notifications/application/ports/NotificationGateway';
|
|
import type { Notification } from '@core/notifications/domain/entities/Notification';
|
|
import type { NotificationChannel } from '@core/notifications/domain/types/NotificationTypes';
|
|
|
|
export class InMemoryNotificationGatewayRegistry implements NotificationGatewayRegistry {
|
|
private gateways: Map<NotificationChannel, NotificationGateway> = new Map();
|
|
|
|
register(gateway: NotificationGateway): void {
|
|
this.gateways.set(gateway.getChannel(), gateway);
|
|
}
|
|
|
|
getGateway(channel: NotificationChannel): NotificationGateway | null {
|
|
return this.gateways.get(channel) || null;
|
|
}
|
|
|
|
getAllGateways(): NotificationGateway[] {
|
|
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 ${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 : String(error),
|
|
attemptedAt: new Date(),
|
|
};
|
|
}
|
|
}
|
|
} |