Merge branch 'mayank' of http://git.wdipl.com/Mayank.Mishra/MinglarBackendNestJS into paritosh
This commit is contained in:
@@ -279,7 +279,7 @@ assignAMToHost:
|
||||
events:
|
||||
- httpApi:
|
||||
path: /minglaradmin/hosthub/onboarding/assign-am
|
||||
method: post
|
||||
method: patch
|
||||
|
||||
editAgreementDetails:
|
||||
handler: src/modules/minglaradmin/handlers/hosthub/onboarding/editAgreementDetails.handler
|
||||
|
||||
@@ -1,47 +1,38 @@
|
||||
import * as bcrypt from "bcryptjs";
|
||||
import { OtpGenerator, OtpGeneratorSixDigit } from "./OtpGenerator";
|
||||
import { encryptUserId } from "./CodeGenerator";
|
||||
import { PrismaClient } from "@prisma/client";
|
||||
|
||||
const prisma = new PrismaClient();
|
||||
|
||||
export interface OtpResult {
|
||||
otp: string; // Plain OTP (for sending)
|
||||
hashedOtp: string; // Hashed OTP (for DB storage)
|
||||
expiry: Date; // Expiry timestamp
|
||||
encryptedId: string; // Encrypted user ID
|
||||
otp: string;
|
||||
hashedOtp: string;
|
||||
expiry: Date;
|
||||
encryptedId: string;
|
||||
}
|
||||
|
||||
/**
|
||||
* Generate OTP, store it in DB, and send email.
|
||||
* @param userId The user’s ID
|
||||
* @param email Recipient email
|
||||
* @param emailPurpose For which flow (e.g. "Register", "Login", "ForgotPassword")
|
||||
* @param otpLength OTP length (4 or 6)
|
||||
* @param expiryMinutes Expiry time in minutes
|
||||
* Generate OTP using Prisma instance passed from Lambda
|
||||
*/
|
||||
export async function generateOtpHelper(
|
||||
prisma: any, // ⭐ Inject prisma
|
||||
userId: number,
|
||||
email: string,
|
||||
emailPurpose: "Register" | "Login" | "ForgotPassword",
|
||||
otpLength: 4 | 6 = 4,
|
||||
expiryMinutes: number = 5
|
||||
): Promise<OtpResult> {
|
||||
// Generate OTP
|
||||
|
||||
const otp =
|
||||
otpLength === 6
|
||||
? OtpGeneratorSixDigit.generateOtp()
|
||||
: OtpGenerator.generateOtp();
|
||||
// Hash OTP
|
||||
|
||||
const hashedOtp = await bcrypt.hash(otp, 10);
|
||||
|
||||
// Expiry time
|
||||
const expiry = new Date(Date.now() + expiryMinutes * 60 * 1000);
|
||||
const expiry = new Date(Date.now() + expiryMinutes * 60000);
|
||||
|
||||
// Encrypt user ID
|
||||
const encryptedId = encryptUserId(userId.toString());
|
||||
|
||||
// 🔹 First delete old OTPs for this user & purpose
|
||||
// Delete previous active OTPs
|
||||
await prisma.userOtp.deleteMany({
|
||||
where: {
|
||||
userXid: userId,
|
||||
@@ -50,7 +41,7 @@ export async function generateOtpHelper(
|
||||
},
|
||||
});
|
||||
|
||||
// Save OTP into user_otps table
|
||||
// Create new OTP entry
|
||||
await prisma.userOtp.create({
|
||||
data: {
|
||||
userXid: userId,
|
||||
@@ -59,8 +50,6 @@ export async function generateOtpHelper(
|
||||
expiresOn: expiry,
|
||||
isVerified: false,
|
||||
isActive: true,
|
||||
// sendOn will default to now()
|
||||
// createdAt will default to now()
|
||||
},
|
||||
});
|
||||
|
||||
|
||||
@@ -55,6 +55,7 @@ export const handler = safeHandler(async (
|
||||
}
|
||||
|
||||
const otpResult = await generateOtpHelper(
|
||||
prismaService, // ⭐ pass Prisma from here
|
||||
Number(newUser?.id),
|
||||
newUser?.emailAddress,
|
||||
'Register',
|
||||
@@ -62,6 +63,7 @@ export const handler = safeHandler(async (
|
||||
5
|
||||
);
|
||||
|
||||
|
||||
if (!otpResult || !otpResult.otp) {
|
||||
throw new ApiError(500, 'Failed to send OTP');
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user