Update email/sms service config

This commit is contained in:
Roba Boru
2026-06-17 19:32:40 +03:00
parent 2c33ce44bf
commit bc61578f80
5 changed files with 56 additions and 22 deletions

View File

@@ -19,6 +19,24 @@ export class SendMessage {
from?: string; from?: string;
} }
export class SingleMessageDto {
@ApiProperty({
description: 'Recipient phone number',
example: '+1234567890',
})
@IsString()
@IsNotEmpty()
to: string;
@ApiProperty({
description: 'Message content',
example: 'Test Single SMS from',
})
@IsString()
@IsNotEmpty()
sms: string;
}
export class BulkMessagesDto { export class BulkMessagesDto {
@ApiProperty({ type: [SendMessage] }) @ApiProperty({ type: [SendMessage] })
@IsArray() @IsArray()

View File

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

View File

@@ -7,7 +7,7 @@ import { TestNotificationDto } from './notifications.dto';
import { EmailClientService } from './email-client.service'; import { EmailClientService } from './email-client.service';
import { SmsClientService } from './sms-client.service'; import { SmsClientService } from './sms-client.service';
import { SendEmail } from './dtos/email.dto'; import { SendEmail } from './dtos/email.dto';
import { BulkMessagesDto, SendMessage } from './dtos/sms.dto'; import { BulkMessagesDto, SingleMessageDto } from './dtos/sms.dto';
@ApiTags('Notifications') @ApiTags('Notifications')
@Controller('notifications') @Controller('notifications')
@@ -51,8 +51,8 @@ export class NotificationsController {
@UseGuards(IamGuard) @UseGuards(IamGuard)
@IamRoles('ADMIN', 'STAFF') @IamRoles('ADMIN', 'STAFF')
@ApiOperation({ summary: 'Send a direct SMS via the SMS microservice' }) @ApiOperation({ summary: 'Send a direct SMS via the SMS microservice' })
@ApiBody({ type: SendMessage }) @ApiBody({ type: SingleMessageDto })
sendSms(@Body() dto: SendMessage) { sendSms(@Body() dto: SingleMessageDto) {
return this.smsClient.sendSms(dto); return this.smsClient.sendSms(dto);
} }

View File

@@ -21,7 +21,7 @@ export class NotificationsService {
) { ) {
this.channels = new Map<NotificationChannelType, NotificationChannel>([ this.channels = new Map<NotificationChannelType, NotificationChannel>([
['EMAIL', { send: (to, subject, body) => this.emailClient.sendEmail({ to, subject, text: body }).then(() => true) }], ['EMAIL', { send: (to, subject, body) => this.emailClient.sendEmail({ to, subject, text: body }).then(() => true) }],
['SMS', { send: (to, _subject, body) => this.smsClient.sendSms({ to, message: body }).then(() => true) }], ['SMS', { send: (to, _subject, body) => this.smsClient.sendSms({ to, sms: body }).then(() => true) }],
['PUSH', this.pushAdapter as NotificationChannel], ['PUSH', this.pushAdapter as NotificationChannel],
]); ]);
} }

View File

@@ -3,42 +3,48 @@ import {
Injectable, Injectable,
Logger, Logger,
OnApplicationBootstrap, OnApplicationBootstrap,
} from '@nestjs/common'; } from "@nestjs/common";
import { ClientProxy } from '@nestjs/microservices'; import { ClientProxy } from "@nestjs/microservices";
import { BulkMessagesDto, SendMessage } from './dtos/sms.dto'; import { BulkMessagesDto, SingleMessageDto } from "./dtos/sms.dto";
@Injectable() @Injectable()
export class SmsClientService implements OnApplicationBootstrap { export class SmsClientService implements OnApplicationBootstrap {
private readonly logger = new Logger(SmsClientService.name); private readonly logger = new Logger(SmsClientService.name);
constructor( constructor(
@Inject('SMS_SERVICE') @Inject("SMS_SERVICE")
private smsClient: ClientProxy, private smsClient: ClientProxy,
) {} ) {}
private readonly enabled = process.env.RABBITMQ_ENABLED !== 'false'; private readonly enabled = process.env.RABBITMQ_ENABLED !== "false";
async onApplicationBootstrap() { async onApplicationBootstrap() {
if (!this.enabled) return; if (!this.enabled) return;
this.smsClient this.smsClient
.connect() .connect()
.then(() => { .then(() => {
this.logger.log('connected to SMS service'); this.logger.log("connected to SMS service");
}) })
.catch((err) => { .catch((err) => {
console.error('Error happened at SMS service', err); console.error("Error happened at SMS service", err);
}); });
} }
async sendSms(dto: SendMessage) { async sendSms(dto: SingleMessageDto) {
if (!this.enabled) return {}; if (!this.enabled) return {};
this.smsClient.emit('send-sms', { ...dto, appKey: 'EDR-PASSENGER-API' }); this.smsClient.emit("send-sms", {
...dto,
appKey: "IFHCRS-LICENSE-MANAGEMENT",
});
return {}; return {};
} }
async sendBulkMessages(dto: BulkMessagesDto) { async sendBulkMessages(dto: BulkMessagesDto) {
if (!this.enabled) return {}; if (!this.enabled) return {};
this.smsClient.emit('ozeking-bulk-sms', { ...dto, appKey: 'EDR-PASSENGER-API' }); this.smsClient.emit("ozeking-bulk-sms", {
...dto,
appKey: "IFHCRS-LICENSE-MANAGEMENT",
});
return {}; return {};
} }
} }