refactor adapters
This commit is contained in:
@@ -0,0 +1,71 @@
|
||||
/**
|
||||
* Infrastructure Adapter: DiscordNotificationAdapter (Stub)
|
||||
*
|
||||
* Handles Discord webhook notifications.
|
||||
* Currently a stub - to be implemented when Discord integration is needed.
|
||||
*/
|
||||
|
||||
import type { Notification } from '../../domain/entities/Notification';
|
||||
import type {
|
||||
INotificationGateway,
|
||||
NotificationDeliveryResult
|
||||
} from '../../application/ports/INotificationGateway';
|
||||
import type { NotificationChannel } from '../../domain/types/NotificationTypes';
|
||||
|
||||
export interface DiscordAdapterConfig {
|
||||
webhookUrl?: string;
|
||||
}
|
||||
|
||||
export class DiscordNotificationAdapter implements INotificationGateway {
|
||||
private readonly channel: NotificationChannel = 'discord';
|
||||
private webhookUrl: string | undefined;
|
||||
|
||||
constructor(config: DiscordAdapterConfig = {}) {
|
||||
this.webhookUrl = config.webhookUrl;
|
||||
}
|
||||
|
||||
async send(notification: Notification): Promise<NotificationDeliveryResult> {
|
||||
if (!this.isConfigured()) {
|
||||
return {
|
||||
success: false,
|
||||
channel: this.channel,
|
||||
error: 'Discord webhook URL not configured',
|
||||
attemptedAt: new Date(),
|
||||
};
|
||||
}
|
||||
|
||||
// TODO: Implement actual Discord webhook call
|
||||
// For now, this is a stub that logs and returns success
|
||||
console.log(`[Discord Stub] Would send notification to ${this.webhookUrl}:`, {
|
||||
title: notification.title,
|
||||
body: notification.body,
|
||||
type: notification.type,
|
||||
});
|
||||
|
||||
return {
|
||||
success: true,
|
||||
channel: this.channel,
|
||||
externalId: `discord-stub-${notification.id}`,
|
||||
attemptedAt: new Date(),
|
||||
};
|
||||
}
|
||||
|
||||
supportsChannel(channel: NotificationChannel): boolean {
|
||||
return channel === this.channel;
|
||||
}
|
||||
|
||||
isConfigured(): boolean {
|
||||
return !!this.webhookUrl;
|
||||
}
|
||||
|
||||
getChannel(): NotificationChannel {
|
||||
return this.channel;
|
||||
}
|
||||
|
||||
/**
|
||||
* Configure the webhook URL
|
||||
*/
|
||||
setWebhookUrl(url: string): void {
|
||||
this.webhookUrl = url;
|
||||
}
|
||||
}
|
||||
76
adapters/notifications/gateways/EmailNotificationGateway.ts
Normal file
76
adapters/notifications/gateways/EmailNotificationGateway.ts
Normal file
@@ -0,0 +1,76 @@
|
||||
/**
|
||||
* Infrastructure Adapter: EmailNotificationAdapter (Stub)
|
||||
*
|
||||
* Handles email notifications.
|
||||
* Currently a stub - to be implemented when email integration is needed.
|
||||
*/
|
||||
|
||||
import type { Notification } from '../../domain/entities/Notification';
|
||||
import type {
|
||||
INotificationGateway,
|
||||
NotificationDeliveryResult
|
||||
} from '../../application/ports/INotificationGateway';
|
||||
import type { NotificationChannel } from '../../domain/types/NotificationTypes';
|
||||
|
||||
export interface EmailAdapterConfig {
|
||||
smtpHost?: string;
|
||||
smtpPort?: number;
|
||||
smtpUser?: string;
|
||||
smtpPassword?: string;
|
||||
fromAddress?: string;
|
||||
}
|
||||
|
||||
export class EmailNotificationAdapter implements INotificationGateway {
|
||||
private readonly channel: NotificationChannel = 'email';
|
||||
private config: EmailAdapterConfig;
|
||||
|
||||
constructor(config: EmailAdapterConfig = {}) {
|
||||
this.config = config;
|
||||
}
|
||||
|
||||
async send(notification: Notification): Promise<NotificationDeliveryResult> {
|
||||
if (!this.isConfigured()) {
|
||||
return {
|
||||
success: false,
|
||||
channel: this.channel,
|
||||
error: 'Email SMTP not configured',
|
||||
attemptedAt: new Date(),
|
||||
};
|
||||
}
|
||||
|
||||
// TODO: Implement actual email sending
|
||||
// For now, this is a stub that logs and returns success
|
||||
console.log(`[Email Stub] Would send email:`, {
|
||||
to: notification.recipientId, // Would need to resolve to actual email
|
||||
subject: notification.title,
|
||||
body: notification.body,
|
||||
type: notification.type,
|
||||
});
|
||||
|
||||
return {
|
||||
success: true,
|
||||
channel: this.channel,
|
||||
externalId: `email-stub-${notification.id}`,
|
||||
attemptedAt: new Date(),
|
||||
};
|
||||
}
|
||||
|
||||
supportsChannel(channel: NotificationChannel): boolean {
|
||||
return channel === this.channel;
|
||||
}
|
||||
|
||||
isConfigured(): boolean {
|
||||
return !!(this.config.smtpHost && this.config.fromAddress);
|
||||
}
|
||||
|
||||
getChannel(): NotificationChannel {
|
||||
return this.channel;
|
||||
}
|
||||
|
||||
/**
|
||||
* Update SMTP configuration
|
||||
*/
|
||||
configure(config: EmailAdapterConfig): void {
|
||||
this.config = { ...this.config, ...config };
|
||||
}
|
||||
}
|
||||
44
adapters/notifications/gateways/InAppNotificationGateway.ts
Normal file
44
adapters/notifications/gateways/InAppNotificationGateway.ts
Normal file
@@ -0,0 +1,44 @@
|
||||
/**
|
||||
* Infrastructure Adapter: InAppNotificationAdapter
|
||||
*
|
||||
* Handles in-app notifications (stored in database, shown in UI).
|
||||
* This is the primary/default notification channel.
|
||||
*/
|
||||
|
||||
import type { Notification } from '../../domain/entities/Notification';
|
||||
import type {
|
||||
INotificationGateway,
|
||||
NotificationDeliveryResult
|
||||
} from '../../application/ports/INotificationGateway';
|
||||
import type { NotificationChannel } from '../../domain/types/NotificationTypes';
|
||||
|
||||
export class InAppNotificationAdapter implements INotificationGateway {
|
||||
private readonly channel: NotificationChannel = 'in_app';
|
||||
|
||||
/**
|
||||
* For in_app, sending is essentially a no-op since the notification
|
||||
* is already persisted by the use case. This just confirms delivery.
|
||||
*/
|
||||
async send(notification: Notification): Promise<NotificationDeliveryResult> {
|
||||
// In-app notifications are stored directly in the repository
|
||||
// This adapter just confirms the "delivery" was successful
|
||||
return {
|
||||
success: true,
|
||||
channel: this.channel,
|
||||
externalId: notification.id,
|
||||
attemptedAt: new Date(),
|
||||
};
|
||||
}
|
||||
|
||||
supportsChannel(channel: NotificationChannel): boolean {
|
||||
return channel === this.channel;
|
||||
}
|
||||
|
||||
isConfigured(): boolean {
|
||||
return true; // Always configured
|
||||
}
|
||||
|
||||
getChannel(): NotificationChannel {
|
||||
return this.channel;
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,67 @@
|
||||
/**
|
||||
* 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(),
|
||||
};
|
||||
}
|
||||
}
|
||||
}
|
||||
Reference in New Issue
Block a user