45 lines
1.4 KiB
TypeScript
45 lines
1.4 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 { UserService } from '../../services/user.service';
|
|
import { verifyUserToken } from '../../../../common/middlewares/jwt/authForUser';
|
|
|
|
const userService = new UserService(prismaClient);
|
|
|
|
export const handler = safeHandler(async (
|
|
event: APIGatewayProxyEvent,
|
|
context?: Context,
|
|
): Promise<APIGatewayProxyResult> => {
|
|
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 verifyUserToken(token);
|
|
const userId = Number(userInfo.id);
|
|
|
|
if (!userId || isNaN(userId)) {
|
|
throw new ApiError(400, 'Invalid user ID');
|
|
}
|
|
|
|
const searchQuery = event.queryStringParameters?.searchQuery ?? '';
|
|
const result = await userService.searchConnectionPeople(userId, searchQuery);
|
|
|
|
return {
|
|
statusCode: 200,
|
|
headers: {
|
|
'Content-Type': 'application/json',
|
|
'Access-Control-Allow-Origin': '*',
|
|
},
|
|
body: JSON.stringify({
|
|
success: true,
|
|
message: 'Connection people retrieved successfully',
|
|
data: result,
|
|
}),
|
|
};
|
|
});
|