From 67da5b57e62756b8ef5ec33340a0ced308945ebf Mon Sep 17 00:00:00 2001 From: Mayank Mishra Date: Thu, 27 Nov 2025 16:56:28 +0530 Subject: [PATCH] fixed the registration api --- serverless/functions/minglaradmin.yml | 2 +- src/common/utils/helper/sendOtp.ts | 33 +++++++------------ .../minglaradmin/handlers/registration.ts | 2 ++ 3 files changed, 14 insertions(+), 23 deletions(-) diff --git a/serverless/functions/minglaradmin.yml b/serverless/functions/minglaradmin.yml index 3be7c4c..38c5897 100644 --- a/serverless/functions/minglaradmin.yml +++ b/serverless/functions/minglaradmin.yml @@ -247,7 +247,7 @@ assignAMToHost: events: - httpApi: path: /minglaradmin/hosthub/onboarding/assign-am - method: post + method: patch editAgreementDetails: handler: src/modules/minglaradmin/handlers/hosthub/onboarding/editAgreementDetails.handler diff --git a/src/common/utils/helper/sendOtp.ts b/src/common/utils/helper/sendOtp.ts index 4c634e1..84b0f01 100644 --- a/src/common/utils/helper/sendOtp.ts +++ b/src/common/utils/helper/sendOtp.ts @@ -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 { - // 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() }, }); diff --git a/src/modules/minglaradmin/handlers/registration.ts b/src/modules/minglaradmin/handlers/registration.ts index 174df9d..4b73e1a 100644 --- a/src/modules/minglaradmin/handlers/registration.ts +++ b/src/modules/minglaradmin/handlers/registration.ts @@ -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'); }