Skip to content

Commit 27d5b5f

Browse files
committed
feat(backend): add logging and improve trip stop reordering logic
- Introduced logging to track the reordering of trip stops for better debugging and monitoring. - Updated the reordering logic to temporarily set high order values to avoid constraint conflicts during the update process. - Enhanced error handling to ensure proper validation of trip stops belonging to the specified trip.
1 parent 78db595 commit 27d5b5f

1 file changed

Lines changed: 12 additions & 1 deletion

File tree

backend/trips/views.py

Lines changed: 12 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -543,10 +543,14 @@ def post(self, request, trip_pk):
543543
"""Reorder trip stops for a given trip"""
544544
try:
545545
from django.db import transaction
546+
import logging
547+
logger = logging.getLogger(__name__)
546548

547549
data = json.loads(request.body)
548550
new_orders = data.get('orders', []) # Expected format: [{'id': stop_id, 'order': new_order}, ...]
549551

552+
logger.info(f"Reordering stops for trip {trip_pk}: {new_orders}")
553+
550554
if not new_orders:
551555
return JsonResponse({'error': 'No orders provided'}, status=400)
552556

@@ -567,7 +571,14 @@ def post(self, request, trip_pk):
567571
if len(existing_stops) != len(provided_stop_ids):
568572
return JsonResponse({'error': 'Some trip stops do not belong to this trip'}, status=400)
569573

570-
# Update the orders
574+
# First, set all orders to very high values to avoid constraint conflicts
575+
# Use values starting from 10000 to avoid conflicts with existing orders
576+
for i, order_item in enumerate(new_orders):
577+
trip_stop = TripStop.objects.get(id=order_item['id'], trip=trip)
578+
trip_stop.order = 10000 + i # Use high values temporarily
579+
trip_stop.save()
580+
581+
# Then set the final order values
571582
for order_item in new_orders:
572583
trip_stop = TripStop.objects.get(id=order_item['id'], trip=trip)
573584
trip_stop.order = order_item['order']

0 commit comments

Comments
 (0)