feat: Event architecture implemented
This commit is contained in:
@@ -22,6 +22,7 @@ import { MailModule } from './mail/mail.module';
|
||||
ConfigModule.forRoot({
|
||||
isGlobal: true,
|
||||
}),
|
||||
EventEmitterModule.forRoot(),
|
||||
UserModule,
|
||||
AuthModule,
|
||||
RequestContextModule,
|
||||
@@ -31,7 +32,6 @@ import { MailModule } from './mail/mail.module';
|
||||
AuthorizationModule,
|
||||
CacheModule,
|
||||
MailModule,
|
||||
EventEmitterModule.forRoot()
|
||||
],
|
||||
controllers: [AppController],
|
||||
providers: [
|
||||
|
||||
@@ -1,12 +1,11 @@
|
||||
import { Global, Module } from '@nestjs/common';
|
||||
import { AuthService } from './auth.service';
|
||||
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 { UserModule } from 'src/user/user.module';
|
||||
import { JwtModule } from '@nestjs/jwt';
|
||||
import { RequestContextModule } from 'core/als/request-context.module';
|
||||
import { MailModule } from 'src/mail/mail.module';
|
||||
|
||||
@Global()
|
||||
@Module({
|
||||
@@ -22,7 +21,6 @@ import { MailModule } from 'src/mail/mail.module';
|
||||
UserModule,
|
||||
JwtModule,
|
||||
RequestContextModule,
|
||||
MailModule
|
||||
],
|
||||
})
|
||||
export class AuthModule { }
|
||||
|
||||
@@ -5,8 +5,7 @@ import * as bcrypt from 'bcrypt';
|
||||
import { UserService } from 'src/user/user.service';
|
||||
import { TokenInputType } from './types';
|
||||
import { JwtService } from '@nestjs/jwt';
|
||||
import EmailTemplates from 'common/emails';
|
||||
import { MailService } from 'src/mail/mail.service';
|
||||
import { EventEmitter2 } from '@nestjs/event-emitter';
|
||||
|
||||
@Injectable()
|
||||
@Public()
|
||||
@@ -14,7 +13,7 @@ export class AuthService {
|
||||
constructor(
|
||||
private readonly userService: UserService,
|
||||
private readonly jwtService: JwtService,
|
||||
private readonly mailService: MailService,
|
||||
private readonly eventEmitter: EventEmitter2
|
||||
) { }
|
||||
|
||||
async register(dto: RegisterUserRequestDTO) {
|
||||
@@ -24,11 +23,8 @@ export class AuthService {
|
||||
password: hashedPassword,
|
||||
});
|
||||
|
||||
this.mailService.sendMail({
|
||||
to: dto.email,
|
||||
subject: "Welcome onboard",
|
||||
body: EmailTemplates.welcomeToApp
|
||||
})
|
||||
this.eventEmitter.emit('user.sign_up', dto.email)
|
||||
|
||||
return true;
|
||||
}
|
||||
|
||||
|
||||
12
src/mail/mail.listener.ts
Normal file
12
src/mail/mail.listener.ts
Normal 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 })
|
||||
}
|
||||
}
|
||||
@@ -1,8 +1,9 @@
|
||||
import { Module } from '@nestjs/common';
|
||||
import { MailService } from './mail.service';
|
||||
import { MailListener } from './mail.listener';
|
||||
|
||||
@Module({
|
||||
providers: [MailService],
|
||||
providers: [MailService, MailListener],
|
||||
exports: [MailService]
|
||||
})
|
||||
export class MailModule { }
|
||||
|
||||
@@ -1,5 +1,6 @@
|
||||
import { Injectable } from '@nestjs/common';
|
||||
import { ConfigService } from "@nestjs/config";
|
||||
import EmailTemplates from 'common/emails';
|
||||
import * as nodemailer from "nodemailer";
|
||||
|
||||
@Injectable()
|
||||
@@ -30,6 +31,24 @@ export class MailService {
|
||||
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 }) {
|
||||
if (!this.mailServiceAvailable)
|
||||
throw new Error("Mail service not available")
|
||||
|
||||
Reference in New Issue
Block a user