diff --git a/serverless.yml b/serverless.yml index e3e09ec..1ac4f3d 100644 --- a/serverless.yml +++ b/serverless.yml @@ -194,10 +194,10 @@ functions: getStepperInfo: - handler: src/modules/common/handlers/getStepperHandler + handler: src/modules/host/handlers/getStepper.handler package: patterns: - - "src/common/utils/handlers/getStepperHandler.*" + - "src/modules/host/handlers/getStepper.handler.*" - "src/common/utils/handlers/safeHandler.*" - "src/common/database/**" - "src/modules/host/services/**" diff --git a/src/modules/common/handlers/getStepperHandler.ts b/src/modules/common/handlers/getStepperHandler.ts deleted file mode 100644 index a4cb3c7..0000000 --- a/src/modules/common/handlers/getStepperHandler.ts +++ /dev/null @@ -1,98 +0,0 @@ -import { APIGatewayProxyEvent, APIGatewayProxyResult, Context } from 'aws-lambda'; -import { safeHandler } from '../../../common/utils/handlers/safeHandler'; -import { PrismaService } from '../../../common/database/prisma.service'; -import ApiError from '../../../common/utils/helper/ApiError'; -import { verifyHostToken } from '../../../common/middlewares/jwt/authForHost'; - -const prismaService = new PrismaService(); - -export const handler = safeHandler(async ( - event: APIGatewayProxyEvent, - context?: Context -): Promise => { - // Extract token from headers - const token = event.headers['x-auth-token'] || event.headers['X-Auth-Token']; - if (!token) { - throw new ApiError(400, 'This is a protected route. Please provide a valid token.'); - } - - // Verify token and get user info - const userInfo = await verifyHostToken(token); - const userId = Number(userInfo.id); - - if (!userId || isNaN(userId)) { - throw new ApiError(400, 'Invalid user ID'); - } - - // Fetch user with their HostHeader stepper info - const user = await prismaService.user.findUnique({ - where: { id: userId }, - select: { - id: true, - firstName: true, - lastName: true, - emailAddress: true, - roleXid: true, - HostHeader: { - select: { - id: true, - stepper: true, - }, - }, - }, - }); - - if (!user) { - throw new ApiError(404, 'User not found'); - } - - if (!user.HostHeader || user.HostHeader.length === 0) { - throw new ApiError(404, 'No HostHeader record found for this user'); - } - - // Return stepper info along with user and host details - const hostHeader = user.HostHeader[0]; - - return { - statusCode: 200, - headers: { - 'Content-Type': 'application/json', - 'Access-Control-Allow-Origin': '*', - }, - body: JSON.stringify({ - success: true, - message: 'Stepper information retrieved successfully', - data: { - user: { - id: user.id, - firstName: user.firstName, - lastName: user.lastName, - emailAddress: user.emailAddress, - roleXid: user.roleXid, - }, - stepper: { - hostId: hostHeader.id, - currentStep: hostHeader.stepper, - stepperDescription: getStepperDescription(hostHeader.stepper), - }, - }, - }), - }; -}); - -/** - * Get a human-readable description of the stepper value - */ -function getStepperDescription(stepper: number): string { - const stepDescriptions: { [key: number]: string } = { - 1: 'Basic Company Information', - 2: 'Company Documents & Verification', - 3: 'Bank & Payment Details', - 4: 'Activities Setup', - 5: 'Pricing & Services', - 6: 'Review & Approval', - 7: 'Active & Live', - }; - - return stepDescriptions[stepper] || 'Unknown Step'; -} diff --git a/src/modules/host/handlers/getStepper.ts b/src/modules/host/handlers/getStepper.ts new file mode 100644 index 0000000..aaaca8f --- /dev/null +++ b/src/modules/host/handlers/getStepper.ts @@ -0,0 +1,50 @@ +import { APIGatewayProxyEvent, APIGatewayProxyResult, Context } from 'aws-lambda'; +import { safeHandler } from '../../../common/utils/handlers/safeHandler'; +import { PrismaService } from '../../../common/database/prisma.service'; +import ApiError from '../../../common/utils/helper/ApiError'; +import { verifyHostToken } from '../../../common/middlewares/jwt/authForHost'; +import { HostService } from '../services/host.service'; + +const prismaService = new PrismaService(); +const hostService = new HostService(prismaService); +export const handler = safeHandler(async ( + event: APIGatewayProxyEvent, + context?: Context +): Promise => { + // Extract token from headers + const token = event.headers['x-auth-token'] || event.headers['X-Auth-Token']; + if (!token) { + throw new ApiError(400, 'This is a protected route. Please provide a valid token.'); + } + + // Verify token and get user info + const userInfo = await verifyHostToken(token); + const userId = Number(userInfo.id); + + if (!userId || isNaN(userId)) { + throw new ApiError(400, 'Invalid user ID'); + } + + // Fetch user with their HostHeader stepper info + const host = await hostService.getHostById(userId); + + if (!host) { + throw new ApiError(404, 'Host record not found'); + } + + return { + statusCode: 200, + headers: { + 'Content-Type': 'application/json', + 'Access-Control-Allow-Origin': '*', + }, + body: JSON.stringify({ + success: true, + message: 'Stepper information retrieved successfully', + data: { + stepper: host.stepper, + }, + }), + }; +}); + diff --git a/src/modules/host/services/host.service.ts b/src/modules/host/services/host.service.ts index 52c9993..8830afe 100644 --- a/src/modules/host/services/host.service.ts +++ b/src/modules/host/services/host.service.ts @@ -35,7 +35,7 @@ export class HostService { async getHostIdByUserXid(user_xid: number) { const host = await this.prisma.hostHeader.findFirst({ where: { userXid: user_xid }, - select: { id: true, companyName: true, countryXid: true }, + select: { id: true, companyName: true, countryXid: true, stepper: true }, }); return host; } diff --git a/src/modules/minglaradmin/services/minglar.service.ts b/src/modules/minglaradmin/services/minglar.service.ts index 92f1b26..e1b44cc 100644 --- a/src/modules/minglaradmin/services/minglar.service.ts +++ b/src/modules/minglaradmin/services/minglar.service.ts @@ -50,36 +50,6 @@ export class MinglarService { return this.prisma.user.findMany({ where: { roleXid: 3 } }); } - async getHostById(id: number) { - const host = await this.prisma.user.findUnique({ - where: { id }, - include: { - HostHeader: { - include: { - currencies: true, - cities: true, - states: true, - countries: true, - HostBankDetails: true, - HostDocuments: true, - HostSuggestion: true, - hostParent: true, - HostTrack: true, - Activities: true, - }, - }, - UserOtp: true, - Token: true, - }, - }); - - if (!host || host.roleXid !== 4) { - throw new ApiError(404, 'Host not found'); - } - - return host; - } - async updateHost(id: number, data: UpdateMinglarDto) { return this.prisma.user.update({ where: { id }, @@ -149,6 +119,7 @@ export class MinglarService { if (!existingUser) { throw new ApiError(404, 'User not found'); } + console.log(existingUser.roleXid); if (existingUser.roleXid !== ROLE.MINGLAR_ADMIN || existingUser.roleXid !== ROLE.CO_ADMIN || existingUser.roleXid !== ROLE.ACCOUNT_MANAGER) { throw new ApiError(403, 'Access denied.');