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, ]); } }