From 0aa2b9b53ef188828ac2f7a6e6e5cd60603bfaa2 Mon Sep 17 00:00:00 2001 From: Mayank Mishra Date: Wed, 11 Mar 2026 13:41:15 +0530 Subject: [PATCH] fixed the search api for the specific and sending by default 15 km radius activities in the nearby activity api --- prisma/schema.prisma | 1 + .../activities/getNearbyActivities.ts | 18 +-- src/modules/user/services/user.service.ts | 124 ++++++------------ 3 files changed, 53 insertions(+), 90 deletions(-) diff --git a/prisma/schema.prisma b/prisma/schema.prisma index 26c22c3..1f50f82 100644 --- a/prisma/schema.prisma +++ b/prisma/schema.prisma @@ -2,6 +2,7 @@ generator client { provider = "prisma-client-js" binaryTargets = ["native", "rhel-openssl-3.0.x"] // Add Linux target previewFeatures = ["multiSchema"] + engineType = "library" } datasource db { diff --git a/src/modules/user/handlers/activities/getNearbyActivities.ts b/src/modules/user/handlers/activities/getNearbyActivities.ts index 4f25cad..9266621 100644 --- a/src/modules/user/handlers/activities/getNearbyActivities.ts +++ b/src/modules/user/handlers/activities/getNearbyActivities.ts @@ -27,16 +27,16 @@ export const handler = safeHandler(async ( const longParam = event.queryStringParameters?.long ?? event.queryStringParameters?.lng ?? event.queryStringParameters?.longitude; const radiusParam = event.queryStringParameters?.radiusKm ?? event.queryStringParameters?.radius; - if (!latParam || !longParam || !radiusParam) { - throw new ApiError(400, 'lat, long and radiusKm (in km) are required as query parameters'); - } + const userLat = latParam ? Number(latParam) : undefined; + const userLong = longParam ? Number(longParam) : undefined; + const radiusKm = radiusParam ? Number(radiusParam) : 15; // default 15km - const userLat = Number(latParam); - const userLong = Number(longParam); - const radiusKm = Number(radiusParam); - - if (Number.isNaN(userLat) || Number.isNaN(userLong) || Number.isNaN(radiusKm)) { - throw new ApiError(400, 'lat, long and radiusKm must be valid numbers'); + 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); diff --git a/src/modules/user/services/user.service.ts b/src/modules/user/services/user.service.ts index 841f44b..f124f0e 100644 --- a/src/modules/user/services/user.service.ts +++ b/src/modules/user/services/user.service.ts @@ -2550,99 +2550,51 @@ export class UserService { // Build the where clause dynamically const where: any = { isActive: true, - activityInternalStatus: ACTIVITY_INTERNAL_STATUS.ACTIVITY_LISTED, - amInternalStatus: ACTIVITY_AM_INTERNAL_STATUS.ACTIVITY_LISTED, - ...(activityType && { - activityType: { - is: { - activityTypeName: { - contains: activityType, - mode: 'insensitive', - }, - }, - }, - }), }; - // Add activityType filter if provided - if (activityType) { - where.activityType = { - is: { - activityTypeName: { - contains: activityType, - mode: 'insensitive', - }, - }, + if (activityType && activityType.trim().length > 0) { + where.activityTypeName = { + contains: activityType.trim(), + mode: 'insensitive', }; } - const activities = await this.prisma.activities.findMany({ + const activityTypes = await this.prisma.activityTypes.findMany({ where, select: { id: true, - activityTitle: true, - activityDescription: true, - checkInAddress: true, - activityDurationMins: true, - sustainabilityScore: true, - activityRefNumber: true, - activityType: { + activityTypeName: true, + interests: { select: { - id: true, - activityTypeName: true, - energyLevel: { - select: { - energyLevelName: true, - energyColor: true, - energyIcon: true, - }, - }, - }, - }, - checkInCity: { - select: { - cityName: true, - }, - }, - ActivitiesMedia: { - where: { isActive: true }, - select: { - id: true, - mediaFileName: true, - mediaType: true, - }, - take: 1, // Get first media item - }, + interestImage: true + } + } }, - take: 50, // Limit results to prevent too many + orderBy: { + activityTypeName: 'asc', + }, + take: 20, // limit suggestions }); // Get interested count for each activity - const activitiesWithCounts = await Promise.all( - activities.map(async (activity) => { - const interestedCount = await this.prisma.userBucketInterested.count({ - where: { - activityXid: activity.id, - isActive: true, - }, - }); + const formattedResults = await Promise.all( + activityTypes.map(async (activity) => { + const image = activity.interests?.interestImage ?? null; - // Attach presigned URLs to media - const mediaWithUrls = await attachMediaWithPresignedUrl( - activity.ActivitiesMedia, - ); + const presignedUrl = image + ? await attachPresignedUrl(image) + : null; return { - ...activity, - ActivitiesMediaPresignedUrl: mediaWithUrls, - interestedCount, - rating: 0, // Placeholder - distance: 0, // Placeholder + id: activity.id, + activityTypeName: activity.activityTypeName, + interestImage: image, + interestImagePresignedUrl: presignedUrl, }; }), ); - return activitiesWithCounts; + return formattedResults; } async getNearbyActivities( @@ -2653,15 +2605,25 @@ export class UserService { page: number, limit: number, ) { - if (userLat === undefined || userLong === undefined || radiusKm === undefined) { - throw new ApiError( - 400, - 'Latitude, longitude and radius are required to find nearby activities', - ); - } + // If lat/long not provided, fetch from user saved address + if (userLat === undefined || userLong === undefined) { + const userAddress = await this.prisma.userAddressDetails.findFirst({ + where: { userXid: userId, isActive: true }, + select: { + locationLat: true, + locationLong: true, + }, + }); - if (radiusKm <= 0) { - throw new ApiError(400, 'Radius must be greater than 0'); + if (!userAddress?.locationLat || !userAddress?.locationLong) { + throw new ApiError( + 400, + 'User location not found. Please provide lat/long.', + ); + } + + userLat = userAddress.locationLat; + userLong = userAddress.locationLong; } const skip = (page - 1) * limit;