diff --git a/src/common/utils/handlers/safeHandler.ts b/src/common/utils/handlers/safeHandler.ts index 6608afd..b5e0ce3 100644 --- a/src/common/utils/handlers/safeHandler.ts +++ b/src/common/utils/handlers/safeHandler.ts @@ -2,22 +2,47 @@ import { APIGatewayProxyEvent, Context, APIGatewayProxyResult } from 'aws-lambda'; import ApiError from '../helper/ApiError'; -const stage = process.env.STAGE ?? 'dev'; - export const safeHandler = ( - handler: (event: APIGatewayProxyEvent, context?: Context) => Promise + handler: (event: APIGatewayProxyEvent, context?: Context) => Promise ): ((event: APIGatewayProxyEvent, context: Context) => Promise) => { return async (event, context) => { try { const result = await handler(event, context); - return ( - result ?? { + + // If handler returned null/undefined → return 204 response + if (!result) { + return { statusCode: 204, - body: '', - } - ); + body: JSON.stringify({ + success: true, + statusCode: 204, + message: 'No content', + data: null, + }), + }; + } + + // If handler returned a structured Lambda response + if (result.statusCode && result.body) { + return { + statusCode: result.statusCode, + headers: result.headers || {}, + body: injectStatusCodeIntoBody(result.body, result.statusCode), + }; + } + + // If handler returned plain data (not wrapped) + return { + statusCode: 200, + body: JSON.stringify({ + success: true, + message: 'OK', + statusCode: 200, + data: result, + }), + }; } catch (error: any) { - console.error('Error occurred:', error); + console.error('❌ Error occurred:', error); if (error instanceof ApiError) { return { @@ -25,31 +50,52 @@ export const safeHandler = ( body: JSON.stringify({ success: false, message: error.message, + statusCode: error.statusCode, data: null, error: { code: error.statusCode, description: error.message, statusCode: error.statusCode, - ...(process.env.STAGE !== 'prod' && { debug: error.stack ?? error.message }), + ...(process.env.STAGE !== 'prod' && { debug: error.stack }), }, }), }; } + // Internal Server Error fallback return { statusCode: 500, body: JSON.stringify({ success: false, message: 'Internal server error', + statusCode: 500, data: null, error: { code: 500, description: 'Internal server error', statusCode: 500, - ...(process.env.STAGE !== 'prod' && { debug: error.stack ?? error.message }), + ...(process.env.STAGE !== 'prod' && { debug: error.stack }), }, }), }; } }; }; + + +// Utility: safely inject statusCode into the JSON response body +function injectStatusCodeIntoBody(body: string, statusCode: number): string { + try { + const json = JSON.parse(body); + json.statusCode = statusCode; + return JSON.stringify(json); + } catch { + // If body is not JSON, wrap it + return JSON.stringify({ + success: true, + statusCode, + message: body, + data: null, + }); + } +} diff --git a/src/modules/host/handlers/addCompanyDetails.ts b/src/modules/host/handlers/addCompanyDetails.ts index baf1af8..1b92214 100644 --- a/src/modules/host/handlers/addCompanyDetails.ts +++ b/src/modules/host/handlers/addCompanyDetails.ts @@ -1,19 +1,18 @@ +import config from '@/config/config'; import { APIGatewayProxyEvent, APIGatewayProxyResult } from 'aws-lambda'; -import { safeHandler } from '../../../common/utils/handlers/safeHandler'; -import { PrismaService } from '../../../common/database/prisma.service'; -import { HostService } from '../../host/services/host.service'; -import ApiError from '../../../common/utils/helper/ApiError'; -import { verifyHostToken } from '../../../common/middlewares/jwt/authForHost'; -import { - hostCompanyDetailsSchema, - REQUIRED_DOC_TYPES, - hostDocumentsSchema, - parentCompanySchema, -} from '../../../common/utils/validation/host/hostCompanyDetails.validation'; import AWS from 'aws-sdk'; import Busboy from 'busboy'; import crypto from 'crypto'; -import config from '@/config/config'; +import { PrismaService } from '../../../common/database/prisma.service'; +import { verifyHostToken } from '../../../common/middlewares/jwt/authForHost'; +import { safeHandler } from '../../../common/utils/handlers/safeHandler'; +import ApiError from '../../../common/utils/helper/ApiError'; +import { + hostCompanyDetailsSchema, + parentCompanySchema, + REQUIRED_DOC_TYPES +} from '../../../common/utils/validation/host/hostCompanyDetails.validation'; +import { HostService } from '../../host/services/host.service'; const prisma = new PrismaService(); const hostService = new HostService(prisma); diff --git a/src/modules/host/handlers/registration.ts b/src/modules/host/handlers/registration.ts index 67790cb..0a7b0b6 100644 --- a/src/modules/host/handlers/registration.ts +++ b/src/modules/host/handlers/registration.ts @@ -1,10 +1,9 @@ import { APIGatewayProxyEvent, APIGatewayProxyResult, Context } from 'aws-lambda'; -import { safeHandler } from '../../../common/utils/handlers/safeHandler'; import { PrismaService } from '../../../common/database/prisma.service'; -import { HostService } from '../services/host.service'; +import { safeHandler } from '../../../common/utils/handlers/safeHandler'; import ApiError from '../../../common/utils/helper/ApiError'; -import * as bcrypt from 'bcryptjs'; import { generateOtpHelper } from '../../../common/utils/helper/sendOtp'; +import { HostService } from '../services/host.service'; const prismaService = new PrismaService(); const hostService = new HostService(prismaService); @@ -34,7 +33,7 @@ export const handler = safeHandler(async ( }); if (user && user.userPassword) { - throw new ApiError(404, 'User is already registered. Please login.'); + throw new ApiError(409, 'User is already registered. Please login.'); } let newUser; diff --git a/src/modules/minglaradmin/handlers/registration.ts b/src/modules/minglaradmin/handlers/registration.ts index 85841b7..a4ae84c 100644 --- a/src/modules/minglaradmin/handlers/registration.ts +++ b/src/modules/minglaradmin/handlers/registration.ts @@ -5,6 +5,7 @@ import { PrismaService } from '../../../common/database/prisma.service'; import ApiError from '../../../common/utils/helper/ApiError'; import * as bcrypt from 'bcryptjs'; import { generateOtpHelper } from '../../../common/utils/helper/sendOtp'; +import { ROLE } from '@/common/utils/constants/common.constant'; const prismaService = new PrismaService(); const minglarService = new MinglarService(prismaService); @@ -33,7 +34,7 @@ export const handler = safeHandler(async ( select: { emailAddress: true, id: true, userPassword: true, roleXid: true }, }); - if(!user){ + if(!user || ![ROLE.MINGLAR_ADMIN, ROLE.CO_ADMIN, ROLE.ACCOUNT_MANAGER].includes(user.roleXid)){ throw new ApiError(404, 'You are not allowed to register directly. Please contact minglar admin.'); }