Spaces:
Sleeping
Sleeping
Update rideController.js
Browse files
src/controllers/rideController.js
CHANGED
|
@@ -30,27 +30,48 @@ const postRide = async (req, res) => {
|
|
| 30 |
cargoType, weightCapacity, truckSize,
|
| 31 |
} = req.body;
|
| 32 |
|
|
|
|
| 33 |
const rideMode = req.body.rideMode || 'share';
|
| 34 |
const normalizedRideMode = String(rideMode).toLowerCase();
|
| 35 |
if (!['share', 'solo'].includes(normalizedRideMode)) {
|
| 36 |
return res.status(400).json({ success: false, error: 'rideMode must be share or solo', code: 'INVALID_RIDE_MODE' });
|
| 37 |
}
|
| 38 |
|
|
|
|
|
|
|
|
|
|
|
|
|
| 39 |
const normalizedVehicleType = String(vehicleType || '').toLowerCase();
|
| 40 |
-
|
| 41 |
-
|
| 42 |
-
|
| 43 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 44 |
}
|
| 45 |
|
| 46 |
-
|
| 47 |
-
if (captainVehicleType) {
|
| 48 |
const postingType = inferredVehicleType === 'shazore' ? 'shazore' : inferredVehicleType;
|
| 49 |
if (postingType !== captainVehicleType) {
|
| 50 |
return res.status(403).json({ success: false, error: `You can only post ${captainVehicleType} rides`, code: 'VEHICLE_TYPE_MISMATCH' });
|
| 51 |
}
|
| 52 |
}
|
| 53 |
|
|
|
|
| 54 |
if (!startLocation || !endLocation || !departureTime || !totalSeats || !suggestedFare) {
|
| 55 |
return res.status(400).json({ success: false, error: 'Missing required fields', code: 'MISSING_FIELDS' });
|
| 56 |
}
|
|
@@ -60,6 +81,7 @@ const postRide = async (req, res) => {
|
|
| 60 |
return res.status(400).json({ success: false, error: 'totalSeats must be at least 1', code: 'INVALID_TOTAL_SEATS' });
|
| 61 |
}
|
| 62 |
|
|
|
|
| 63 |
const ride = {
|
| 64 |
captainId: uid,
|
| 65 |
captainName: userData.name || 'Anonymous',
|
|
@@ -77,7 +99,7 @@ const postRide = async (req, res) => {
|
|
| 77 |
availableSeats: parsedSeats,
|
| 78 |
full: false,
|
| 79 |
suggestedFare: parseFloat(suggestedFare),
|
| 80 |
-
rideType:
|
| 81 |
rideMode: normalizedRideMode,
|
| 82 |
vehicleType: inferredVehicleType === 'shazore' ? 'truck' : inferredVehicleType,
|
| 83 |
isShazoreRide: inferredVehicleType === 'shazore',
|
|
@@ -89,7 +111,8 @@ const postRide = async (req, res) => {
|
|
| 89 |
updatedAt: new Date().toISOString(),
|
| 90 |
};
|
| 91 |
|
| 92 |
-
|
|
|
|
| 93 |
const normalizedTourType = String(tourType || 'share').toLowerCase();
|
| 94 |
if (!['share', 'solo'].includes(normalizedTourType)) {
|
| 95 |
return res.status(400).json({ success: false, error: "tourType must be share or solo", code: 'INVALID_TOUR_TYPE' });
|
|
@@ -106,6 +129,7 @@ const postRide = async (req, res) => {
|
|
| 106 |
}
|
| 107 |
}
|
| 108 |
|
|
|
|
| 109 |
if (inferredVehicleType === 'truck' || inferredVehicleType === 'shazore') {
|
| 110 |
const normalizedTruckSize = String(truckSize || '').toLowerCase();
|
| 111 |
if (normalizedTruckSize && !['mini', 'half', 'full'].includes(normalizedTruckSize)) {
|
|
@@ -114,11 +138,12 @@ const postRide = async (req, res) => {
|
|
| 114 |
ride.cargoType = (cargoType || '').toString().trim() || null;
|
| 115 |
ride.weightCapacity = weightCapacity != null ? Number(weightCapacity) : null;
|
| 116 |
ride.truckSize = normalizedTruckSize || (inferredVehicleType === 'shazore' ? 'full' : null);
|
| 117 |
-
if (inferredVehicleType === 'shazore') ride.isShazoreRide = true;
|
| 118 |
}
|
| 119 |
|
|
|
|
| 120 |
const ref = await db.collection('rides').add(ride);
|
| 121 |
|
|
|
|
| 122 |
try {
|
| 123 |
const usersSnap = await db.collection('users').where('role', 'in', ['customer', 'passenger']).get();
|
| 124 |
const captainCity = (userData.city || '').toString().trim().toLowerCase();
|
|
@@ -159,16 +184,20 @@ const getActiveRides = async (req, res) => {
|
|
| 159 |
const snap = await query.orderBy('departureTime').get();
|
| 160 |
let rides = snap.docs.map(d => ({ id: d.id, ...d.data() }));
|
| 161 |
|
|
|
|
| 162 |
if (requesterGender !== 'female') {
|
| 163 |
rides = rides.filter(r => r.isLadiesRide !== true);
|
| 164 |
}
|
| 165 |
|
|
|
|
| 166 |
if (rideType) {
|
| 167 |
const rt = String(rideType).toLowerCase();
|
| 168 |
-
if (['car', 'bike', 'bus', 'truck'
|
| 169 |
rides = rides.filter(r => (r.vehicleType || '').toString().toLowerCase() === rt);
|
| 170 |
} else if (rt === 'shazore') {
|
| 171 |
rides = rides.filter(r => r.isShazoreRide === true);
|
|
|
|
|
|
|
| 172 |
} else if (rt === 'ladies') {
|
| 173 |
if (requesterGender !== 'female') rides = [];
|
| 174 |
else rides = rides.filter(r => r.isLadiesRide === true);
|
|
@@ -177,6 +206,7 @@ const getActiveRides = async (req, res) => {
|
|
| 177 |
}
|
| 178 |
}
|
| 179 |
|
|
|
|
| 180 |
if (rideMode) {
|
| 181 |
const rm = String(rideMode).toLowerCase();
|
| 182 |
if (['solo', 'share'].includes(rm)) {
|
|
@@ -184,6 +214,7 @@ const getActiveRides = async (req, res) => {
|
|
| 184 |
}
|
| 185 |
}
|
| 186 |
|
|
|
|
| 187 |
if (startLocation) {
|
| 188 |
const q = startLocation.toLowerCase();
|
| 189 |
rides = rides.filter(r => r.startLocation.toLowerCase().includes(q) || r.endLocation.toLowerCase().includes(q));
|
|
@@ -202,6 +233,7 @@ const updateRideStatus = async (req, res) => {
|
|
| 202 |
const uid = req.user ? req.user.uid : req.body.captainId;
|
| 203 |
const validStatuses = ['active', 'filled', 'completed', 'cancelled'];
|
| 204 |
if (!validStatuses.includes(status)) return res.status(400).json({ success: false, error: 'Invalid status type', code: 'INVALID_STATUS' });
|
|
|
|
| 205 |
try {
|
| 206 |
const rideRef = db.collection('rides').doc(rideId);
|
| 207 |
const rideDoc = await rideRef.get();
|
|
@@ -217,6 +249,7 @@ const updateRideStatus = async (req, res) => {
|
|
| 217 |
const getMyRides = async (req, res) => {
|
| 218 |
const uid = req.user ? req.user.uid : req.query.captainId;
|
| 219 |
if (!uid) return res.status(400).json({ success: false, error: 'Captain ID is required', code: 'MISSING_CAPTAIN_ID' });
|
|
|
|
| 220 |
try {
|
| 221 |
const snap = await db.collection('rides').where('captainId', '==', uid).orderBy('createdAt', 'desc').get();
|
| 222 |
const rides = snap.docs.map(d => ({ id: d.id, ...d.data() }));
|
|
@@ -242,6 +275,7 @@ const updateRideLocation = async (req, res) => {
|
|
| 242 |
const { lat, lng } = req.body;
|
| 243 |
const uid = req.user ? req.user.uid : req.body.captainId;
|
| 244 |
if (lat === undefined || lng === undefined) return res.status(400).json({ success: false, error: 'Latitude and Longitude are required', code: 'MISSING_COORDINATES' });
|
|
|
|
| 245 |
try {
|
| 246 |
const rideRef = db.collection('rides').doc(rideId);
|
| 247 |
const rideDoc = await rideRef.get();
|
|
|
|
| 30 |
cargoType, weightCapacity, truckSize,
|
| 31 |
} = req.body;
|
| 32 |
|
| 33 |
+
// Validate rideMode
|
| 34 |
const rideMode = req.body.rideMode || 'share';
|
| 35 |
const normalizedRideMode = String(rideMode).toLowerCase();
|
| 36 |
if (!['share', 'solo'].includes(normalizedRideMode)) {
|
| 37 |
return res.status(400).json({ success: false, error: 'rideMode must be share or solo', code: 'INVALID_RIDE_MODE' });
|
| 38 |
}
|
| 39 |
|
| 40 |
+
// Validate rideType and vehicleType
|
| 41 |
+
const normalizedRideType = String(rideType || 'random').toLowerCase();
|
| 42 |
+
const isTourRide = normalizedRideType === 'tour';
|
| 43 |
+
const captainVehicleType = String(userData.captainVehicleType || '').toLowerCase();
|
| 44 |
const normalizedVehicleType = String(vehicleType || '').toLowerCase();
|
| 45 |
+
|
| 46 |
+
// Allowed vehicle types
|
| 47 |
+
const allowedVehicleTypes = ['car', 'bike', 'bus', 'truck', 'shazore'];
|
| 48 |
+
|
| 49 |
+
// Infer vehicle type - FIXED
|
| 50 |
+
let inferredVehicleType;
|
| 51 |
+
if (isTourRide) {
|
| 52 |
+
inferredVehicleType = 'tour';
|
| 53 |
+
} else if (normalizedVehicleType && allowedVehicleTypes.includes(normalizedVehicleType)) {
|
| 54 |
+
inferredVehicleType = normalizedVehicleType;
|
| 55 |
+
} else if (captainVehicleType && allowedVehicleTypes.includes(captainVehicleType)) {
|
| 56 |
+
inferredVehicleType = captainVehicleType;
|
| 57 |
+
} else {
|
| 58 |
+
inferredVehicleType = 'car';
|
| 59 |
+
}
|
| 60 |
+
|
| 61 |
+
// Validate vehicle type
|
| 62 |
+
if (!allowedVehicleTypes.includes(inferredVehicleType) && inferredVehicleType !== 'tour') {
|
| 63 |
+
return res.status(400).json({ success: false, error: 'vehicleType must be one of car, bike, bus, truck, shazore', code: 'INVALID_VEHICLE_TYPE' });
|
| 64 |
}
|
| 65 |
|
| 66 |
+
// Check captain vehicle type mismatch (only for non-tour rides)
|
| 67 |
+
if (captainVehicleType && !isTourRide) {
|
| 68 |
const postingType = inferredVehicleType === 'shazore' ? 'shazore' : inferredVehicleType;
|
| 69 |
if (postingType !== captainVehicleType) {
|
| 70 |
return res.status(403).json({ success: false, error: `You can only post ${captainVehicleType} rides`, code: 'VEHICLE_TYPE_MISMATCH' });
|
| 71 |
}
|
| 72 |
}
|
| 73 |
|
| 74 |
+
// Required fields check
|
| 75 |
if (!startLocation || !endLocation || !departureTime || !totalSeats || !suggestedFare) {
|
| 76 |
return res.status(400).json({ success: false, error: 'Missing required fields', code: 'MISSING_FIELDS' });
|
| 77 |
}
|
|
|
|
| 81 |
return res.status(400).json({ success: false, error: 'totalSeats must be at least 1', code: 'INVALID_TOTAL_SEATS' });
|
| 82 |
}
|
| 83 |
|
| 84 |
+
// Build ride object
|
| 85 |
const ride = {
|
| 86 |
captainId: uid,
|
| 87 |
captainName: userData.name || 'Anonymous',
|
|
|
|
| 99 |
availableSeats: parsedSeats,
|
| 100 |
full: false,
|
| 101 |
suggestedFare: parseFloat(suggestedFare),
|
| 102 |
+
rideType: normalizedRideType,
|
| 103 |
rideMode: normalizedRideMode,
|
| 104 |
vehicleType: inferredVehicleType === 'shazore' ? 'truck' : inferredVehicleType,
|
| 105 |
isShazoreRide: inferredVehicleType === 'shazore',
|
|
|
|
| 111 |
updatedAt: new Date().toISOString(),
|
| 112 |
};
|
| 113 |
|
| 114 |
+
// Handle tour ride specific fields
|
| 115 |
+
if (isTourRide) {
|
| 116 |
const normalizedTourType = String(tourType || 'share').toLowerCase();
|
| 117 |
if (!['share', 'solo'].includes(normalizedTourType)) {
|
| 118 |
return res.status(400).json({ success: false, error: "tourType must be share or solo", code: 'INVALID_TOUR_TYPE' });
|
|
|
|
| 129 |
}
|
| 130 |
}
|
| 131 |
|
| 132 |
+
// Handle truck / shazore specific fields
|
| 133 |
if (inferredVehicleType === 'truck' || inferredVehicleType === 'shazore') {
|
| 134 |
const normalizedTruckSize = String(truckSize || '').toLowerCase();
|
| 135 |
if (normalizedTruckSize && !['mini', 'half', 'full'].includes(normalizedTruckSize)) {
|
|
|
|
| 138 |
ride.cargoType = (cargoType || '').toString().trim() || null;
|
| 139 |
ride.weightCapacity = weightCapacity != null ? Number(weightCapacity) : null;
|
| 140 |
ride.truckSize = normalizedTruckSize || (inferredVehicleType === 'shazore' ? 'full' : null);
|
|
|
|
| 141 |
}
|
| 142 |
|
| 143 |
+
// Save to Firebase
|
| 144 |
const ref = await db.collection('rides').add(ride);
|
| 145 |
|
| 146 |
+
// Send notifications
|
| 147 |
try {
|
| 148 |
const usersSnap = await db.collection('users').where('role', 'in', ['customer', 'passenger']).get();
|
| 149 |
const captainCity = (userData.city || '').toString().trim().toLowerCase();
|
|
|
|
| 184 |
const snap = await query.orderBy('departureTime').get();
|
| 185 |
let rides = snap.docs.map(d => ({ id: d.id, ...d.data() }));
|
| 186 |
|
| 187 |
+
// Filter ladies rides
|
| 188 |
if (requesterGender !== 'female') {
|
| 189 |
rides = rides.filter(r => r.isLadiesRide !== true);
|
| 190 |
}
|
| 191 |
|
| 192 |
+
// Filter by rideType / vehicleType
|
| 193 |
if (rideType) {
|
| 194 |
const rt = String(rideType).toLowerCase();
|
| 195 |
+
if (['car', 'bike', 'bus', 'truck'].includes(rt)) {
|
| 196 |
rides = rides.filter(r => (r.vehicleType || '').toString().toLowerCase() === rt);
|
| 197 |
} else if (rt === 'shazore') {
|
| 198 |
rides = rides.filter(r => r.isShazoreRide === true);
|
| 199 |
+
} else if (rt === 'tour') {
|
| 200 |
+
rides = rides.filter(r => (r.rideType || '').toString().toLowerCase() === 'tour');
|
| 201 |
} else if (rt === 'ladies') {
|
| 202 |
if (requesterGender !== 'female') rides = [];
|
| 203 |
else rides = rides.filter(r => r.isLadiesRide === true);
|
|
|
|
| 206 |
}
|
| 207 |
}
|
| 208 |
|
| 209 |
+
// Filter by rideMode
|
| 210 |
if (rideMode) {
|
| 211 |
const rm = String(rideMode).toLowerCase();
|
| 212 |
if (['solo', 'share'].includes(rm)) {
|
|
|
|
| 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));
|
|
|
|
| 233 |
const uid = req.user ? req.user.uid : req.body.captainId;
|
| 234 |
const validStatuses = ['active', 'filled', 'completed', 'cancelled'];
|
| 235 |
if (!validStatuses.includes(status)) return res.status(400).json({ success: false, error: 'Invalid status type', code: 'INVALID_STATUS' });
|
| 236 |
+
|
| 237 |
try {
|
| 238 |
const rideRef = db.collection('rides').doc(rideId);
|
| 239 |
const rideDoc = await rideRef.get();
|
|
|
|
| 249 |
const getMyRides = async (req, res) => {
|
| 250 |
const uid = req.user ? req.user.uid : req.query.captainId;
|
| 251 |
if (!uid) return res.status(400).json({ success: false, error: 'Captain ID is required', code: 'MISSING_CAPTAIN_ID' });
|
| 252 |
+
|
| 253 |
try {
|
| 254 |
const snap = await db.collection('rides').where('captainId', '==', uid).orderBy('createdAt', 'desc').get();
|
| 255 |
const rides = snap.docs.map(d => ({ id: d.id, ...d.data() }));
|
|
|
|
| 275 |
const { lat, lng } = req.body;
|
| 276 |
const uid = req.user ? req.user.uid : req.body.captainId;
|
| 277 |
if (lat === undefined || lng === undefined) return res.status(400).json({ success: false, error: 'Latitude and Longitude are required', code: 'MISSING_COORDINATES' });
|
| 278 |
+
|
| 279 |
try {
|
| 280 |
const rideRef = db.collection('rides').doc(rideId);
|
| 281 |
const rideDoc = await rideRef.get();
|