feat: sms using twillio

This commit is contained in:
Abdalhamid Alhamad
2024-12-29 09:44:12 +03:00
parent 0750509a85
commit a7028fa64c
8 changed files with 122 additions and 7 deletions

View File

@ -1,15 +1,25 @@
import { Module } from '@nestjs/common';
import { ConfigService } from '@nestjs/config';
import { TypeOrmModule } from '@nestjs/typeorm';
import { TwilioModule } from 'nestjs-twilio';
import { AuthModule } from '~/auth/auth.module';
import { buildTwilioOptions } from '~/core/module-options';
import { NotificationsController } from './controllers';
import { Notification } from './entities';
import { NotificationsRepository } from './repositories';
import { FirebaseService } from './services/firebase.service';
import { NotificationsService } from './services/notifications.service';
import { TwilioService } from './services/twilio.service';
@Module({
imports: [TypeOrmModule.forFeature([Notification]), AuthModule],
providers: [NotificationsService, FirebaseService, NotificationsRepository],
imports: [
TypeOrmModule.forFeature([Notification]),
AuthModule,
TwilioModule.forRootAsync({
useFactory: buildTwilioOptions,
inject: [ConfigService],
}),
],
providers: [NotificationsService, FirebaseService, NotificationsRepository, TwilioService],
exports: [NotificationsService],
controllers: [NotificationsController],
})

View File

@ -4,6 +4,7 @@ import { PageOptionsRequestDto } from '~/core/dtos';
import { Notification } from '../entities';
import { NotificationsRepository } from '../repositories';
import { FirebaseService } from './firebase.service';
import { TwilioService } from './twilio.service';
@Injectable()
export class NotificationsService {
@ -11,6 +12,7 @@ export class NotificationsService {
private readonly deviceService: DeviceService,
private readonly firebaseService: FirebaseService,
private readonly notificationRepository: NotificationsRepository,
private readonly twilioService: TwilioService,
) {}
async sendPushNotification(userId: string, title: string, body: string) {
@ -25,6 +27,10 @@ export class NotificationsService {
return this.firebaseService.sendNotification(tokens, title, body);
}
async sendSMS(to: string, body: string) {
await this.twilioService.sendSMS(to, body);
}
async getNotifications(userId: string, pageOptionsDto: PageOptionsRequestDto) {
const [[notifications, count], unreadCount] = await Promise.all([
this.notificationRepository.getNotifications(userId, pageOptionsDto),

View File

@ -0,0 +1,21 @@
import { Injectable, Logger } from '@nestjs/common';
import { ConfigService } from '@nestjs/config';
import { TwilioService as TwilioApiService } from 'nestjs-twilio';
import { Environment } from '~/core/enums';
@Injectable()
export class TwilioService {
private logger = new Logger(TwilioService.name);
constructor(private readonly twilioService: TwilioApiService, private readonly configService: ConfigService) {}
private from = this.configService.getOrThrow('TWILIO_PHONE_NUMBER');
sendSMS(to: string, body: string) {
if (this.configService.get('NODE_ENV') === Environment.DEV) {
this.logger.log(`Skipping SMS sending in DEV environment. Message: ${body} to: ${to}`);
return;
}
return this.twilioService.client.messages.create({
body,
to,
from: this.from,
});
}
}