42 lines
1.5 KiB
PHP
42 lines
1.5 KiB
PHP
<?php
|
|
// transit/trip/end.php — السائق ينهي الرحلة
|
|
|
|
require_once __DIR__ . '/../../transit/connect_app.php';
|
|
|
|
if ($transit_user_role !== 'driver') jsonError('Only drivers can access this endpoint', 403);
|
|
|
|
$tripId = filterRequest('trip_id', 'int');
|
|
if (!$tripId) jsonError('trip_id is required', 400);
|
|
|
|
// استخرج سجل السائق من JWT
|
|
$driverRow = $transit_con->prepare(
|
|
"SELECT id FROM transit_drivers WHERE main_driver_id=? AND status='active' LIMIT 1"
|
|
);
|
|
$driverRow->execute([(string)$transit_user_id]);
|
|
$driverRec = $driverRow->fetch();
|
|
if (!$driverRec) jsonError('Driver not found or not active', 403);
|
|
$driverId = (int)$driverRec['id'];
|
|
|
|
$st = $transit_con->prepare(
|
|
"SELECT id, route_id, status FROM transit_trips WHERE id=? AND driver_id=? LIMIT 1"
|
|
);
|
|
$st->execute([$tripId, $driverId]);
|
|
$trip = $st->fetch();
|
|
|
|
if (!$trip) jsonError('Trip not found', 404);
|
|
if ($trip['status'] !== 'started') jsonError('Trip is not in started state', 409);
|
|
|
|
$transit_con->prepare(
|
|
"UPDATE transit_trips SET status='completed', completed_at=NOW(), updated_at=NOW() WHERE id=?"
|
|
)->execute([$tripId]);
|
|
|
|
global $redis;
|
|
if ($redis) $redis->del("transit:trip:{$tripId}:status");
|
|
|
|
// موقع الباص + ملكية الرحلة على Redis سيرفر الموقع
|
|
transitClearTripOwner($tripId);
|
|
global $redisLocation;
|
|
if ($redisLocation) $redisLocation->del("transit:trip:{$tripId}:pos");
|
|
|
|
jsonSuccess(['trip_id' => $tripId, 'status' => 'completed'], 'Trip completed');
|