From 4284ed386f9b0b3620b887696642a3c7f5a1ac2f Mon Sep 17 00:00:00 2001 From: neoandmatrix Date: Mon, 23 Dec 2024 19:30:56 +0530 Subject: [PATCH] chore : added otp mail functionality and files renamed --- src/app.module.ts | 4 +- src/services/apis/mailer/mailer.controller.ts | 40 +++++----- src/services/apis/mailer/mailer.module.ts | 4 +- src/services/apis/mailer/mailer.service.ts | 15 ++-- .../apis/mailer/templates/partials/otp.hbs | 74 +++++++++++++++++-- .../dto/genrateOtp.dto.ts | 0 .../generateOtp.controller.ts | 0 .../generateOtp.module.ts | 0 .../{generateOtp => otp}/genrateOtp.helper.ts | 0 .../genrateOtp.service.ts | 0 .../bullmq/processors/otp.processor.ts | 12 ++- src/services/bullmq/queue.module.ts | 2 + 12 files changed, 115 insertions(+), 36 deletions(-) rename src/services/apis/{generateOtp => otp}/dto/genrateOtp.dto.ts (100%) rename src/services/apis/{generateOtp => otp}/generateOtp.controller.ts (100%) rename src/services/apis/{generateOtp => otp}/generateOtp.module.ts (100%) rename src/services/apis/{generateOtp => otp}/genrateOtp.helper.ts (100%) rename src/services/apis/{generateOtp => otp}/genrateOtp.service.ts (100%) diff --git a/src/app.module.ts b/src/app.module.ts index 6909ae5..c3636ca 100644 --- a/src/app.module.ts +++ b/src/app.module.ts @@ -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: [ @@ -45,6 +46,7 @@ import { GenerateOtpModule } from './services/apis/generateOtp/generateOtp.modul ProfilesModule, ReactionsModule, GenerateOtpModule, + MailerModule, ], controllers: [AppController], providers: [ diff --git a/src/services/apis/mailer/mailer.controller.ts b/src/services/apis/mailer/mailer.controller.ts index 23c0f06..3173021 100644 --- a/src/services/apis/mailer/mailer.controller.ts +++ b/src/services/apis/mailer/mailer.controller.ts @@ -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( - // 'saswat.pb03@gmail.com', - // 'Saswat', - // 'XYZ' - // ) - // await this.mailerService.sendSupportTicketResolvedEmail( - // 'saswat.pb03@gmail.com', - // 'Saswat', - // '123456' - // ) - // } -} +// // @Public() +// // @Post() +// // async sendMail() { +// // await this.mailerService.sendMPassGenerationEmail( +// // 'saswat.pb03@gmail.com', +// // 'Saswat', +// // 'XYZ' +// // ) +// // await this.mailerService.sendSupportTicketResolvedEmail( +// // 'saswat.pb03@gmail.com', +// // 'Saswat', +// // '123456' +// // ) +// // } +// } diff --git a/src/services/apis/mailer/mailer.module.ts b/src/services/apis/mailer/mailer.module.ts index 12a61e5..88db40b 100644 --- a/src/services/apis/mailer/mailer.module.ts +++ b/src/services/apis/mailer/mailer.module.ts @@ -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({ @@ -41,7 +41,7 @@ import { MailerController } from './mailer.controller'; }), }), ], - controllers: [MailerController], + controllers: [], providers: [MailerService], exports: [MailerService], }) diff --git a/src/services/apis/mailer/mailer.service.ts b/src/services/apis/mailer/mailer.service.ts index ccb7ff3..795c2ef 100644 --- a/src/services/apis/mailer/mailer.service.ts +++ b/src/services/apis/mailer/mailer.service.ts @@ -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, + //context?: Record, ) { await this.mailerService.sendMail({ to, - subject, - text: content, + subject: 'OTP for your account', template: template ? `./${template}` : undefined, - context: context || {}, + context: { + otp, + supportEmail: 'techsociety@iiit-bh.ac.in', + }, }); } } diff --git a/src/services/apis/mailer/templates/partials/otp.hbs b/src/services/apis/mailer/templates/partials/otp.hbs index 2c73732..a4c8109 100644 --- a/src/services/apis/mailer/templates/partials/otp.hbs +++ b/src/services/apis/mailer/templates/partials/otp.hbs @@ -1,6 +1,68 @@ -

Use - the OTP below to complete the process:

-
{{otp}}
-

This OTP is valid for 15 minutes. If you didn’t request this, please ignore - this email or contact support at - {{supportEmail}}

\ No newline at end of file + + + + + + + + +
+
+

Programming Society

+
+
+

Dear Member,

+

Use the OTP below to complete the process:

+
{{otp}}
+

This OTP is valid for 10 minutes. If you didn’t request this, please ignore this email or contact support at {{supportEmail}}.

+
+ +
+ + \ No newline at end of file diff --git a/src/services/apis/generateOtp/dto/genrateOtp.dto.ts b/src/services/apis/otp/dto/genrateOtp.dto.ts similarity index 100% rename from src/services/apis/generateOtp/dto/genrateOtp.dto.ts rename to src/services/apis/otp/dto/genrateOtp.dto.ts diff --git a/src/services/apis/generateOtp/generateOtp.controller.ts b/src/services/apis/otp/generateOtp.controller.ts similarity index 100% rename from src/services/apis/generateOtp/generateOtp.controller.ts rename to src/services/apis/otp/generateOtp.controller.ts diff --git a/src/services/apis/generateOtp/generateOtp.module.ts b/src/services/apis/otp/generateOtp.module.ts similarity index 100% rename from src/services/apis/generateOtp/generateOtp.module.ts rename to src/services/apis/otp/generateOtp.module.ts diff --git a/src/services/apis/generateOtp/genrateOtp.helper.ts b/src/services/apis/otp/genrateOtp.helper.ts similarity index 100% rename from src/services/apis/generateOtp/genrateOtp.helper.ts rename to src/services/apis/otp/genrateOtp.helper.ts diff --git a/src/services/apis/generateOtp/genrateOtp.service.ts b/src/services/apis/otp/genrateOtp.service.ts similarity index 100% rename from src/services/apis/generateOtp/genrateOtp.service.ts rename to src/services/apis/otp/genrateOtp.service.ts diff --git a/src/services/bullmq/processors/otp.processor.ts b/src/services/bullmq/processors/otp.processor.ts index e7dd771..47f7cb2 100644 --- a/src/services/bullmq/processors/otp.processor.ts +++ b/src/services/bullmq/processors/otp.processor.ts @@ -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(); @@ -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); } diff --git a/src/services/bullmq/queue.module.ts b/src/services/bullmq/queue.module.ts index e8bb428..7ee2b9d 100644 --- a/src/services/bullmq/queue.module.ts +++ b/src/services/bullmq/queue.module.ts @@ -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({ @@ -15,6 +16,7 @@ import { OtpQueueProcessor } from './processors/otp.processor'; { name: OTP_QUEUE }, ), PresenceModule, + MailerModule, ], providers: [ ReactionStreamProcessor,