Spaces:
Sleeping
Sleeping
Commit ·
0d4e43d
1
Parent(s): e2bde26
Backend: restore ride map wallet nearby fixes
Browse files
src/controllers/customerRequestController.js
CHANGED
|
@@ -44,6 +44,7 @@ const createCustomerRequest = async (req, res) => {
|
|
| 44 |
startLocation,
|
| 45 |
endLocation,
|
| 46 |
pickupLocation,
|
|
|
|
| 47 |
vehicleType,
|
| 48 |
rideMode,
|
| 49 |
desiredFare,
|
|
@@ -90,6 +91,7 @@ const createCustomerRequest = async (req, res) => {
|
|
| 90 |
startLocation: String(startLocation).trim(),
|
| 91 |
endLocation: String(endLocation).trim(),
|
| 92 |
pickupLocation: String(pickupLocation || startLocation).trim(),
|
|
|
|
| 93 |
requestedAt: new Date(requestedAt).toISOString(),
|
| 94 |
vehicleType: normalizedVehicleType,
|
| 95 |
rideMode: normalizedRideMode,
|
|
@@ -123,7 +125,7 @@ const createCustomerRequest = async (req, res) => {
|
|
| 123 |
await Promise.all(targets.map((doc) =>
|
| 124 |
pushToUser(doc.id, {
|
| 125 |
title: 'New Customer Request',
|
| 126 |
-
body: `${request.customerName} needs a ride from ${request.startLocation} to ${request.endLocation}.`,
|
| 127 |
type: 'customer_request',
|
| 128 |
data: { requestId: ref.id, screen: 'customer-requests' },
|
| 129 |
}),
|
|
@@ -148,21 +150,30 @@ const getOpenCustomerRequests = async (req, res) => {
|
|
| 148 |
const captain = captainDoc.data();
|
| 149 |
const captainLat = parseNumber(req.query.lat);
|
| 150 |
const captainLng = parseNumber(req.query.lng);
|
|
|
|
| 151 |
|
| 152 |
const snap = await db
|
| 153 |
.collection('customerRideRequests')
|
| 154 |
.where('status', 'in', ['open', 'countered', 'accepted'])
|
| 155 |
.orderBy('createdAt', 'desc')
|
| 156 |
-
.limit(
|
| 157 |
.get();
|
| 158 |
|
| 159 |
let requests = snap.docs.map((d) => {
|
| 160 |
const request = { id: d.id, ...d.data() };
|
| 161 |
const km = distanceKm(captainLat, captainLng, request.startLat, request.startLng);
|
| 162 |
request.distanceKm = km == null ? null : Number(km.toFixed(2));
|
| 163 |
-
request.isNearby = km != null && km <=
|
| 164 |
return request;
|
| 165 |
});
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 166 |
requests = requests.filter((r) => r.status !== 'accepted' || r.acceptedCaptainId === uid);
|
| 167 |
requests.sort((a, b) => {
|
| 168 |
if (a.isNearby !== b.isNearby) return a.isNearby ? -1 : 1;
|
|
@@ -184,11 +195,16 @@ const getMyCustomerRequests = async (req, res) => {
|
|
| 184 |
.collection('customerRideRequests')
|
| 185 |
.where('customerId', '==', req.user.uid)
|
| 186 |
.orderBy('createdAt', 'desc')
|
| 187 |
-
.limit(
|
| 188 |
.get();
|
| 189 |
-
|
| 190 |
snap.docs.map((doc) => attachOffers({ id: doc.id, ...doc.data() })),
|
| 191 |
);
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 192 |
return res.json({ success: true, requests });
|
| 193 |
} catch (err) {
|
| 194 |
return res.status(500).json({ success: false, error: err.message, code: 'GET_MY_CUSTOMER_REQUESTS_ERROR' });
|
|
|
|
| 44 |
startLocation,
|
| 45 |
endLocation,
|
| 46 |
pickupLocation,
|
| 47 |
+
dropLocation,
|
| 48 |
vehicleType,
|
| 49 |
rideMode,
|
| 50 |
desiredFare,
|
|
|
|
| 91 |
startLocation: String(startLocation).trim(),
|
| 92 |
endLocation: String(endLocation).trim(),
|
| 93 |
pickupLocation: String(pickupLocation || startLocation).trim(),
|
| 94 |
+
dropLocation: String(dropLocation || endLocation).trim(),
|
| 95 |
requestedAt: new Date(requestedAt).toISOString(),
|
| 96 |
vehicleType: normalizedVehicleType,
|
| 97 |
rideMode: normalizedRideMode,
|
|
|
|
| 125 |
await Promise.all(targets.map((doc) =>
|
| 126 |
pushToUser(doc.id, {
|
| 127 |
title: 'New Customer Request',
|
| 128 |
+
body: `${request.customerName} needs a ride from ${request.startLocation} to ${request.endLocation}. ${request.pickupLocation ? `Pickup: ${request.pickupLocation}.` : ''}${request.dropLocation ? ` Drop: ${request.dropLocation}.` : ''}`,
|
| 129 |
type: 'customer_request',
|
| 130 |
data: { requestId: ref.id, screen: 'customer-requests' },
|
| 131 |
}),
|
|
|
|
| 150 |
const captain = captainDoc.data();
|
| 151 |
const captainLat = parseNumber(req.query.lat);
|
| 152 |
const captainLng = parseNumber(req.query.lng);
|
| 153 |
+
const radiusKm = parseNumber(req.query.radiusKm) || 15;
|
| 154 |
|
| 155 |
const snap = await db
|
| 156 |
.collection('customerRideRequests')
|
| 157 |
.where('status', 'in', ['open', 'countered', 'accepted'])
|
| 158 |
.orderBy('createdAt', 'desc')
|
| 159 |
+
.limit(100)
|
| 160 |
.get();
|
| 161 |
|
| 162 |
let requests = snap.docs.map((d) => {
|
| 163 |
const request = { id: d.id, ...d.data() };
|
| 164 |
const km = distanceKm(captainLat, captainLng, request.startLat, request.startLng);
|
| 165 |
request.distanceKm = km == null ? null : Number(km.toFixed(2));
|
| 166 |
+
request.isNearby = km != null && km <= radiusKm;
|
| 167 |
return request;
|
| 168 |
});
|
| 169 |
+
const now = new Date();
|
| 170 |
+
requests = requests.filter((r) => {
|
| 171 |
+
const requested = r.requestedAt ? new Date(r.requestedAt) : null;
|
| 172 |
+
return !requested || Number.isNaN(requested.getTime()) || requested >= now;
|
| 173 |
+
});
|
| 174 |
+
if (captainLat != null && captainLng != null) {
|
| 175 |
+
requests = requests.filter((r) => r.distanceKm == null || r.distanceKm <= radiusKm);
|
| 176 |
+
}
|
| 177 |
requests = requests.filter((r) => r.status !== 'accepted' || r.acceptedCaptainId === uid);
|
| 178 |
requests.sort((a, b) => {
|
| 179 |
if (a.isNearby !== b.isNearby) return a.isNearby ? -1 : 1;
|
|
|
|
| 195 |
.collection('customerRideRequests')
|
| 196 |
.where('customerId', '==', req.user.uid)
|
| 197 |
.orderBy('createdAt', 'desc')
|
| 198 |
+
.limit(100)
|
| 199 |
.get();
|
| 200 |
+
let requests = await Promise.all(
|
| 201 |
snap.docs.map((doc) => attachOffers({ id: doc.id, ...doc.data() })),
|
| 202 |
);
|
| 203 |
+
const now = new Date();
|
| 204 |
+
requests = requests.filter((r) => {
|
| 205 |
+
const requested = r.requestedAt ? new Date(r.requestedAt) : null;
|
| 206 |
+
return !requested || Number.isNaN(requested.getTime()) || requested >= now;
|
| 207 |
+
});
|
| 208 |
return res.json({ success: true, requests });
|
| 209 |
} catch (err) {
|
| 210 |
return res.status(500).json({ success: false, error: err.message, code: 'GET_MY_CUSTOMER_REQUESTS_ERROR' });
|
src/controllers/dealController.js
CHANGED
|
@@ -93,6 +93,12 @@ async function populateRide(deal) {
|
|
| 93 |
id: deal.rideId,
|
| 94 |
startLocation: ride.startLocation,
|
| 95 |
endLocation: ride.endLocation,
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 96 |
departureTime: ride.departureTime,
|
| 97 |
captainName: ride.captainName,
|
| 98 |
captainId: ride.captainId,
|
|
@@ -277,9 +283,10 @@ const confirmDeal = async (req, res) => {
|
|
| 277 |
await db.collection('transactions').add({
|
| 278 |
walletId: uid,
|
| 279 |
type: 'commission',
|
| 280 |
-
amount:
|
| 281 |
reference: dealId,
|
| 282 |
description: `5% commission for deal ${dealId}`,
|
|
|
|
| 283 |
createdAt: now,
|
| 284 |
});
|
| 285 |
|
|
|
|
| 93 |
id: deal.rideId,
|
| 94 |
startLocation: ride.startLocation,
|
| 95 |
endLocation: ride.endLocation,
|
| 96 |
+
exactLocation: ride.exactLocation,
|
| 97 |
+
exactDropLocation: ride.exactDropLocation,
|
| 98 |
+
startLat: ride.startLat,
|
| 99 |
+
startLng: ride.startLng,
|
| 100 |
+
endLat: ride.endLat,
|
| 101 |
+
endLng: ride.endLng,
|
| 102 |
departureTime: ride.departureTime,
|
| 103 |
captainName: ride.captainName,
|
| 104 |
captainId: ride.captainId,
|
|
|
|
| 283 |
await db.collection('transactions').add({
|
| 284 |
walletId: uid,
|
| 285 |
type: 'commission',
|
| 286 |
+
amount: commission,
|
| 287 |
reference: dealId,
|
| 288 |
description: `5% commission for deal ${dealId}`,
|
| 289 |
+
balanceAfter: newBalance,
|
| 290 |
createdAt: now,
|
| 291 |
});
|
| 292 |
|
src/controllers/rideController.js
CHANGED
|
@@ -49,7 +49,7 @@ const postRide = async (req, res) => {
|
|
| 49 |
departureTime, totalSeats, suggestedFare,
|
| 50 |
rideType, vehicleType, acceptsDelivery, vehicleInfo,
|
| 51 |
tourType, maxPassengers,
|
| 52 |
-
cargoType, weightCapacity, truckSize,
|
| 53 |
} = req.body;
|
| 54 |
|
| 55 |
// Validate rideMode
|
|
@@ -112,6 +112,8 @@ const postRide = async (req, res) => {
|
|
| 112 |
captainGender: (userData.gender || '').toString().toLowerCase() || null,
|
| 113 |
startLocation: startLocation.trim(),
|
| 114 |
endLocation: endLocation.trim(),
|
|
|
|
|
|
|
| 115 |
startLat: parseFloat(startLat) || 0.0,
|
| 116 |
startLng: parseFloat(startLng) || 0.0,
|
| 117 |
endLat: parseFloat(endLat) || 0.0,
|
|
@@ -177,7 +179,7 @@ const postRide = async (req, res) => {
|
|
| 177 |
});
|
| 178 |
await Promise.all(targets.map(doc => pushToUser(doc.id, {
|
| 179 |
title: 'New Ride Available',
|
| 180 |
-
body: `New ride from ${ride.startLocation} to ${ride.endLocation} near you!`,
|
| 181 |
type: 'new_ride',
|
| 182 |
data: { rideId: ref.id, screen: 'find-ride' },
|
| 183 |
})));
|
|
@@ -193,9 +195,11 @@ const postRide = async (req, res) => {
|
|
| 193 |
};
|
| 194 |
|
| 195 |
const getActiveRides = async (req, res) => {
|
| 196 |
-
const { rideType, startLocation,
|
| 197 |
-
|
| 198 |
try {
|
|
|
|
|
|
|
|
|
|
| 199 |
let requesterGender = '';
|
| 200 |
if (req.user?.uid) {
|
| 201 |
const requesterDoc = await db.collection('users').doc(req.user.uid).get();
|
|
@@ -240,22 +244,21 @@ const getActiveRides = async (req, res) => {
|
|
| 240 |
// Filter by location (text-based)
|
| 241 |
if (startLocation) {
|
| 242 |
const q = startLocation.toLowerCase();
|
| 243 |
-
rides = rides.filter(r => r.startLocation.toLowerCase().includes(q)
|
| 244 |
}
|
| 245 |
|
| 246 |
-
|
| 247 |
-
|
| 248 |
-
|
| 249 |
-
|
| 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 <=
|
| 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;
|
|
@@ -267,7 +270,6 @@ const getActiveRides = async (req, res) => {
|
|
| 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 |
|
|
@@ -302,8 +304,17 @@ const getMyRides = async (req, res) => {
|
|
| 302 |
if (!uid) return res.status(400).json({ success: false, error: 'Captain ID is required', code: 'MISSING_CAPTAIN_ID' });
|
| 303 |
|
| 304 |
try {
|
|
|
|
| 305 |
const snap = await db.collection('rides').where('captainId', '==', uid).orderBy('createdAt', 'desc').get();
|
| 306 |
-
const rides = snap.docs
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 307 |
return res.json({ success: true, rides });
|
| 308 |
} catch (err) {
|
| 309 |
return res.status(500).json({ success: false, error: err.message, code: 'GET_MY_RIDES_ERROR' });
|
|
|
|
| 49 |
departureTime, totalSeats, suggestedFare,
|
| 50 |
rideType, vehicleType, acceptsDelivery, vehicleInfo,
|
| 51 |
tourType, maxPassengers,
|
| 52 |
+
cargoType, weightCapacity, truckSize, exactLocation, exactDropLocation,
|
| 53 |
} = req.body;
|
| 54 |
|
| 55 |
// Validate rideMode
|
|
|
|
| 112 |
captainGender: (userData.gender || '').toString().toLowerCase() || null,
|
| 113 |
startLocation: startLocation.trim(),
|
| 114 |
endLocation: endLocation.trim(),
|
| 115 |
+
exactLocation: (exactLocation || '').toString().trim() || null,
|
| 116 |
+
exactDropLocation: (exactDropLocation || '').toString().trim() || null,
|
| 117 |
startLat: parseFloat(startLat) || 0.0,
|
| 118 |
startLng: parseFloat(startLng) || 0.0,
|
| 119 |
endLat: parseFloat(endLat) || 0.0,
|
|
|
|
| 179 |
});
|
| 180 |
await Promise.all(targets.map(doc => pushToUser(doc.id, {
|
| 181 |
title: 'New Ride Available',
|
| 182 |
+
body: `New ride from ${ride.startLocation} to ${ride.endLocation} near you!${ride.exactLocation ? ` Exact pickup: ${ride.exactLocation}.` : ''}${ride.exactDropLocation ? ` Exact drop: ${ride.exactDropLocation}.` : ''}`,
|
| 183 |
type: 'new_ride',
|
| 184 |
data: { rideId: ref.id, screen: 'find-ride' },
|
| 185 |
})));
|
|
|
|
| 195 |
};
|
| 196 |
|
| 197 |
const getActiveRides = async (req, res) => {
|
| 198 |
+
const { rideType, startLocation, endLocation, rideMode } = req.query;
|
|
|
|
| 199 |
try {
|
| 200 |
+
const userLat = parseNumber(req.query.lat);
|
| 201 |
+
const userLng = parseNumber(req.query.lng);
|
| 202 |
+
const radiusKm = parseNumber(req.query.radiusKm) || 15;
|
| 203 |
let requesterGender = '';
|
| 204 |
if (req.user?.uid) {
|
| 205 |
const requesterDoc = await db.collection('users').doc(req.user.uid).get();
|
|
|
|
| 244 |
// Filter by location (text-based)
|
| 245 |
if (startLocation) {
|
| 246 |
const q = startLocation.toLowerCase();
|
| 247 |
+
rides = rides.filter(r => r.startLocation.toLowerCase().includes(q));
|
| 248 |
}
|
| 249 |
|
| 250 |
+
if (endLocation) {
|
| 251 |
+
const q = endLocation.toLowerCase();
|
| 252 |
+
rides = rides.filter(r => r.endLocation.toLowerCase().includes(q));
|
| 253 |
+
}
|
| 254 |
|
| 255 |
if (userLat != null && userLng != null) {
|
|
|
|
| 256 |
rides = rides
|
| 257 |
.map((r) => ({
|
| 258 |
...r,
|
| 259 |
distanceKm: distanceKm(userLat, userLng, parseNumber(r.startLat), parseNumber(r.startLng)),
|
| 260 |
}))
|
| 261 |
+
.filter((r) => r.distanceKm == null || r.distanceKm <= radiusKm)
|
| 262 |
.sort((a, b) => {
|
| 263 |
const ad = a.distanceKm == null ? Number.MAX_SAFE_INTEGER : a.distanceKm;
|
| 264 |
const bd = b.distanceKm == null ? Number.MAX_SAFE_INTEGER : b.distanceKm;
|
|
|
|
| 270 |
distanceKm: r.distanceKm == null ? null : Number(r.distanceKm.toFixed(2)),
|
| 271 |
}));
|
| 272 |
} else {
|
|
|
|
| 273 |
rides.sort((a, b) => String(b.createdAt || '').localeCompare(String(a.createdAt || '')));
|
| 274 |
}
|
| 275 |
|
|
|
|
| 304 |
if (!uid) return res.status(400).json({ success: false, error: 'Captain ID is required', code: 'MISSING_CAPTAIN_ID' });
|
| 305 |
|
| 306 |
try {
|
| 307 |
+
const now = new Date();
|
| 308 |
const snap = await db.collection('rides').where('captainId', '==', uid).orderBy('createdAt', 'desc').get();
|
| 309 |
+
const rides = snap.docs
|
| 310 |
+
.map(d => ({ id: d.id, ...d.data() }))
|
| 311 |
+
.filter((ride) => {
|
| 312 |
+
const status = (ride.status || '').toString().toLowerCase();
|
| 313 |
+
if (['completed', 'cancelled'].includes(status)) return false;
|
| 314 |
+
const departure = ride.departureTime ? new Date(ride.departureTime) : null;
|
| 315 |
+
if (!departure || Number.isNaN(departure.getTime())) return true;
|
| 316 |
+
return departure >= now;
|
| 317 |
+
});
|
| 318 |
return res.json({ success: true, rides });
|
| 319 |
} catch (err) {
|
| 320 |
return res.status(500).json({ success: false, error: err.message, code: 'GET_MY_RIDES_ERROR' });
|
src/controllers/walletController.js
CHANGED
|
@@ -63,9 +63,11 @@ const getEarningsSummary = async (req, res) => {
|
|
| 63 |
const now = new Date();
|
| 64 |
|
| 65 |
for (const tx of transactions) {
|
| 66 |
-
const amt = tx.amount || 0;
|
| 67 |
if (tx.type === 'commission_deduction') {
|
| 68 |
totalDebit += amt;
|
|
|
|
|
|
|
| 69 |
} else {
|
| 70 |
totalCredit += amt;
|
| 71 |
}
|
|
@@ -73,7 +75,8 @@ const getEarningsSummary = async (req, res) => {
|
|
| 73 |
const diffDays = Math.floor((now - created) / (1000 * 60 * 60 * 24));
|
| 74 |
if (diffDays >= 0 && diffDays < 7) {
|
| 75 |
const idx = 6 - diffDays;
|
| 76 |
-
|
|
|
|
| 77 |
}
|
| 78 |
}
|
| 79 |
|
|
|
|
| 63 |
const now = new Date();
|
| 64 |
|
| 65 |
for (const tx of transactions) {
|
| 66 |
+
const amt = Math.abs(Number(tx.amount || 0));
|
| 67 |
if (tx.type === 'commission_deduction') {
|
| 68 |
totalDebit += amt;
|
| 69 |
+
} else if (tx.type === 'commission') {
|
| 70 |
+
totalDebit += amt;
|
| 71 |
} else {
|
| 72 |
totalCredit += amt;
|
| 73 |
}
|
|
|
|
| 75 |
const diffDays = Math.floor((now - created) / (1000 * 60 * 60 * 24));
|
| 76 |
if (diffDays >= 0 && diffDays < 7) {
|
| 77 |
const idx = 6 - diffDays;
|
| 78 |
+
const isDebit = tx.type === 'commission_deduction' || tx.type === 'commission';
|
| 79 |
+
weekBuckets[idx] += isDebit ? -amt : amt;
|
| 80 |
}
|
| 81 |
}
|
| 82 |
|
src/utils/walletHelper.js
CHANGED
|
@@ -40,7 +40,7 @@ async function getBalance(uid) {
|
|
| 40 |
return wallet.balance || 0;
|
| 41 |
}
|
| 42 |
|
| 43 |
-
async function recordTransaction({ walletId, type, amount, description, dealId, reference }) {
|
| 44 |
await db.collection('transactions').add({
|
| 45 |
walletId,
|
| 46 |
type,
|
|
@@ -48,6 +48,7 @@ async function recordTransaction({ walletId, type, amount, description, dealId,
|
|
| 48 |
description: description || type,
|
| 49 |
dealId: dealId || null,
|
| 50 |
reference: reference || null,
|
|
|
|
| 51 |
createdAt: new Date().toISOString(),
|
| 52 |
});
|
| 53 |
}
|
|
@@ -70,6 +71,7 @@ async function addBalance(uid, amount, meta = {}) {
|
|
| 70 |
amount,
|
| 71 |
description: meta.description || 'Wallet top-up',
|
| 72 |
reference: meta.reference,
|
|
|
|
| 73 |
});
|
| 74 |
return newBalance;
|
| 75 |
}
|
|
@@ -96,6 +98,7 @@ async function deductBalance(uid, amount, meta = {}) {
|
|
| 96 |
amount,
|
| 97 |
description: meta.description || 'Wallet deduction',
|
| 98 |
dealId: meta.dealId,
|
|
|
|
| 99 |
});
|
| 100 |
return newBalance;
|
| 101 |
}
|
|
|
|
| 40 |
return wallet.balance || 0;
|
| 41 |
}
|
| 42 |
|
| 43 |
+
async function recordTransaction({ walletId, type, amount, description, dealId, reference, balanceAfter }) {
|
| 44 |
await db.collection('transactions').add({
|
| 45 |
walletId,
|
| 46 |
type,
|
|
|
|
| 48 |
description: description || type,
|
| 49 |
dealId: dealId || null,
|
| 50 |
reference: reference || null,
|
| 51 |
+
balanceAfter: balanceAfter ?? null,
|
| 52 |
createdAt: new Date().toISOString(),
|
| 53 |
});
|
| 54 |
}
|
|
|
|
| 71 |
amount,
|
| 72 |
description: meta.description || 'Wallet top-up',
|
| 73 |
reference: meta.reference,
|
| 74 |
+
balanceAfter: newBalance,
|
| 75 |
});
|
| 76 |
return newBalance;
|
| 77 |
}
|
|
|
|
| 98 |
amount,
|
| 99 |
description: meta.description || 'Wallet deduction',
|
| 100 |
dealId: meta.dealId,
|
| 101 |
+
balanceAfter: newBalance,
|
| 102 |
});
|
| 103 |
return newBalance;
|
| 104 |
}
|