Skip to content

Commit

Permalink
chore : added otp mail functionality and files renamed
Browse files Browse the repository at this point in the history
  • Loading branch information
neoandmatrix committed Dec 23, 2024
1 parent 3cff959 commit 4284ed3
Show file tree
Hide file tree
Showing 12 changed files with 115 additions and 36 deletions.
4 changes: 3 additions & 1 deletion src/app.module.ts
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,8 @@ import { ReactionsModule } from './services/apis/reactions/reactions.module';
import { ProfilesModule } from './services/apis/profiles/profiles.module';
import { QueueModule } from './services/bullmq/queue.module';
import { BullModule } from '@nestjs/bullmq';
import { GenerateOtpModule } from './services/apis/generateOtp/generateOtp.module';
import { GenerateOtpModule } from './services/apis/otp/generateOtp.module';
import { MailerModule } from './services/apis/mailer/mailer.module';

@Module({
imports: [
Expand Down Expand Up @@ -45,6 +46,7 @@ import { GenerateOtpModule } from './services/apis/generateOtp/generateOtp.modul
ProfilesModule,
ReactionsModule,
GenerateOtpModule,
MailerModule,
],
controllers: [AppController],
providers: [
Expand Down
40 changes: 20 additions & 20 deletions src/services/apis/mailer/mailer.controller.ts
Original file line number Diff line number Diff line change
@@ -1,22 +1,22 @@
import { Controller, Post } from '@nestjs/common';
import { MailerService } from './mailer.service';
// import { Controller, Post } from '@nestjs/common';
// import { MailerService } from './mailer.service';

@Controller('mail')
export class MailerController {
constructor(private mailerService: MailerService) {}
// @Controller('mail')
// export class MailerController {
// constructor(private mailerService: MailerService) {}

// @Public()
// @Post()
// async sendMail() {
// await this.mailerService.sendMPassGenerationEmail(
// '[email protected]',
// 'Saswat',
// 'XYZ'
// )
// await this.mailerService.sendSupportTicketResolvedEmail(
// '[email protected]',
// 'Saswat',
// '123456'
// )
// }
}
// // @Public()
// // @Post()
// // async sendMail() {
// // await this.mailerService.sendMPassGenerationEmail(
// // '[email protected]',
// // 'Saswat',
// // 'XYZ'
// // )
// // await this.mailerService.sendSupportTicketResolvedEmail(
// // '[email protected]',
// // 'Saswat',
// // '123456'
// // )
// // }
// }
4 changes: 2 additions & 2 deletions src/services/apis/mailer/mailer.module.ts
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@ import { MailerService } from './mailer.service';
import { MailerModule as NestMailerModule } from '@nestjs-modules/mailer';
import { HandlebarsAdapter } from '@nestjs-modules/mailer/dist/adapters/handlebars.adapter';
import * as path from 'path';
import { MailerController } from './mailer.controller';
// import { MailerController } from './mailer.controller';

@Global()
@Module({
Expand Down Expand Up @@ -41,7 +41,7 @@ import { MailerController } from './mailer.controller';
}),
}),
],
controllers: [MailerController],
controllers: [],
providers: [MailerService],
exports: [MailerService],
})
Expand Down
15 changes: 9 additions & 6 deletions src/services/apis/mailer/mailer.service.ts
Original file line number Diff line number Diff line change
Expand Up @@ -9,17 +9,20 @@ export class MailerService {

async sendMail(
to: string,
subject: string,
content: string,
// subject: string,
// content: string,
otp: string,
template?: string,
context?: Record<string, any>,
//context?: Record<string, any>,
) {
await this.mailerService.sendMail({
to,
subject,
text: content,
subject: 'OTP for your account',
template: template ? `./${template}` : undefined,
context: context || {},
context: {
otp,
supportEmail: '[email protected]',
},
});
}
}
74 changes: 68 additions & 6 deletions src/services/apis/mailer/templates/partials/otp.hbs
Original file line number Diff line number Diff line change
@@ -1,6 +1,68 @@
<p> Use
the OTP below to complete the process:</p>
<div class='otp'> {{otp}} </div>
<p>This OTP is valid for 15 minutes. If you didn’t request this, please ignore
this email or contact support at
{{supportEmail}}</p>
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<meta name="viewport" content="width=device-width, initial-scale=1.0">
<style>
body {
font-family: Arial, sans-serif;
background-color: #f4f4f4;
margin: 0;
padding: 0;
}
.container {
max-width: 600px;
margin: 20px auto;
background-color: #ffffff;
padding: 20px;
border-radius: 8px;
box-shadow: 0 0 10px rgba(0, 0, 0, 0.1);
}
.header {
text-align: center;
padding-bottom: 20px;
border-bottom: 1px solid #dddddd;
}
.header h1 {
margin: 0;
color: #333333;
}
.content {
padding: 20px 0;
}
.otp {
font-size: 24px;
font-weight: bold;
color: #333333;
text-align: center;
margin: 20px 0;
}
.footer {
text-align: center;
padding-top: 20px;
border-top: 1px solid #dddddd;
color: #777777;
}
.footer a {
color: #007BFF;
text-decoration: none;
}
</style>
</head>
<body>
<div class="container">
<div class="header">
<h1>Programming Society</h1>
</div>
<div class="content">
<p>Dear Member,</p>
<p>Use the OTP below to complete the process:</p>
<div class="otp">{{otp}}</div>
<p>This OTP is valid for 10 minutes. If you didn’t request this, please ignore this email or contact support at <a href="mailto:{{supportEmail}}">{{supportEmail}}</a>.</p>
</div>
<div class="footer">
<p>Thank you,<br>Programming Society Team</p>
</div>
</div>
</body>
</html>
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
12 changes: 11 additions & 1 deletion src/services/bullmq/processors/otp.processor.ts
Original file line number Diff line number Diff line change
Expand Up @@ -7,11 +7,15 @@ import { hashString } from 'src/common/hashing';
import { OtpJob } from '../jobs/otp.jobs';
import generateRandomNumber from 'src/common/generate-random-number';
import { Injectable } from '@nestjs/common';
import { MailerService } from 'src/services/apis/mailer/mailer.service';

@Injectable()
@Processor(OTP_QUEUE)
export class OtpQueueProcessor extends WorkerHost {
constructor(private readonly redisService: RedisService) {
constructor(
private readonly redisService: RedisService,
private readonly mailerService: MailerService,
) {
super();
}
otp: string = generateRandomNumber();
Expand All @@ -22,6 +26,12 @@ export class OtpQueueProcessor extends WorkerHost {
} = job as { data: OtpJob };
await this.storeOtpWithTTL(email, parseInt(this.otp));
console.log(`OTP for ${email} is ${this.otp} and stored in Redis`);
await this.mailerService.sendMail(
email,
this.otp,
'templates/partials/otp.hbs',
);
console.log(`OTP email sent to ${email}`);
} catch (error) {
console.error('Error in processing OTP job', error);
}
Expand Down
2 changes: 2 additions & 0 deletions src/services/bullmq/queue.module.ts
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@ import { ReactionStreamProcessor } from './processors/reaction-stream.processor'
import { OtpProducer } from './producers/otp.producer';
import { PresenceModule } from '../gateways/presence/presence.module';
import { OtpQueueProcessor } from './processors/otp.processor';
import { MailerModule } from '../apis/mailer/mailer.module';

@Global()
@Module({
Expand All @@ -15,6 +16,7 @@ import { OtpQueueProcessor } from './processors/otp.processor';
{ name: OTP_QUEUE },
),
PresenceModule,
MailerModule,
],
providers: [
ReactionStreamProcessor,
Expand Down

0 comments on commit 4284ed3

Please sign in to comment.