Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Implement RabbitMQ #16

Merged
merged 3 commits into from
Oct 23, 2023
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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 { PrismaModule } from './prisma/prisma.module';
import { AuthModule } from './auth/auth.module';
import { UserModule } from './user/user.module';
import { ClientsModule, Transport } from '@nestjs/microservices';

Check warning on line 7 in src/app.module.ts

View workflow job for this annotation

GitHub Actions / build (18.x)

'ClientsModule' is defined but never used

Check warning on line 7 in src/app.module.ts

View workflow job for this annotation

GitHub Actions / build (18.x)

'Transport' is defined but never used

@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,15 +1,26 @@
import { Module } from '@nestjs/common';
import { Inject, Module } from '@nestjs/common';

Check warning on line 1 in src/auth/auth.module.ts

View workflow job for this annotation

GitHub Actions / build (18.x)

'Inject' is defined but never used
import { PrismaModule } from '../prisma/prisma.module';
import { UserRepository } from '../repository/user.repository';
import { AuthService } from './auth.service';
import { AuthController } from './auth.controller';
import { JwtModule, JwtService } from '@nestjs/jwt';

Check warning on line 6 in src/auth/auth.module.ts

View workflow job for this annotation

GitHub Actions / build (18.x)

'JwtService' is defined but never used
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 @@ -9,7 +9,7 @@
const mockJwtService = {
sign: jest.fn(),
};
const mockBlacklistService = {

Check warning on line 12 in src/auth/auth.service.spec.ts

View workflow job for this annotation

GitHub Actions / build (18.x)

'mockBlacklistService' is assigned a value but never used
insertToken: jest.fn(),
check: jest.fn(),
};
Expand All @@ -24,6 +24,10 @@
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 @@
{
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,14 +24,14 @@
// 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';
import { Role } from '@prisma/client';
import { BlacklistRepository } from '../repository/blacklist.repository';
import { JwtPayload } from './strategies/accessToken.strategy';
import * as nodemailer from 'nodemailer';

Check warning on line 34 in src/auth/auth.service.ts

View workflow job for this annotation

GitHub Actions / build (18.x)

'nodemailer' is defined but never used

@Injectable()
export class AuthService implements AuthServiceController {
Expand All @@ -40,7 +40,8 @@
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 @@
'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 @@
<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
Loading