From 008bcf7c79ecd4240a8dc4d2268d6f699032a741 Mon Sep 17 00:00:00 2001 From: Mayank Mishra Date: Mon, 17 Nov 2025 12:42:25 +0530 Subject: [PATCH] fixed the invitation status for minglar admin registration --- .../minglaradmin/handlers/registration.ts | 11 ++- .../minglaradmin/services/minglar.service.ts | 78 ++++++++++++------- 2 files changed, 57 insertions(+), 32 deletions(-) diff --git a/src/modules/minglaradmin/handlers/registration.ts b/src/modules/minglaradmin/handlers/registration.ts index c577dc8..1ade84b 100644 --- a/src/modules/minglaradmin/handlers/registration.ts +++ b/src/modules/minglaradmin/handlers/registration.ts @@ -17,7 +17,7 @@ export const handler = safeHandler(async ( ): Promise => { // Parse request body let body: { email?: string }; - + try { body = event.body ? JSON.parse(event.body) : {}; } catch (error) { @@ -35,11 +35,16 @@ export const handler = safeHandler(async ( select: { emailAddress: true, id: true, userPassword: true, roleXid: true }, }); - if(!user && ![ROLE.MINGLAR_ADMIN, ROLE.CO_ADMIN, ROLE.ACCOUNT_MANAGER].includes(user.roleXid)){ + if (!user) { throw new ApiError(403, 'You are not allowed to register directly. Please contact minglar admin.'); } - if (user && user.userPassword) { + if (![ROLE.MINGLAR_ADMIN, ROLE.CO_ADMIN, ROLE.ACCOUNT_MANAGER].includes(user.roleXid)) { + throw new ApiError(403, 'You are not allowed to register directly. Please contact minglar admin.'); + } + + + if (user.userPassword) { throw new ApiError(404, 'User is already registered. Please login.'); } diff --git a/src/modules/minglaradmin/services/minglar.service.ts b/src/modules/minglaradmin/services/minglar.service.ts index 9afd453..5903700 100644 --- a/src/modules/minglaradmin/services/minglar.service.ts +++ b/src/modules/minglaradmin/services/minglar.service.ts @@ -7,6 +7,7 @@ import { hostCompanyDetailsSchema } from '../../../common/utils/validation/host/ import { CreateMinglarDto, UpdateMinglarDto } from '../dto/minglar.dto'; import { User } from '@prisma/client'; import { ROLE } from '@/common/utils/constants/common.constant'; +import { MINGLAR_INVITATION_STATUS } from '@/common/utils/constants/minglar.constant'; @Injectable() @@ -14,35 +15,53 @@ export class MinglarService { constructor(private prisma: PrismaService) { } async createPassword(user_xid: number, password: string): Promise { - // Find user by id - const user = await this.prisma.user.findUnique({ - where: { id: user_xid }, - select: { id: true, emailAddress: true, userPassword: true }, - }); - - if (!user) { - throw new ApiError(404, 'User not found'); - } - - // Check if password already exists - if (user.userPassword) { - throw new ApiError(400, 'Password already exists. Use update password instead.'); - } - - // Hash the password - const saltRounds = parseInt(process.env.SALT_ROUNDS || '10', 10); - const hashedPassword = await bcrypt.hash(password, saltRounds); - - // Update user with hashed password - await this.prisma.user.update({ - where: { id: user.id }, - data: { userPassword: hashedPassword }, - }); - - return true; + // Find user by id + const user = await this.prisma.user.findUnique({ + where: { id: user_xid }, + select: { id: true, emailAddress: true, userPassword: true }, + }); + + const invitationDetails = await this.prisma.inviteDetails.findMany({ + where: { + userXid: user.id, + isActive: true, + isMinglarInvitation: true, + }, + }); + if (invitationDetails.length > 0) { + await this.prisma.inviteDetails.update({ + where: { id: invitationDetails[0].id }, + data: { + invitation_status: MINGLAR_INVITATION_STATUS.ACCEPTED, + accepted_on: new Date(), + is_accepted: true, + } + }) } - async createHost(data: CreateMinglarDto) { + if (!user) { + throw new ApiError(404, 'User not found'); + } + + // Check if password already exists + if (user.userPassword) { + throw new ApiError(400, 'Password already exists. Use update password instead.'); + } + + // Hash the password + const saltRounds = parseInt(process.env.SALT_ROUNDS || '10', 10); + const hashedPassword = await bcrypt.hash(password, saltRounds); + + // Update user with hashed password + await this.prisma.user.update({ + where: { id: user.id }, + data: { userPassword: hashedPassword }, + }); + + return true; + } + + async createHost(data: CreateMinglarDto) { return this.prisma.user.create({ data }); } @@ -167,7 +186,8 @@ export class MinglarService { invited_on: new Date(), is_accepted: false, invitation_status: invitationStatus, - isActive: true + isActive: true, + isMinglarInvitation: true, } }); } @@ -356,5 +376,5 @@ export class MinglarService { }) } - + }