Spaces:
Sleeping
Sleeping
Update rideController.js
Browse files
src/controllers/rideController.js
CHANGED
|
@@ -2,6 +2,28 @@ const { db } = require('../config/firebase');
|
|
| 2 |
const { getBalance } = require('../utils/walletHelper');
|
| 3 |
const { pushToUser } = require('../utils/notificationHelper');
|
| 4 |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 5 |
const postRide = async (req, res) => {
|
| 6 |
const uid = req.user ? req.user.uid : req.body.captainId;
|
| 7 |
if (!uid) return res.status(400).json({ success: false, error: 'Captain ID is required', code: 'MISSING_CAPTAIN_ID' });
|
|
@@ -171,7 +193,8 @@ const postRide = async (req, res) => {
|
|
| 171 |
};
|
| 172 |
|
| 173 |
const getActiveRides = async (req, res) => {
|
| 174 |
-
const { rideType, startLocation, rideMode } = req.query;
|
|
|
|
| 175 |
try {
|
| 176 |
let requesterGender = '';
|
| 177 |
if (req.user?.uid) {
|
|
@@ -214,12 +237,40 @@ const getActiveRides = async (req, res) => {
|
|
| 214 |
}
|
| 215 |
}
|
| 216 |
|
| 217 |
-
// Filter by location
|
| 218 |
if (startLocation) {
|
| 219 |
const q = startLocation.toLowerCase();
|
| 220 |
rides = rides.filter(r => r.startLocation.toLowerCase().includes(q) || r.endLocation.toLowerCase().includes(q));
|
| 221 |
}
|
| 222 |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 223 |
return res.json({ success: true, count: rides.length, rides });
|
| 224 |
} catch (err) {
|
| 225 |
console.error('Error fetching rides:', err);
|
|
|
|
| 2 |
const { getBalance } = require('../utils/walletHelper');
|
| 3 |
const { pushToUser } = require('../utils/notificationHelper');
|
| 4 |
|
| 5 |
+
// Helper function to parse numbers
|
| 6 |
+
function parseNumber(value) {
|
| 7 |
+
const n = Number(value);
|
| 8 |
+
return Number.isFinite(n) ? n : null;
|
| 9 |
+
}
|
| 10 |
+
|
| 11 |
+
// Helper function to calculate distance in km using Haversine formula
|
| 12 |
+
function distanceKm(aLat, aLng, bLat, bLng) {
|
| 13 |
+
if ([aLat, aLng, bLat, bLng].some((v) => v == null)) return null;
|
| 14 |
+
const toRad = (value) => (value * Math.PI) / 180;
|
| 15 |
+
const earthKm = 6371;
|
| 16 |
+
const dLat = toRad(bLat - aLat);
|
| 17 |
+
const dLng = toRad(bLng - aLng);
|
| 18 |
+
const lat1 = toRad(aLat);
|
| 19 |
+
const lat2 = toRad(bLat);
|
| 20 |
+
const h =
|
| 21 |
+
Math.sin(dLat / 2) * Math.sin(dLat / 2) +
|
| 22 |
+
Math.cos(lat1) * Math.cos(lat2) *
|
| 23 |
+
Math.sin(dLng / 2) * Math.sin(dLng / 2);
|
| 24 |
+
return earthKm * 2 * Math.atan2(Math.sqrt(h), Math.sqrt(1 - h));
|
| 25 |
+
}
|
| 26 |
+
|
| 27 |
const postRide = async (req, res) => {
|
| 28 |
const uid = req.user ? req.user.uid : req.body.captainId;
|
| 29 |
if (!uid) return res.status(400).json({ success: false, error: 'Captain ID is required', code: 'MISSING_CAPTAIN_ID' });
|
|
|
|
| 193 |
};
|
| 194 |
|
| 195 |
const getActiveRides = async (req, res) => {
|
| 196 |
+
const { rideType, startLocation, rideMode, lat, lng, radiusKm } = req.query;
|
| 197 |
+
|
| 198 |
try {
|
| 199 |
let requesterGender = '';
|
| 200 |
if (req.user?.uid) {
|
|
|
|
| 237 |
}
|
| 238 |
}
|
| 239 |
|
| 240 |
+
// Filter by location (text-based)
|
| 241 |
if (startLocation) {
|
| 242 |
const q = startLocation.toLowerCase();
|
| 243 |
rides = rides.filter(r => r.startLocation.toLowerCase().includes(q) || r.endLocation.toLowerCase().includes(q));
|
| 244 |
}
|
| 245 |
|
| 246 |
+
// **NEW: Location-based distance filtering using coordinates**
|
| 247 |
+
const userLat = parseNumber(lat);
|
| 248 |
+
const userLng = parseNumber(lng);
|
| 249 |
+
const radiusKmNum = parseNumber(radiusKm) || 15;
|
| 250 |
+
|
| 251 |
+
if (userLat != null && userLng != null) {
|
| 252 |
+
// Calculate distance for each ride and filter within radius
|
| 253 |
+
rides = rides
|
| 254 |
+
.map((r) => ({
|
| 255 |
+
...r,
|
| 256 |
+
distanceKm: distanceKm(userLat, userLng, parseNumber(r.startLat), parseNumber(r.startLng)),
|
| 257 |
+
}))
|
| 258 |
+
.filter((r) => r.distanceKm == null || r.distanceKm <= radiusKmNum)
|
| 259 |
+
.sort((a, b) => {
|
| 260 |
+
const ad = a.distanceKm == null ? Number.MAX_SAFE_INTEGER : a.distanceKm;
|
| 261 |
+
const bd = b.distanceKm == null ? Number.MAX_SAFE_INTEGER : b.distanceKm;
|
| 262 |
+
if (ad !== bd) return ad - bd;
|
| 263 |
+
return String(b.createdAt || '').localeCompare(String(a.createdAt || ''));
|
| 264 |
+
})
|
| 265 |
+
.map((r) => ({
|
| 266 |
+
...r,
|
| 267 |
+
distanceKm: r.distanceKm == null ? null : Number(r.distanceKm.toFixed(2)),
|
| 268 |
+
}));
|
| 269 |
+
} else {
|
| 270 |
+
// Sort by creation date if no coordinates provided
|
| 271 |
+
rides.sort((a, b) => String(b.createdAt || '').localeCompare(String(a.createdAt || '')));
|
| 272 |
+
}
|
| 273 |
+
|
| 274 |
return res.json({ success: true, count: rides.length, rides });
|
| 275 |
} catch (err) {
|
| 276 |
console.error('Error fetching rides:', err);
|