72 lines
2.3 KiB
TypeScript
72 lines
2.3 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 latParam = event.queryStringParameters?.lat ?? event.queryStringParameters?.latitude;
|
|
const longParam = event.queryStringParameters?.long ?? event.queryStringParameters?.lng ?? event.queryStringParameters?.longitude;
|
|
const radiusParam = event.queryStringParameters?.radiusKm ?? event.queryStringParameters?.radius;
|
|
|
|
const userLat = latParam ? Number(latParam) : undefined;
|
|
const userLong = longParam ? Number(longParam) : undefined;
|
|
const radiusKm = radiusParam ? Number(radiusParam) : 15; // default 15km
|
|
|
|
if (
|
|
(userLat !== undefined && Number.isNaN(userLat)) ||
|
|
(userLong !== undefined && Number.isNaN(userLong)) ||
|
|
Number.isNaN(radiusKm)
|
|
) {
|
|
throw new ApiError(400, 'Invalid lat/long values');
|
|
}
|
|
|
|
const page = Number(event.queryStringParameters?.page ?? 1);
|
|
const limit = Number(event.queryStringParameters?.limit ?? 20);
|
|
|
|
if (page < 1 || limit < 1) {
|
|
throw new ApiError(400, 'Invalid pagination values');
|
|
}
|
|
|
|
const result = await userService.getNearbyActivities(
|
|
userId,
|
|
userLat,
|
|
userLong,
|
|
radiusKm,
|
|
page,
|
|
limit,
|
|
);
|
|
|
|
return {
|
|
statusCode: 200,
|
|
headers: {
|
|
'Content-Type': 'application/json',
|
|
'Access-Control-Allow-Origin': '*',
|
|
},
|
|
body: JSON.stringify({
|
|
success: true,
|
|
message: 'Nearby activities retrieved successfully',
|
|
data: result,
|
|
}),
|
|
};
|
|
});
|
|
|