Files
MinglarBackendNestJS/src/modules/host/handlers/getbyidhandler.ts

50 lines
1.5 KiB
TypeScript

import { verifyMinglarAdminHostToken } from '../../../common/middlewares/jwt/authForMinglarAdminHost';
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 { HostService } from '../services/host.service';
const hostService = new HostService(prismaClient);
export const handler = safeHandler(async (
event: APIGatewayProxyEvent,
context?: Context
): Promise<APIGatewayProxyResult> => {
// Get host ID from path parameters
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.');
}
const userInfo = await verifyMinglarAdminHostToken(token);
const id = Number(userInfo.id)
if (!id) {
throw new ApiError(400, 'Host ID is required');
}
if (isNaN(id)) {
throw new ApiError(400, 'Invalid host ID format');
}
const hostDetails = await hostService.getHostById(id);
if (!hostDetails) {
throw new ApiError(404, 'Host not found');
}
return {
statusCode: 200,
headers: {
'Content-Type': 'application/json',
'Access-Control-Allow-Origin': '*',
},
body: JSON.stringify({
success: true,
message: 'Host details retrieved successfully',
data: hostDetails,
}),
};
});