feat: Event architecture implemented

This commit is contained in:
SauravDhakal
2026-04-04 20:53:07 +05:45
parent 9d931e0d96
commit f21ee1d131
8 changed files with 44 additions and 14 deletions

View File

@@ -1,7 +1,10 @@
import { welcomeToApp } from "./auth" import { welcomeToApp } from "./auth"
const EmailTemplates = { const EmailTemplates = {
welcomeToApp, signup_completed: {
subject: "Welcome to app",
body: welcomeToApp
},
} }
export default EmailTemplates export default EmailTemplates

View File

@@ -15,6 +15,7 @@
``` ```
2. Also an API of my own 2. Also an API of my own
3. Production and testing env diff
# 🏗️ SaaS Architects Roadmap: NestJS & DevOps # 🏗️ SaaS Architects Roadmap: NestJS & DevOps

View File

@@ -22,6 +22,7 @@ import { MailModule } from './mail/mail.module';
ConfigModule.forRoot({ ConfigModule.forRoot({
isGlobal: true, isGlobal: true,
}), }),
EventEmitterModule.forRoot(),
UserModule, UserModule,
AuthModule, AuthModule,
RequestContextModule, RequestContextModule,
@@ -31,7 +32,6 @@ import { MailModule } from './mail/mail.module';
AuthorizationModule, AuthorizationModule,
CacheModule, CacheModule,
MailModule, MailModule,
EventEmitterModule.forRoot()
], ],
controllers: [AppController], controllers: [AppController],
providers: [ providers: [

View File

@@ -1,12 +1,11 @@
import { Global, Module } from '@nestjs/common'; import { Global, Module } from '@nestjs/common';
import { AuthService } from './auth.service'; import { AuthService } from './auth.service';
import { AuthController } from './auth.controller'; import { AuthController } from './auth.controller';
import { APP_GUARD, Reflector } from '@nestjs/core'; import { APP_GUARD } from '@nestjs/core';
import { AuthGuard } from './guards/auth.guard'; import { AuthGuard } from './guards/auth.guard';
import { UserModule } from 'src/user/user.module'; import { UserModule } from 'src/user/user.module';
import { JwtModule } from '@nestjs/jwt'; import { JwtModule } from '@nestjs/jwt';
import { RequestContextModule } from 'core/als/request-context.module'; import { RequestContextModule } from 'core/als/request-context.module';
import { MailModule } from 'src/mail/mail.module';
@Global() @Global()
@Module({ @Module({
@@ -22,7 +21,6 @@ import { MailModule } from 'src/mail/mail.module';
UserModule, UserModule,
JwtModule, JwtModule,
RequestContextModule, RequestContextModule,
MailModule
], ],
}) })
export class AuthModule { } export class AuthModule { }

View File

@@ -5,8 +5,7 @@ import * as bcrypt from 'bcrypt';
import { UserService } from 'src/user/user.service'; import { UserService } from 'src/user/user.service';
import { TokenInputType } from './types'; import { TokenInputType } from './types';
import { JwtService } from '@nestjs/jwt'; import { JwtService } from '@nestjs/jwt';
import EmailTemplates from 'common/emails'; import { EventEmitter2 } from '@nestjs/event-emitter';
import { MailService } from 'src/mail/mail.service';
@Injectable() @Injectable()
@Public() @Public()
@@ -14,7 +13,7 @@ export class AuthService {
constructor( constructor(
private readonly userService: UserService, private readonly userService: UserService,
private readonly jwtService: JwtService, private readonly jwtService: JwtService,
private readonly mailService: MailService, private readonly eventEmitter: EventEmitter2
) { } ) { }
async register(dto: RegisterUserRequestDTO) { async register(dto: RegisterUserRequestDTO) {
@@ -24,11 +23,8 @@ export class AuthService {
password: hashedPassword, password: hashedPassword,
}); });
this.mailService.sendMail({ this.eventEmitter.emit('user.sign_up', dto.email)
to: dto.email,
subject: "Welcome onboard",
body: EmailTemplates.welcomeToApp
})
return true; return true;
} }

12
src/mail/mail.listener.ts Normal file
View File

@@ -0,0 +1,12 @@
import { Injectable } from "@nestjs/common"
import { OnEvent } from "@nestjs/event-emitter"
import { MailService } from "./mail.service"
@Injectable()
export class MailListener {
constructor(private readonly mailService: MailService) { }
@OnEvent('user.sign_up')
handleUserSignUp(email: string) {
this.mailService.sendWelcomeMail({ to: email })
}
}

View File

@@ -1,8 +1,9 @@
import { Module } from '@nestjs/common'; import { Module } from '@nestjs/common';
import { MailService } from './mail.service'; import { MailService } from './mail.service';
import { MailListener } from './mail.listener';
@Module({ @Module({
providers: [MailService], providers: [MailService, MailListener],
exports: [MailService] exports: [MailService]
}) })
export class MailModule { } export class MailModule { }

View File

@@ -1,5 +1,6 @@
import { Injectable } from '@nestjs/common'; import { Injectable } from '@nestjs/common';
import { ConfigService } from "@nestjs/config"; import { ConfigService } from "@nestjs/config";
import EmailTemplates from 'common/emails';
import * as nodemailer from "nodemailer"; import * as nodemailer from "nodemailer";
@Injectable() @Injectable()
@@ -30,6 +31,24 @@ export class MailService {
this.mailServiceAvailable = true; this.mailServiceAvailable = true;
} }
/*
* SIGN-UP
* */
sendWelcomeMail({ to }: { to: string }) {
if (!this.mailServiceAvailable)
throw new Error("Mail service not available")
const email = EmailTemplates.signup_completed;
this.transporter.sendMail(
{
to,
subject: email.subject,
html: email.body
}
)
}
sendMail({ to, subject, body }: { to: string, subject: string, body: string }) { sendMail({ to, subject, body }: { to: string, subject: string, body: string }) {
if (!this.mailServiceAvailable) if (!this.mailServiceAvailable)
throw new Error("Mail service not available") throw new Error("Mail service not available")