Skip to content

Commit

Permalink
Implement RabbitMQ (#16)
Browse files Browse the repository at this point in the history
* Implement RabbitMQ

* Fix test suit
  • Loading branch information
Arnan-Dee authored Oct 23, 2023
1 parent 8e0cc25 commit 9d0474a
Show file tree
Hide file tree
Showing 4 changed files with 44 additions and 17 deletions.
3 changes: 2 additions & 1 deletion src/app.module.ts
Original file line number Diff line number Diff line change
Expand Up @@ -4,10 +4,11 @@ import { AppService } from './app.service';
import { PrismaModule } from './prisma/prisma.module';
import { AuthModule } from './auth/auth.module';
import { UserModule } from './user/user.module';
import { ClientsModule, Transport } from '@nestjs/microservices';

@Module({
imports: [PrismaModule, AuthModule, UserModule],
controllers: [AppController],
providers: [AppService],
})
export class AppModule {}
export class AppModule { }
15 changes: 13 additions & 2 deletions src/auth/auth.module.ts
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
import { Module } from '@nestjs/common';
import { Inject, Module } from '@nestjs/common';
import { PrismaModule } from '../prisma/prisma.module';
import { UserRepository } from '../repository/user.repository';
import { AuthService } from './auth.service';
Expand All @@ -7,9 +7,20 @@ import { JwtModule, JwtService } from '@nestjs/jwt';
import { AccessTokenStrategy } from './strategies/accessToken.strategy';
import { ConfigService } from '@nestjs/config';
import { BlacklistRepository } from 'src/repository/blacklist.repository';
import { ClientsModule, Transport } from '@nestjs/microservices';

@Module({
imports: [PrismaModule, JwtModule.register({})],
imports: [PrismaModule, JwtModule.register({}), ClientsModule.register([{
name: 'EMAIL_SERVICE',
transport: Transport.RMQ,
options: {
urls: ['amqp://localhost:5672'],
queue: 'email_queue',
queueOptions: {
durable: false
}
}
}])],
providers: [AuthService, UserRepository, AccessTokenStrategy, ConfigService, BlacklistRepository],
controllers: [AuthController],
})
Expand Down
8 changes: 8 additions & 0 deletions src/auth/auth.service.spec.ts
Original file line number Diff line number Diff line change
Expand Up @@ -24,6 +24,10 @@ describe('AuthService', () => {
const mockBlacklistRepository = {
addOutdatedToken: jest.fn(),
}

const mockEmailService = {
emit: jest.fn(),
}
beforeEach(async () => {
const module: TestingModule = await Test.createTestingModule({
providers: [AuthService,
Expand All @@ -44,6 +48,10 @@ describe('AuthService', () => {
{
provide: BlacklistRepository,
useValue: mockBlacklistRepository,
},
{
provide: 'EMAIL_SERVICE',
useValue: mockEmailService,
}
],
}).compile();
Expand Down
35 changes: 21 additions & 14 deletions src/auth/auth.service.ts
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
import { Injectable } from '@nestjs/common';
import { Inject, Injectable } from '@nestjs/common';
import { JwtService } from '@nestjs/jwt';
import { UserRepository } from '../repository/user.repository';
import * as bcrypt from 'bcrypt';
Expand All @@ -24,7 +24,7 @@ import {
// ValidateGoogleResponse,
ValidateOAuthRequest,
} from './auth.pb';
import { RpcException } from '@nestjs/microservices';
import { ClientProxy, RpcException } from '@nestjs/microservices';
import { status } from '@grpc/grpc-js';
import { $Enums, Prisma } from '@prisma/client';
import { v4 as uuidv4 } from 'uuid';
Expand All @@ -40,7 +40,8 @@ export class AuthService implements AuthServiceController {
private blacklistRepo: BlacklistRepository,
private jwtService: JwtService,
private configService: ConfigService,
) {}
@Inject('EMAIL_SERVICE') private client: ClientProxy,
) { }

public async login(request: LoginRequest): Promise<LoginResponse> {
try {
Expand Down Expand Up @@ -433,15 +434,15 @@ export class AuthService implements AuthServiceController {
'http://localhost:3000/reset-password/' + userToken;

//set connection
const transporter = nodemailer.createTransport({
host: 'smtp.office365.com',
port: 587,
secure: false, // Set to true if using SSL
auth: {
user: process.env.EMAIL_USERNAME,
pass: process.env.EMAIL_PASSWORD,
},
});
// const transporter = nodemailer.createTransport({
// host: 'smtp.office365.com',
// port: 587,
// secure: false, // Set to true if using SSL
// auth: {
// user: process.env.EMAIL_USERNAME,
// pass: process.env.EMAIL_PASSWORD,
// },
// });

// Define email options
const mailOptions = {
Expand All @@ -453,8 +454,14 @@ export class AuthService implements AuthServiceController {
<a href="${linkToResetPassword}">Reset Password</a>
`,
};
await transporter.sendMail(mailOptions);
return { resetPasswordUrl: linkToResetPassword };


this.client.emit('forgot-password', { 'mailOptions': mailOptions });
// await transporter.sendMail(mailOptions);

return { resetPasswordUrl: linkToResetPassword }


} catch (err: any) {
console.log(err);
if (!(err instanceof RpcException)) {
Expand Down

0 comments on commit 9d0474a

Please sign in to comment.