From 5cced2981a3029f566f161ba9c07424567d14fda Mon Sep 17 00:00:00 2001 From: Mayank Mishra Date: Thu, 5 Mar 2026 18:57:58 +0530 Subject: [PATCH] sending the distance in the apis --- src/modules/user/services/user.service.ts | 76 ++++++++++++++++++++++- 1 file changed, 73 insertions(+), 3 deletions(-) diff --git a/src/modules/user/services/user.service.ts b/src/modules/user/services/user.service.ts index 7972645..5dbf62e 100644 --- a/src/modules/user/services/user.service.ts +++ b/src/modules/user/services/user.service.ts @@ -32,6 +32,30 @@ import config from '@/config/config'; // return R * c; // } +const calculateDistance = ( + lat1: number | null, + lon1: number | null, + lat2: number | null, + lon2: number | null, +) => { + if (!lat1 || !lon1 || !lat2 || !lon2) return null; + + const R = 6371; // km + const dLat = ((lat2 - lat1) * Math.PI) / 180; + const dLon = ((lon2 - lon1) * Math.PI) / 180; + + const a = + Math.sin(dLat / 2) * Math.sin(dLat / 2) + + Math.cos((lat1 * Math.PI) / 180) * + Math.cos((lat2 * Math.PI) / 180) * + Math.sin(dLon / 2) * + Math.sin(dLon / 2); + + const c = 2 * Math.atan2(Math.sqrt(a), Math.sqrt(1 - a)); + + return Number((R * c).toFixed(2)); +}; + const normalizeName = (name: string) => name.trim().toLowerCase().replace(/\s+/g, " "); @@ -637,6 +661,9 @@ export class UserService { }, }); + const userLatitude = userAddressDetails?.locationLat ?? null; + const userLongitude = userAddressDetails?.locationLong ?? null; + let effectiveLocation: { countryXid?: number | null; stateXid?: number | null; @@ -1156,6 +1183,13 @@ export class UserService { .filter(Boolean) .sort((a, b) => a - b)[0] ?? null; + const distance = calculateDistance( + userLatitude, + userLongitude, + activity.checkInLat, + activity.checkInLong, + ); + return { interestXid: activity.activityType.interestXid, activityId: activity.id, @@ -1165,7 +1199,7 @@ export class UserService { activityDurationMins: activity.activityDurationMins, sustainabilityScore: activity.sustainabilityScore, cheapestPrice, - distance: 0, + distance, rating: 0, energyLevel: activity.activityType.energyLevel ? { @@ -1246,9 +1280,14 @@ export class UserService { stateXid: true, cityXid: true, countryXid: true, + locationLat: true, + locationLong: true, }, }); + const userLat = userAddressDetails?.locationLat ?? null; + const userLng = userAddressDetails?.locationLong ?? null; + let effectiveLocation: { countryXid?: number | null; stateXid?: number | null; @@ -1398,6 +1437,8 @@ export class UserService { select: { id: true, activityTitle: true, + checkInLat: true, + checkInLong: true, activityType: { select: { interestXid: true, @@ -1418,7 +1459,12 @@ export class UserService { connectionInterestedCount: connectionInterestMap.get(a.id) ?? 0, activityTitle: a.activityTitle, - distance: 0, + distance: calculateDistance( + userLat, + userLng, + a.checkInLat, + a.checkInLong + ), rating: 0, energyLevel: { ...a.activityType.energyLevel, @@ -1489,6 +1535,8 @@ export class UserService { select: { id: true, activityTitle: true, + checkInLat: true, + checkInLong: true, activityType: { select: { energyLevel: true } }, ActivitiesMedia: { where: { isActive: true }, @@ -1507,7 +1555,12 @@ export class UserService { hypeCount: g._count.activityXid, connectionInterestedCount: connectionInterestMap.get(act.id) ?? 0, - distance: 0, + distance: calculateDistance( + userLat, + userLng, + act.checkInLat, + act.checkInLong + ), rating: 0, energyLevel: { ...act.activityType.energyLevel, @@ -1970,6 +2023,17 @@ export class UserService { }, }); + // const userLocation = await tx.userAddressDetails.findFirst({ + // where: { userXid: userId }, + // select: { + // locationLat: true, + // locationLong: true, + // }, + // }); + + // const userLat = userLocation?.locationLat ?? null; + // const userLng = userLocation?.locationLong ?? null; + // ================= PRESIGNED URL SECTION ================= // 1️⃣ Activity Media @@ -2141,6 +2205,12 @@ export class UserService { cheapestPrice, totalCapacity, rating: 0, // ⭐ Placeholder, implement rating logic as needed + // distance: calculateDistance( + // userLat, + // userLng, + // activity?.checkInLat ?? null, + // activity?.checkInLong ?? null + // ), distance: 0, interestedUserImages };