-
Notifications
You must be signed in to change notification settings - Fork 3
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
pulled latest changes and resolved merge conflicts.
- Loading branch information
Showing
17 changed files
with
164 additions
and
97 deletions.
There are no files selected for viewing
This file was deleted.
Oops, something went wrong.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,23 @@ | ||
import { date, z } from 'zod'; | ||
|
||
// DTO for incoming OTP payload. This ensures the payload structure and validates fields. | ||
/** | ||
* The email of the user to send OTP. | ||
* @example "[email protected]" | ||
* */ | ||
|
||
export const OtpValidation = z.object({ | ||
email: z.string().email(), | ||
}); | ||
|
||
/** | ||
* The email of the user to send OTP. | ||
* @example " | ||
* */ | ||
export const VerifyOtpValidation = z.object({ | ||
email: z.string().email(), | ||
otp: z.string().trim(), | ||
}); | ||
|
||
export type OtpDto = z.infer<typeof OtpValidation>; | ||
export type VerifyOtpDto = z.infer<typeof VerifyOtpValidation>; |
This file was deleted.
Oops, something went wrong.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,12 +1,11 @@ | ||
import { Module } from '@nestjs/common'; | ||
import { GenerateOtpController } from './generateOtp.controller'; | ||
import { QueueModule } from 'src/services/bullmq/queue.module'; | ||
import { GenrateOtpService } from './genrateOtp.service'; | ||
import { GenerateOtpService } from './generateOtp.service'; | ||
|
||
@Module({ | ||
imports: [QueueModule], | ||
imports: [], | ||
controllers: [GenerateOtpController], | ||
providers: [GenrateOtpService], | ||
exports: [GenrateOtpService], // not exporting services as no need in testing | ||
providers: [GenerateOtpService], | ||
exports: [GenerateOtpService], // not exporting services as no need in testing (??) | ||
}) | ||
export class GenerateOtpModule {} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,46 @@ | ||
import { BadRequestException, Injectable } from '@nestjs/common'; | ||
import { OtpProducer } from 'src/services/bullmq/producers/otp.producer'; | ||
import { OtpDto, VerifyOtpDto } from './dto/generateOtp.dto'; | ||
// import { processEmail } from './helpers/generateOtp.helper'; | ||
import { OtpQueueProcessor } from 'src/services/bullmq/processors/otp.processor'; | ||
import { compareHashedString } from 'src/common/hashing'; | ||
import { RedisService } from 'src/services/redis/redis.service'; | ||
import { generateKey } from 'src/services/bullmq/constants/generate-keys'; | ||
|
||
@Injectable() | ||
export class GenerateOtpService { | ||
constructor( | ||
private readonly otpProducer: OtpProducer, | ||
private readonly redisService: RedisService, | ||
) {} | ||
|
||
async enqueueOtpJob(createOtpDto: OtpDto) { | ||
const email = createOtpDto['email'].trim(); | ||
const resp = await this.otpProducer.pushForAsyncMailing( | ||
`process-otp`, | ||
{ email }, | ||
{ | ||
removeOnComplete: true, | ||
}, | ||
); | ||
return resp; | ||
} | ||
|
||
async compareOtp( | ||
verifyOtpDto: VerifyOtpDto, | ||
removeEntryAfterCheck = false, | ||
): Promise<boolean | BadRequestException> { | ||
const key = generateKey(verifyOtpDto.email); | ||
const val = await this.redisService.get(key); | ||
|
||
if (!val) { | ||
throw new BadRequestException( | ||
'The OTP has expired or no request for an OTP was found. Please try requesting a new OTP.', | ||
); | ||
} | ||
|
||
const resp = await compareHashedString(verifyOtpDto.otp, val); | ||
if (removeEntryAfterCheck) await this.redisService.del(key); | ||
return resp; | ||
} | ||
} |
This file was deleted.
Oops, something went wrong.
This file was deleted.
Oops, something went wrong.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,15 @@ | ||
// import { z } from 'zod'; | ||
|
||
// const emailSchema = z.string().email(); | ||
|
||
// export function processEmail(input: { email: string }): { email: string } { | ||
// const parsedEmail = emailSchema.safeParse(input.email); | ||
|
||
// if (!parsedEmail.success) { | ||
// throw new Error('Invalid email: Must be a valid email format'); | ||
// } | ||
|
||
// return { | ||
// email: input.email.trim(), | ||
// }; | ||
// } |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,3 @@ | ||
export const generateKey = (email: string): string => | ||
`verification::email:${email}`; | ||
// more keys |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.