Files
Verde-Web/app/Http/Controllers/Api/V1/Driver/DriverLocationController.php
admin 15d56d0e98 feat(backend): finish Module 13 sub-modules + flow fixes
Notifications: notification_preferences + Laravel notifications inbox.
SmsChannel adapter for our SmsService. RoutesByPreferences trait reads
per-user toggles. HouseholdApproved/Rejected, QrBalanceLow, and
CodesPurchased notifications wired in via auto-discovered listeners
or direct dispatch from controllers/StoreOperations.

Payments: payments table + PaymentDriver interface. ManualPaymentDriver
works out of the box; PayMongoDriver activates when
PAYMONGO_SECRET_KEY is set, falls back to manual otherwise. Resident
initiates code-purchase, admin can mark paid manually, webhook applies
real provider events. Fulfillment runs StoreOperations::sellToHousehold.

Live tracking (HTTP polling): truck_location_history (with SPATIAL
INDEX + 7-day retention plan). Driver POST /driver/trucks/{uuid}/location
writes history, updates trucks.last_known_coordinates, caches in Redis,
flags geofence-trigger when entering active trip dumpsite. Admin
GET /admin/live/trucks returns active truck positions. Reverb broadcast
deferred.

Flow corrections:
- QrAllocator now idempotent — re-approving a household no longer
  re-dispenses free codes.
- arrive-dumpsite enforces dumpsite geofence via ST_Contains; can be
  bypassed with override_geofence: true.

171 feature tests passing.

Co-Authored-By: Claude Opus 4.7 (1M context) <noreply@anthropic.com>
2026-05-01 14:01:38 +08:00

51 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 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\Carbon::parse($data['recorded_at']) : null,
);
return $this->ok([
'recorded' => $result->recorded,
'geofence_triggered' => $result->geofenceTriggered,
]);
}
}