Files
Verde-Web/app/Http/Controllers/Api/V1/Driver/DriverLocationController.php

52 lines
1.9 KiB
PHP

<?php
namespace App\Http\Controllers\Api\V1\Driver;
use App\Http\Controllers\Api\V1\ApiController;
use App\Models\Trip;
use App\Models\Truck;
use App\Services\LiveTracking\TruckTracker;
use Carbon\Carbon;
use Illuminate\Http\JsonResponse;
use Illuminate\Http\Request;
class DriverLocationController extends ApiController
{
public function __construct(private readonly TruckTracker $tracker) {}
public function store(Request $request, Truck $truck): JsonResponse
{
$data = $request->validate([
'lat' => ['required', 'numeric', 'between:-90,90'],
'lng' => ['required', 'numeric', 'between:-180,180'],
'heading_degrees' => ['nullable', 'integer', 'min:0', 'max:359'],
'speed_kmh' => ['nullable', 'numeric', 'min:0', 'max:300'],
'trip_id' => ['nullable', 'string', 'exists:trips,uuid'],
'recorded_at' => ['nullable', 'date'],
]);
// The driver POSTing must be the team's driver for this truck
$team = $truck->fresh()->assignedTeam;
if ($team && $team->driver_id !== $request->user()->id) {
return $this->forbidden('You are not the assigned driver for this truck');
}
$trip = isset($data['trip_id']) ? Trip::where('uuid', $data['trip_id'])->first() : null;
$result = $this->tracker->record(
truck: $truck,
lat: (float) $data['lat'],
lng: (float) $data['lng'],
heading: isset($data['heading_degrees']) ? (int) $data['heading_degrees'] : null,
speedKmh: isset($data['speed_kmh']) ? (float) $data['speed_kmh'] : null,
trip: $trip,
recordedAt: isset($data['recorded_at']) ? Carbon::parse($data['recorded_at']) : null,
);
return $this->ok([
'recorded' => $result->recorded,
'geofence_triggered' => $result->geofenceTriggered,
]);
}
}