import { Inject, Injectable, Logger, OnApplicationBootstrap } from '@nestjs/common'; import { ClientProxy } from '@nestjs/microservices'; import { SendEmail } from './dtos/email.dto'; @Injectable() export class EmailClientService implements OnApplicationBootstrap { private readonly logger = new Logger(EmailClientService.name); constructor( @Inject('EMAIL_SERVICE') private readonly emailServiceClient: ClientProxy, ) {} private readonly enabled = process.env.RABBITMQ_ENABLED !== 'false'; async onApplicationBootstrap() { if (!this.enabled) return; this.emailServiceClient .connect() .then(() => this.logger.log('Connected to Email service')) .catch((err) => this.logger.error('Error connecting to Email service', err)); } async sendEmail(dto: SendEmail) { if (!this.enabled) return {}; this.emailServiceClient.emit('send-email', { ...dto, appKey: 'EDR-PASSENGER-API' }); return {}; } }