codeBOKER commited on
Commit
76bb2bd
·
1 Parent(s): b10f5a0

feat: prevent duplicate driver trips at same date and time

Browse files
Files changed (2) hide show
  1. app/database/supabase.py +18 -0
  2. app/tools/handlers.py +15 -0
app/database/supabase.py CHANGED
@@ -421,6 +421,24 @@ class SupabaseRepository:
421
  )
422
  return _response_data(response)
423
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
424
  async def list_driver_cars(self, driver_id: str) -> list[dict[str, Any]]:
425
  response = await (
426
  self.client.table("driver_cars")
 
421
  )
422
  return _response_data(response)
423
 
424
+ async def get_driver_trip_by_datetime(
425
+ self,
426
+ driver_id: str,
427
+ departure_date: date,
428
+ departure_time: str,
429
+ ) -> dict[str, Any] | None:
430
+ response = await (
431
+ self.client.table("driver_trips")
432
+ .select("id, departure, destination, departure_date, departure_time")
433
+ .eq("driver_id", driver_id)
434
+ .eq("status", "active")
435
+ .eq("departure_date", departure_date.isoformat())
436
+ .eq("departure_time", departure_time)
437
+ .maybe_single()
438
+ .execute()
439
+ )
440
+ return _response_data(response)
441
+
442
  async def list_driver_cars(self, driver_id: str) -> list[dict[str, Any]]:
443
  response = await (
444
  self.client.table("driver_cars")
app/tools/handlers.py CHANGED
@@ -396,6 +396,21 @@ class FalsaToolHandlers:
396
  error="departure_time must be morning, noon, night, or Arabic صباح / ظهر / ليل",
397
  )
398
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
399
  latest_trip = await self.repository.get_driver_latest_trip(str(driver["id"]))
400
  cars = await self.repository.list_driver_cars(str(driver["id"]))
401
 
 
396
  error="departure_time must be morning, noon, night, or Arabic صباح / ظهر / ليل",
397
  )
398
 
399
+ existing = await self.repository.get_driver_trip_by_datetime(
400
+ driver_id=str(driver["id"]),
401
+ departure_date=parsed_date,
402
+ departure_time=departure_time,
403
+ )
404
+ if existing:
405
+ return ToolResult(
406
+ ok=False,
407
+ data={"existing_trip_id": existing.get("id")},
408
+ error=(
409
+ "You already have a trip scheduled at this date and time. "
410
+ "Cancel or modify the existing trip before creating a new one."
411
+ ),
412
+ )
413
+
414
  latest_trip = await self.repository.get_driver_latest_trip(str(driver["id"]))
415
  cars = await self.repository.list_driver_cars(str(driver["id"]))
416