UNPKG

@mbc-cqrs-serverless/core

Version:
59 lines 3.4 kB
"use strict"; var __decorate = (this && this.__decorate) || function (decorators, target, key, desc) { var c = arguments.length, r = c < 3 ? target : desc === null ? desc = Object.getOwnPropertyDescriptor(target, key) : desc, d; if (typeof Reflect === "object" && typeof Reflect.decorate === "function") r = Reflect.decorate(decorators, target, key, desc); else for (var i = decorators.length - 1; i >= 0; i--) if (d = decorators[i]) r = (c < 3 ? d(r) : c > 3 ? d(target, key, r) : d(target, key)) || r; return c > 3 && r && Object.defineProperty(target, key, r), r; }; var __metadata = (this && this.__metadata) || function (k, v) { if (typeof Reflect === "object" && typeof Reflect.metadata === "function") return Reflect.metadata(k, v); }; var NotificationEventHandler_1; Object.defineProperty(exports, "__esModule", { value: true }); exports.NotificationEventHandler = void 0; const common_1 = require("@nestjs/common"); const config_1 = require("@nestjs/config"); const core_1 = require("@nestjs/core"); const decorators_1 = require("../../decorators"); const notification_env_validation_1 = require("../../notification-env.validation"); const services_1 = require("../../services"); const notification_event_1 = require("./notification.event"); let NotificationEventHandler = NotificationEventHandler_1 = class NotificationEventHandler { constructor(moduleRef, explorerService, config) { this.moduleRef = moduleRef; this.explorerService = explorerService; this.config = config; this.logger = new common_1.Logger(NotificationEventHandler_1.name); this.transports = new Map(); this.activeTransportNames = (0, notification_env_validation_1.parseNotificationTransports)(this.config.get('NOTIFICATION_TRANSPORTS')); } onModuleInit() { this.logger.debug('Finding notification transports from decorators'); const { notificationTransports } = this.explorerService.exploreNotificationTransports(); for (const TransportClass of notificationTransports) { const name = Reflect.getMetadata(decorators_1.NOTIFICATION_TRANSPORT_METADATA, TransportClass); if (this.activeTransportNames.includes(name)) { const instance = this.moduleRef.get(TransportClass, { strict: false }); // Give the instance its decorator name automatically if it isn't set if (!instance.name) { instance.name = name; } this.transports.set(name, instance); } } this.logger.debug(`Initialized active transports: [${[...this.transports.keys()].join(', ')}]`); } async execute(event) { const body = JSON.parse(event.body); this.logger.debug(`execute:: broadcasting via ${this.transports.size} transport(s)`); await Promise.all([...this.transports.values()].map((t) => t.sendMessage(body))); } }; exports.NotificationEventHandler = NotificationEventHandler; exports.NotificationEventHandler = NotificationEventHandler = NotificationEventHandler_1 = __decorate([ (0, decorators_1.EventHandler)(notification_event_1.NotificationEvent), __metadata("design:paramtypes", [core_1.ModuleRef, services_1.ExplorerService, config_1.ConfigService]) ], NotificationEventHandler); //# sourceMappingURL=notification.event.handler.js.map