This commit is contained in:
Roba Boru
2026-06-17 16:50:41 +03:00
26 changed files with 870 additions and 334 deletions

View File

@@ -11,7 +11,10 @@ export class EmailClientService implements OnApplicationBootstrap {
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'))
@@ -19,10 +22,8 @@ export class EmailClientService implements OnApplicationBootstrap {
}
async sendEmail(dto: SendEmail) {
this.emailServiceClient.emit('send-email', {
...dto,
appKey: 'EDR-PASSENGER-API',
});
if (!this.enabled) return {};
this.emailServiceClient.emit('send-email', { ...dto, appKey: 'EDR-PASSENGER-API' });
return {};
}
}

View File

@@ -1,4 +1,4 @@
import { Module } from '@nestjs/common';
import { DynamicModule, Module } from '@nestjs/common';
import { HttpModule } from '@nestjs/axios';
import { ClientsModule, Transport } from '@nestjs/microservices';
import { NotificationsController } from './notifications.controller';
@@ -38,17 +38,37 @@ import { SmsClientService } from './sms-client.service';
queueOptions: { durable: true },
},
},
]),
],
controllers: [NotificationsController],
providers: [
NotificationsService,
EmailAdapter,
SmsAdapter,
PushAdapter,
EmailClientService,
SmsClientService,
],
exports: [NotificationsService, EmailClientService, SmsClientService],
})
export class NotificationsModule {}
}),
},
]);
@Module({})
export class NotificationsModule {
static register(): DynamicModule {
const rmqEnabled = process.env.RABBITMQ_ENABLED !== 'false';
return {
module: NotificationsModule,
imports: [
HttpModule.register({ timeout: 10_000 }),
...(rmqEnabled ? [rmqClientsModule] : []),
],
controllers: [NotificationsController],
providers: [
NotificationsService,
EmailAdapter,
SmsAdapter,
PushAdapter,
...(rmqEnabled
? [EmailClientService, SmsClientService]
: [
{ provide: 'EMAIL_SERVICE', useValue: null },
{ provide: 'SMS_SERVICE', useValue: null },
EmailClientService,
SmsClientService,
]),
],
exports: [NotificationsService, EmailClientService, SmsClientService],
};
}
}

View File

@@ -16,7 +16,10 @@ export class SmsClientService implements OnApplicationBootstrap {
private smsClient: ClientProxy,
) {}
private readonly enabled = process.env.RABBITMQ_ENABLED !== 'false';
async onApplicationBootstrap() {
if (!this.enabled) return;
this.smsClient
.connect()
.then(() => {
@@ -28,18 +31,14 @@ export class SmsClientService implements OnApplicationBootstrap {
}
async sendSms(dto: SendMessage) {
this.smsClient.emit('send-sms', {
...dto,
appKey: 'EDR-PASSENGER-API',
});
if (!this.enabled) return {};
this.smsClient.emit('send-sms', { ...dto, appKey: 'EDR-PASSENGER-API' });
return {};
}
async sendBulkMessages(dto: BulkMessagesDto) {
this.smsClient.emit('ozeking-bulk-sms', {
...dto,
appKey: 'EDR-PASSENGER-API',
});
if (!this.enabled) return {};
this.smsClient.emit('ozeking-bulk-sms', { ...dto, appKey: 'EDR-PASSENGER-API' });
return {};
}
}