48 lines
1.6 KiB
TypeScript
48 lines
1.6 KiB
TypeScript
import { APIGatewayProxyEvent, APIGatewayProxyResult, Context } from 'aws-lambda';
|
|
import { safeHandler } from '../../../common/utils/handlers/safeHandler';
|
|
import { prismaClient } from '../../../common/database/prisma.lambda.service';
|
|
import ApiError from '../../../common/utils/helper/ApiError';
|
|
import { verifyHostToken } from '../../../common/middlewares/jwt/authForHost';
|
|
import { HostService } from '../services/host.service';
|
|
|
|
const hostService = new HostService(prismaClient);
|
|
export const handler = safeHandler(async (
|
|
event: APIGatewayProxyEvent,
|
|
context?: Context
|
|
): Promise<APIGatewayProxyResult> => {
|
|
// 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.getHostIdByUserXid(userId);
|
|
|
|
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?.host?.stepper || null,
|
|
emailAddress: host.user?.emailAddress || null,
|
|
hostId: host.user?.userRefNumber || null,
|
|
},
|
|
}),
|
|
};
|
|
});
|
|
|