Huzaifa1435 commited on
Commit
300c8a2
·
verified ·
1 Parent(s): 284adac

Update src/controllers/customerRequestController.js

Browse files
src/controllers/customerRequestController.js CHANGED
@@ -413,6 +413,158 @@ const getMyCustomerRequests = async (req, res) => {
413
  }
414
  };
415
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
416
  const deleteCustomerRequest = async (req, res) => {
417
  const uid = req.user.uid;
418
  const { requestId } = req.params;
@@ -961,9 +1113,10 @@ module.exports = {
961
  createCustomerRequest,
962
  getOpenCustomerRequests,
963
  getMyCustomerRequests,
 
964
  deleteCustomerRequest,
965
  acceptCustomerFare,
966
  createOffer,
967
  respondOffer,
968
  updateOffer,
969
- };
 
413
  }
414
  };
415
 
416
+ const updateCustomerRequest = async (req, res) => {
417
+ const uid = req.user.uid;
418
+ const { requestId } = req.params;
419
+ const {
420
+ startLocation,
421
+ endLocation,
422
+ pickupLocation,
423
+ dropLocation,
424
+ vehicleType,
425
+ rideMode,
426
+ desiredFare,
427
+ requestedAt,
428
+ startLat,
429
+ startLng,
430
+ endLat,
431
+ endLng,
432
+ pickupLat,
433
+ pickupLng,
434
+ dropLat,
435
+ dropLng,
436
+ customerLat,
437
+ customerLng,
438
+ city,
439
+ } = req.body;
440
+
441
+ try {
442
+ const requestRef = db.collection('customerRideRequests').doc(requestId);
443
+ const requestDoc = await requestRef.get();
444
+ if (!requestDoc.exists) {
445
+ return res.status(404).json({ success: false, error: 'Request not found', code: 'REQUEST_NOT_FOUND' });
446
+ }
447
+
448
+ const request = requestDoc.data() || {};
449
+ if (request.customerId !== uid) {
450
+ return res.status(403).json({ success: false, error: 'Unauthorized', code: 'UNAUTHORIZED' });
451
+ }
452
+
453
+ const status = (request.status || '').toString().toLowerCase();
454
+ if (['accepted', 'completed'].includes(status)) {
455
+ return res.status(400).json({ success: false, error: 'Accepted or completed requests cannot be edited', code: 'REQUEST_LOCKED' });
456
+ }
457
+
458
+ if (
459
+ startLocation != null && exceedsMaxLength(startLocation, MAX_LOCATION) ||
460
+ endLocation != null && exceedsMaxLength(endLocation, MAX_LOCATION) ||
461
+ pickupLocation != null && exceedsMaxLength(pickupLocation, MAX_LOCATION) ||
462
+ dropLocation != null && exceedsMaxLength(dropLocation, MAX_LOCATION)
463
+ ) {
464
+ return res.status(400).json({ success: false, error: `Location fields must be at most ${MAX_LOCATION} characters`, code: 'FIELD_TOO_LONG' });
465
+ }
466
+
467
+ const nextStartLocation = startLocation != null ? sanitizeString(labelFromLocation(startLocation), MAX_LOCATION) : request.startLocation;
468
+ const nextEndLocation = endLocation != null ? sanitizeString(labelFromLocation(endLocation), MAX_LOCATION) : request.endLocation;
469
+ const nextPickupLocation = pickupLocation != null ? sanitizeString(labelFromLocation(pickupLocation), MAX_LOCATION) : request.pickupLocation;
470
+ const nextDropLocation = dropLocation != null ? sanitizeString(labelFromLocation(dropLocation), MAX_LOCATION) : request.dropLocation;
471
+
472
+ if (!nextStartLocation || !nextEndLocation) {
473
+ return res.status(400).json({ success: false, error: 'startLocation and endLocation are required', code: 'MISSING_FIELDS' });
474
+ }
475
+
476
+ let parsedStartLat = startLat != null ? parseNumber(startLat) : request.startLat;
477
+ let parsedStartLng = startLng != null ? parseNumber(startLng) : request.startLng;
478
+ let parsedEndLat = endLat != null ? parseNumber(endLat) : request.endLat;
479
+ let parsedEndLng = endLng != null ? parseNumber(endLng) : request.endLng;
480
+ const parsedCustomerLat = customerLat != null ? parseNumber(customerLat) : request.customerLat;
481
+ const parsedCustomerLng = customerLng != null ? parseNumber(customerLng) : request.customerLng;
482
+ const parsedRequestedAt = requestedAt ? new Date(requestedAt) : request.requestedAt ? new Date(request.requestedAt) : null;
483
+
484
+ if (
485
+ (!isValidLat(parsedStartLat) || !isValidLng(parsedStartLng) || isZeroCoordinate(parsedStartLat, parsedStartLng)) &&
486
+ isValidLat(parsedCustomerLat) &&
487
+ isValidLng(parsedCustomerLng) &&
488
+ !isZeroCoordinate(parsedCustomerLat, parsedCustomerLng)
489
+ ) {
490
+ parsedStartLat = parsedCustomerLat;
491
+ parsedStartLng = parsedCustomerLng;
492
+ }
493
+
494
+ if (
495
+ (!isValidLat(parsedEndLat) || !isValidLng(parsedEndLng) || isZeroCoordinate(parsedEndLat, parsedEndLng)) &&
496
+ isValidLat(parsedStartLat) &&
497
+ isValidLng(parsedStartLng)
498
+ ) {
499
+ parsedEndLat = parsedStartLat;
500
+ parsedEndLng = parsedStartLng;
501
+ }
502
+
503
+ if (!isValidLat(parsedStartLat) || !isValidLng(parsedStartLng) || !isValidLat(parsedEndLat) || !isValidLng(parsedEndLng)) {
504
+ return res.status(400).json({ success: false, error: 'Unable to resolve route location. Type a clearer area or use current location.', code: 'MAP_COORDINATES_REQUIRED' });
505
+ }
506
+
507
+ if (parsedRequestedAt && Number.isNaN(parsedRequestedAt.getTime())) {
508
+ return res.status(400).json({ success: false, error: 'requestedAt is invalid', code: 'INVALID_REQUESTED_AT' });
509
+ }
510
+ if (parsedRequestedAt && parsedRequestedAt <= new Date()) {
511
+ return res.status(400).json({ success: false, error: 'requestedAt must be in the future', code: 'PAST_REQUESTED_AT' });
512
+ }
513
+
514
+ const updatePayload = {
515
+ startLocation: nextStartLocation,
516
+ endLocation: nextEndLocation,
517
+ pickupLocation: nextPickupLocation || nextStartLocation,
518
+ dropLocation: nextDropLocation || nextEndLocation,
519
+ startLat: parsedStartLat,
520
+ startLng: parsedStartLng,
521
+ endLat: parsedEndLat,
522
+ endLng: parsedEndLng,
523
+ customerLat: parsedCustomerLat,
524
+ customerLng: parsedCustomerLng,
525
+ city: city != null ? String(city).trim() : request.city,
526
+ updatedAt: new Date().toISOString(),
527
+ };
528
+
529
+ if (vehicleType != null) {
530
+ const normalizedVehicleType = String(vehicleType).toLowerCase();
531
+ if (!['car', 'bike', 'bus', 'truck', 'shazore'].includes(normalizedVehicleType)) {
532
+ return res.status(400).json({ success: false, error: 'Invalid vehicleType', code: 'INVALID_VEHICLE_TYPE' });
533
+ }
534
+ updatePayload.vehicleType = normalizedVehicleType;
535
+ }
536
+
537
+ if (rideMode != null) {
538
+ const normalizedRideMode = String(rideMode).toLowerCase();
539
+ if (!['solo', 'share'].includes(normalizedRideMode)) {
540
+ return res.status(400).json({ success: false, error: 'rideMode must be solo or share', code: 'INVALID_RIDE_MODE' });
541
+ }
542
+ updatePayload.rideMode = normalizedRideMode;
543
+ }
544
+
545
+ if (desiredFare != null) {
546
+ const parsedDesiredFare = parseNumber(desiredFare);
547
+ if (!parsedDesiredFare || parsedDesiredFare <= 0) {
548
+ return res.status(400).json({ success: false, error: 'desiredFare must be greater than 0', code: 'INVALID_FARE' });
549
+ }
550
+ if (parsedDesiredFare < MIN_FARE) {
551
+ return res.status(400).json({ success: false, error: 'Minimum fare is Rs 50', code: 'FARE_TOO_LOW' });
552
+ }
553
+ updatePayload.desiredFare = parsedDesiredFare;
554
+ }
555
+
556
+ if (parsedRequestedAt) {
557
+ updatePayload.requestedAt = parsedRequestedAt.toISOString();
558
+ }
559
+
560
+ await requestRef.update(updatePayload);
561
+ const updatedDoc = await requestRef.get();
562
+ return res.json({ success: true, request: serializeCustomerRequest({ id: updatedDoc.id, ...updatedDoc.data() }) });
563
+ } catch (err) {
564
+ return res.status(500).json({ success: false, error: err.message, code: 'UPDATE_CUSTOMER_REQUEST_ERROR' });
565
+ }
566
+ };
567
+
568
  const deleteCustomerRequest = async (req, res) => {
569
  const uid = req.user.uid;
570
  const { requestId } = req.params;
 
1113
  createCustomerRequest,
1114
  getOpenCustomerRequests,
1115
  getMyCustomerRequests,
1116
+ updateCustomerRequest,
1117
  deleteCustomerRequest,
1118
  acceptCustomerFare,
1119
  createOffer,
1120
  respondOffer,
1121
  updateOffer,
1122
+ };