mirror of
https://github.com/HamzaSha1/zod-backend.git
synced 2026-03-10 17:11:44 +00:00
- Implement messaging system factory pattern - Fix all transaction notification blockers - Complete listener logic for all notification types
59 lines
1.8 KiB
TypeScript
59 lines
1.8 KiB
TypeScript
import { Injectable, Logger, Optional } from '@nestjs/common';
|
|
import { NotificationScope, requiresGuaranteedDelivery } from '../../enums/notification-scope.enum';
|
|
import { IMessagingSystem } from '../../interfaces/messaging-system.interface';
|
|
import { RedisPubSubMessagingService } from './redis-pubsub-messaging.service';
|
|
|
|
/**
|
|
* Messaging System Factory
|
|
*
|
|
* Determines which messaging system to use based on notification requirements.
|
|
*
|
|
* - Regular notifications → Redis PubSub (fast, 2-5ms)
|
|
* - Critical notifications → RabbitMQ/Kafka (guaranteed delivery, 20-50ms)
|
|
*
|
|
* Usage:
|
|
* ```typescript
|
|
* const system = factory.getMessagingSystem(NotificationScope.CHILD_SPENDING);
|
|
* await system.publish('NOTIFICATION_CREATED', payload);
|
|
* ```
|
|
*/
|
|
@Injectable()
|
|
export class MessagingSystemFactory {
|
|
private readonly logger = new Logger(MessagingSystemFactory.name);
|
|
|
|
constructor(
|
|
private readonly redisPubSubService: RedisPubSubMessagingService,
|
|
) {}
|
|
|
|
/**
|
|
* Get the appropriate messaging system based on notification scope
|
|
*
|
|
* @param scope - Notification scope
|
|
* @returns Messaging system to use
|
|
*/
|
|
getMessagingSystem(scope: NotificationScope): IMessagingSystem {
|
|
const needsGuaranteedDelivery = requiresGuaranteedDelivery(scope);
|
|
|
|
if (needsGuaranteedDelivery) {
|
|
this.logger.warn(
|
|
`[Factory] Critical notification ${scope} requires guaranteed delivery, ` +
|
|
`but RabbitMQ not configured. Falling back to Redis PubSub.`
|
|
);
|
|
return this.redisPubSubService;
|
|
} else {
|
|
this.logger.debug(`[Factory] Using Redis PubSub for notification: ${scope}`);
|
|
return this.redisPubSubService;
|
|
}
|
|
}
|
|
|
|
/**
|
|
* Get default messaging system (Redis PubSub)
|
|
*
|
|
* @returns Default messaging system
|
|
*/
|
|
getDefaultMessagingSystem(): IMessagingSystem {
|
|
return this.redisPubSubService;
|
|
}
|
|
}
|
|
|