Spaces:
Sleeping
Sleeping
Commit ·
b569544
1
Parent(s): 024dda2
feat: customer requests API + FCM fixes
Browse files
src/controllers/customerRequestController.js
CHANGED
|
@@ -13,6 +13,21 @@ function distanceScore(aLat, aLng, bLat, bLng) {
|
|
| 13 |
return dLat * dLat + dLng * dLng;
|
| 14 |
}
|
| 15 |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 16 |
async function attachOffers(request) {
|
| 17 |
const snap = await db
|
| 18 |
.collection('customerRideOffers')
|
|
@@ -35,6 +50,10 @@ const createCustomerRequest = async (req, res) => {
|
|
| 35 |
requestedAt,
|
| 36 |
startLat,
|
| 37 |
startLng,
|
|
|
|
|
|
|
|
|
|
|
|
|
| 38 |
city,
|
| 39 |
} = req.body;
|
| 40 |
|
|
@@ -83,6 +102,10 @@ const createCustomerRequest = async (req, res) => {
|
|
| 83 |
customerPhoneRevealed: false,
|
| 84 |
startLat: parseNumber(startLat),
|
| 85 |
startLng: parseNumber(startLng),
|
|
|
|
|
|
|
|
|
|
|
|
|
| 86 |
city: (city || user.city || '').toString().trim(),
|
| 87 |
status: 'open',
|
| 88 |
createdAt: now,
|
|
@@ -93,12 +116,9 @@ const createCustomerRequest = async (req, res) => {
|
|
| 93 |
|
| 94 |
try {
|
| 95 |
const captainsSnap = await db.collection('users').where('role', '==', 'captain').get();
|
| 96 |
-
const requestCity = request.city.toLowerCase();
|
| 97 |
const targets = captainsSnap.docs.filter((doc) => {
|
| 98 |
const captain = doc.data() || {};
|
| 99 |
-
|
| 100 |
-
if (!requestCity) return true;
|
| 101 |
-
return (captain.city || '').toString().trim().toLowerCase() === requestCity;
|
| 102 |
});
|
| 103 |
await Promise.all(targets.map((doc) =>
|
| 104 |
pushToUser(doc.id, {
|
|
@@ -136,9 +156,16 @@ const getOpenCustomerRequests = async (req, res) => {
|
|
| 136 |
.limit(50)
|
| 137 |
.get();
|
| 138 |
|
| 139 |
-
let requests = snap.docs.map((d) =>
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 140 |
requests = requests.filter((r) => r.status !== 'accepted' || r.acceptedCaptainId === uid);
|
| 141 |
requests.sort((a, b) => {
|
|
|
|
| 142 |
const byDistance = distanceScore(captainLat, captainLng, a.startLat, a.startLng) -
|
| 143 |
distanceScore(captainLat, captainLng, b.startLat, b.startLng);
|
| 144 |
if (byDistance !== 0) return byDistance;
|
|
@@ -159,10 +186,9 @@ const getMyCustomerRequests = async (req, res) => {
|
|
| 159 |
.orderBy('createdAt', 'desc')
|
| 160 |
.limit(25)
|
| 161 |
.get();
|
| 162 |
-
const requests =
|
| 163 |
-
|
| 164 |
-
|
| 165 |
-
}
|
| 166 |
return res.json({ success: true, requests });
|
| 167 |
} catch (err) {
|
| 168 |
return res.status(500).json({ success: false, error: err.message, code: 'GET_MY_CUSTOMER_REQUESTS_ERROR' });
|
|
|
|
| 13 |
return dLat * dLat + dLng * dLng;
|
| 14 |
}
|
| 15 |
|
| 16 |
+
function distanceKm(aLat, aLng, bLat, bLng) {
|
| 17 |
+
if ([aLat, aLng, bLat, bLng].some((v) => v == null)) return null;
|
| 18 |
+
const toRad = (value) => (value * Math.PI) / 180;
|
| 19 |
+
const earthKm = 6371;
|
| 20 |
+
const dLat = toRad(bLat - aLat);
|
| 21 |
+
const dLng = toRad(bLng - aLng);
|
| 22 |
+
const lat1 = toRad(aLat);
|
| 23 |
+
const lat2 = toRad(bLat);
|
| 24 |
+
const h =
|
| 25 |
+
Math.sin(dLat / 2) * Math.sin(dLat / 2) +
|
| 26 |
+
Math.cos(lat1) * Math.cos(lat2) *
|
| 27 |
+
Math.sin(dLng / 2) * Math.sin(dLng / 2);
|
| 28 |
+
return earthKm * 2 * Math.atan2(Math.sqrt(h), Math.sqrt(1 - h));
|
| 29 |
+
}
|
| 30 |
+
|
| 31 |
async function attachOffers(request) {
|
| 32 |
const snap = await db
|
| 33 |
.collection('customerRideOffers')
|
|
|
|
| 50 |
requestedAt,
|
| 51 |
startLat,
|
| 52 |
startLng,
|
| 53 |
+
endLat,
|
| 54 |
+
endLng,
|
| 55 |
+
customerLat,
|
| 56 |
+
customerLng,
|
| 57 |
city,
|
| 58 |
} = req.body;
|
| 59 |
|
|
|
|
| 102 |
customerPhoneRevealed: false,
|
| 103 |
startLat: parseNumber(startLat),
|
| 104 |
startLng: parseNumber(startLng),
|
| 105 |
+
endLat: parseNumber(endLat),
|
| 106 |
+
endLng: parseNumber(endLng),
|
| 107 |
+
customerLat: parseNumber(customerLat),
|
| 108 |
+
customerLng: parseNumber(customerLng),
|
| 109 |
city: (city || user.city || '').toString().trim(),
|
| 110 |
status: 'open',
|
| 111 |
createdAt: now,
|
|
|
|
| 116 |
|
| 117 |
try {
|
| 118 |
const captainsSnap = await db.collection('users').where('role', '==', 'captain').get();
|
|
|
|
| 119 |
const targets = captainsSnap.docs.filter((doc) => {
|
| 120 |
const captain = doc.data() || {};
|
| 121 |
+
return Boolean(captain.fcmToken);
|
|
|
|
|
|
|
| 122 |
});
|
| 123 |
await Promise.all(targets.map((doc) =>
|
| 124 |
pushToUser(doc.id, {
|
|
|
|
| 156 |
.limit(50)
|
| 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 <= 10;
|
| 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;
|
| 169 |
const byDistance = distanceScore(captainLat, captainLng, a.startLat, a.startLng) -
|
| 170 |
distanceScore(captainLat, captainLng, b.startLat, b.startLng);
|
| 171 |
if (byDistance !== 0) return byDistance;
|
|
|
|
| 186 |
.orderBy('createdAt', 'desc')
|
| 187 |
.limit(25)
|
| 188 |
.get();
|
| 189 |
+
const requests = await Promise.all(
|
| 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' });
|