mirror of
https://github.com/HamzaSha1/zod-backend.git
synced 2025-08-26 06:09:41 +00:00
feat: add smtp and fix dynamic link
This commit is contained in:
@ -1,9 +1,10 @@
|
||||
import { Controller, Get, HttpCode, HttpStatus, Post, Query, UseGuards } from '@nestjs/common';
|
||||
import { Body, Controller, Get, HttpCode, HttpStatus, Post, Query, UseGuards } from '@nestjs/common';
|
||||
import { ApiBearerAuth, ApiResponse, ApiTags } from '@nestjs/swagger';
|
||||
import { IJwtPayload } from '~/auth/interfaces';
|
||||
import { AuthenticatedUser } from '~/common/decorators';
|
||||
import { AccessTokenGuard } from '~/common/guards';
|
||||
import { PageOptionsRequestDto } from '~/core/dtos';
|
||||
import { SendEmailRequestDto } from '../dtos/request';
|
||||
import { NotificationsPageResponseDto } from '../dtos/response';
|
||||
import { NotificationsService } from '../services/notifications.service';
|
||||
|
||||
@ -33,4 +34,11 @@ export class NotificationsController {
|
||||
markAsRead(@AuthenticatedUser() { sub }: IJwtPayload) {
|
||||
return this.notificationsService.markAsRead(sub);
|
||||
}
|
||||
|
||||
@Post('email')
|
||||
@UseGuards(AccessTokenGuard)
|
||||
@HttpCode(HttpStatus.NO_CONTENT)
|
||||
sendEmail(@AuthenticatedUser() { sub }: IJwtPayload, @Body() body: SendEmailRequestDto) {
|
||||
return this.notificationsService.sendEmail(body);
|
||||
}
|
||||
}
|
||||
|
1
src/common/modules/notification/dtos/request/index.ts
Normal file
1
src/common/modules/notification/dtos/request/index.ts
Normal file
@ -0,0 +1 @@
|
||||
export * from './send-email.request.dto';
|
@ -0,0 +1,20 @@
|
||||
import { ApiProperty } from '@nestjs/swagger';
|
||||
import { IsEmail, IsOptional, IsString } from 'class-validator';
|
||||
|
||||
export class SendEmailRequestDto {
|
||||
@ApiProperty({ example: 'test@test.com' })
|
||||
@IsEmail()
|
||||
to!: string;
|
||||
|
||||
@ApiProperty({ example: 'Test Subject' })
|
||||
@IsString()
|
||||
subject!: string;
|
||||
|
||||
@ApiProperty({ example: 'test' })
|
||||
@IsString()
|
||||
template!: string;
|
||||
|
||||
@ApiProperty({ example: { name: 'John Doe' } })
|
||||
@IsOptional()
|
||||
data!: Record<string, any>;
|
||||
}
|
@ -1,15 +1,15 @@
|
||||
import { MailerModule } from '@nestjs-modules/mailer';
|
||||
import { Module } from '@nestjs/common';
|
||||
import { ConfigService } from '@nestjs/config';
|
||||
import { TypeOrmModule } from '@nestjs/typeorm';
|
||||
import { TwilioModule } from 'nestjs-twilio';
|
||||
import { buildTwilioOptions } from '~/core/module-options';
|
||||
import { buildMailerOptions, buildTwilioOptions } from '~/core/module-options';
|
||||
import { UserModule } from '~/user/user.module';
|
||||
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';
|
||||
import { FirebaseService, NotificationsService, TwilioService } from './services';
|
||||
|
||||
@Module({
|
||||
imports: [
|
||||
TypeOrmModule.forFeature([Notification]),
|
||||
@ -17,6 +17,10 @@ import { TwilioService } from './services/twilio.service';
|
||||
useFactory: buildTwilioOptions,
|
||||
inject: [ConfigService],
|
||||
}),
|
||||
MailerModule.forRootAsync({
|
||||
useFactory: buildMailerOptions,
|
||||
inject: [ConfigService],
|
||||
}),
|
||||
UserModule,
|
||||
],
|
||||
providers: [NotificationsService, FirebaseService, NotificationsRepository, TwilioService],
|
||||
|
@ -0,0 +1,3 @@
|
||||
export * from './firebase.service';
|
||||
export * from './notifications.service';
|
||||
export * from './twilio.service';
|
||||
|
@ -1,3 +1,4 @@
|
||||
import { MailerService } from '@nestjs-modules/mailer';
|
||||
import { Injectable, Logger } from '@nestjs/common';
|
||||
import { EventEmitter2, OnEvent } from '@nestjs/event-emitter';
|
||||
import { PageOptionsRequestDto } from '~/core/dtos';
|
||||
@ -5,6 +6,7 @@ import { DeviceService } from '~/user/services';
|
||||
import { OTP_BODY, OTP_TITLE } from '../../otp/constants';
|
||||
import { OtpType } from '../../otp/enums';
|
||||
import { ISendOtp } from '../../otp/interfaces';
|
||||
import { SendEmailRequestDto } from '../dtos/request';
|
||||
import { Notification } from '../entities';
|
||||
import { EventType, NotificationChannel, NotificationScope } from '../enums';
|
||||
import { NotificationsRepository } from '../repositories';
|
||||
@ -20,6 +22,7 @@ export class NotificationsService {
|
||||
private readonly twilioService: TwilioService,
|
||||
private readonly eventEmitter: EventEmitter2,
|
||||
private readonly deviceService: DeviceService,
|
||||
private readonly mailerService: MailerService,
|
||||
) {}
|
||||
|
||||
async sendPushNotification(userId: string, title: string, body: string) {
|
||||
@ -41,6 +44,17 @@ export class NotificationsService {
|
||||
await this.twilioService.sendSMS(to, body);
|
||||
}
|
||||
|
||||
async sendEmail({ to, subject, data, template }: SendEmailRequestDto) {
|
||||
this.logger.log(`Sending email to ${to}`);
|
||||
await this.mailerService.sendMail({
|
||||
to,
|
||||
subject,
|
||||
template,
|
||||
context: { ...data },
|
||||
});
|
||||
this.logger.log(`Email sent to ${to}`);
|
||||
}
|
||||
|
||||
async getNotifications(userId: string, pageOptionsDto: PageOptionsRequestDto) {
|
||||
this.logger.log(`Getting notifications for user ${userId}`);
|
||||
const [[notifications, count], unreadCount] = await Promise.all([
|
||||
@ -78,8 +92,17 @@ export class NotificationsService {
|
||||
return this.eventEmitter.emit(EventType.NOTIFICATION_CREATED, notification);
|
||||
}
|
||||
|
||||
private getTemplateFromNotification(notification: Notification) {
|
||||
switch (notification.scope) {
|
||||
case NotificationScope.OTP:
|
||||
return 'otp';
|
||||
default:
|
||||
return 'otp';
|
||||
}
|
||||
}
|
||||
|
||||
@OnEvent(EventType.NOTIFICATION_CREATED)
|
||||
handleNotificationCreatedEvent(notification: Notification) {
|
||||
handleNotificationCreatedEvent(notification: Notification, data?: any) {
|
||||
this.logger.log(
|
||||
`Handling ${EventType.NOTIFICATION_CREATED} event for notification ${notification.id} and type ${notification.channel}`,
|
||||
);
|
||||
@ -88,6 +111,13 @@ export class NotificationsService {
|
||||
return this.sendSMS(notification.recipient!, notification.message);
|
||||
case NotificationChannel.PUSH:
|
||||
return this.sendPushNotification(notification.userId, notification.title, notification.message);
|
||||
case NotificationChannel.EMAIL:
|
||||
return this.sendEmail({
|
||||
to: notification.recipient!,
|
||||
subject: notification.title,
|
||||
data,
|
||||
template: this.getTemplateFromNotification(notification),
|
||||
});
|
||||
}
|
||||
}
|
||||
}
|
||||
|
21
src/common/modules/notification/templates/otp.hbs
Normal file
21
src/common/modules/notification/templates/otp.hbs
Normal file
@ -0,0 +1,21 @@
|
||||
<body>
|
||||
<div class="otp">
|
||||
<h1 class="title">Your OTP Code</h1>
|
||||
<p class="message">To verify your account, please use the following One-Time Password (OTP):</p>
|
||||
<div class="otp-code">{{otp}}</div>
|
||||
</div>
|
||||
|
||||
<style>
|
||||
.otp {
|
||||
text-align: center;
|
||||
font-family: sans-serif;
|
||||
font-size: 16px;
|
||||
line-height: 1.5;
|
||||
}
|
||||
.otp-code {
|
||||
font-size: 24px;
|
||||
font-weight: bold;
|
||||
margin-top: 20px;
|
||||
}
|
||||
</style>
|
||||
</body>
|
Reference in New Issue
Block a user