import { verifyMinglarAdminToken } from '../../../common/middlewares/jwt/authForMinglarAdmin'; import { APIGatewayProxyEvent, APIGatewayProxyResult, Context } from 'aws-lambda'; import { prismaClient } from '../../../common/database/prisma.lambda.service'; import { safeHandler } from '../../../common/utils/handlers/safeHandler'; import ApiError from '../../../common/utils/helper/ApiError'; import { MinglarService } from '../services/minglar.service'; const minglarService = new MinglarService(prismaClient); 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.'); } // Authenticate user using the shared authForHost function const userInfo = await verifyMinglarAdminToken(token); const user_xid = userInfo.id; // Parse request body let body: { password?: string; confirmPassword?: string }; try { body = event.body ? JSON.parse(event.body) : {}; } catch (error) { throw new ApiError(400, 'Invalid JSON in request body'); } const { password, confirmPassword } = body; if (!password || !confirmPassword) { throw new ApiError(400, 'Password and confirm password are required'); } // Validate password match if (password !== confirmPassword) { throw new ApiError(400, 'Password and confirm password do not match'); } // Validate password length if (password.length < 8) { throw new ApiError(400, 'Password must be at least 8 characters long'); } await minglarService.createPassword(user_xid, password); const userDetails = await minglarService.getBasicUserDetails(user_xid) return { statusCode: 200, headers: { 'Content-Type': 'application/json', 'Access-Control-Allow-Origin': '*', }, body: JSON.stringify({ success: true, message: 'Password created successfully', data: userDetails, }), }; });