127 lines
4.9 KiB
PHP
127 lines
4.9 KiB
PHP
<?php
|
|
|
|
namespace App\Http\Controllers\Api\V1\Scanner;
|
|
|
|
use App\Http\Controllers\Api\V1\ApiController;
|
|
use App\Models\DropOffPoint;
|
|
use App\Models\Trip;
|
|
use App\Models\TripStop;
|
|
use App\Services\Scan\ScanService;
|
|
use Carbon\Carbon;
|
|
use Illuminate\Http\JsonResponse;
|
|
use Illuminate\Http\Request;
|
|
|
|
class ScannerController extends ApiController
|
|
{
|
|
public function __construct(private readonly ScanService $scanner) {}
|
|
|
|
public function scan(Request $request): JsonResponse
|
|
{
|
|
$data = $request->validate([
|
|
'serial' => ['required', 'string', 'max:64'],
|
|
'drop_off_point_id' => ['required', 'integer', 'exists:drop_off_points,id'],
|
|
'lat' => ['required', 'numeric', 'between:-90,90'],
|
|
'lng' => ['required', 'numeric', 'between:-180,180'],
|
|
'trip_id' => ['nullable', 'string', 'exists:trips,uuid'],
|
|
'trip_stop_id' => ['nullable', 'integer', 'exists:trip_stops,id'],
|
|
'weight_kg' => ['nullable', 'integer', 'min:0'],
|
|
'waste_type' => ['nullable', 'string', 'max:50'],
|
|
'photo_path' => ['nullable', 'string', 'max:255'],
|
|
'notes' => ['nullable', 'string', 'max:500'],
|
|
'scanned_at' => ['nullable', 'date'],
|
|
]);
|
|
|
|
$dop = DropOffPoint::findOrFail($data['drop_off_point_id']);
|
|
$trip = isset($data['trip_id']) ? Trip::where('uuid', $data['trip_id'])->first() : null;
|
|
$stop = isset($data['trip_stop_id']) ? TripStop::find($data['trip_stop_id']) : null;
|
|
|
|
$result = $this->scanner->scan(
|
|
serial: $data['serial'],
|
|
dropOff: $dop,
|
|
scanner: $request->user(),
|
|
lat: (float) $data['lat'],
|
|
lng: (float) $data['lng'],
|
|
trip: $trip,
|
|
tripStop: $stop,
|
|
weightKg: $data['weight_kg'] ?? null,
|
|
wasteType: $data['waste_type'] ?? null,
|
|
photoPath: $data['photo_path'] ?? null,
|
|
notes: $data['notes'] ?? null,
|
|
scannedAt: isset($data['scanned_at']) ? Carbon::parse($data['scanned_at']) : null,
|
|
);
|
|
|
|
if (! $result->accepted) {
|
|
return $this->fail(
|
|
'Scan rejected',
|
|
['reason' => [$result->rejectReason]],
|
|
422,
|
|
);
|
|
}
|
|
|
|
return $this->ok([
|
|
'serial' => $result->code->serial,
|
|
'log_id' => $result->log->id,
|
|
'status' => (string) $result->code->status,
|
|
], 'Scan recorded');
|
|
}
|
|
|
|
/**
|
|
* Bulk scan endpoint for offline sync. Each scan is processed
|
|
* independently; failures don't abort the batch.
|
|
*/
|
|
public function bulk(Request $request): JsonResponse
|
|
{
|
|
$data = $request->validate([
|
|
'scans' => ['required', 'array', 'min:1', 'max:200'],
|
|
'scans.*.serial' => ['required', 'string'],
|
|
'scans.*.drop_off_point_id' => ['required', 'integer', 'exists:drop_off_points,id'],
|
|
'scans.*.lat' => ['required', 'numeric'],
|
|
'scans.*.lng' => ['required', 'numeric'],
|
|
'scans.*.trip_id' => ['nullable', 'string'],
|
|
'scans.*.trip_stop_id' => ['nullable', 'integer'],
|
|
'scans.*.scanned_at' => ['nullable', 'date'],
|
|
]);
|
|
|
|
$results = [];
|
|
foreach ($data['scans'] as $i => $s) {
|
|
try {
|
|
$dop = DropOffPoint::find($s['drop_off_point_id']);
|
|
if (! $dop) {
|
|
$results[] = ['index' => $i, 'serial' => $s['serial'], 'accepted' => false, 'reason' => 'dop_not_found'];
|
|
|
|
continue;
|
|
}
|
|
$trip = isset($s['trip_id']) ? Trip::where('uuid', $s['trip_id'])->first() : null;
|
|
$stop = isset($s['trip_stop_id']) ? TripStop::find($s['trip_stop_id']) : null;
|
|
|
|
$r = $this->scanner->scan(
|
|
serial: $s['serial'],
|
|
dropOff: $dop,
|
|
scanner: $request->user(),
|
|
lat: (float) $s['lat'],
|
|
lng: (float) $s['lng'],
|
|
trip: $trip,
|
|
tripStop: $stop,
|
|
scannedAt: isset($s['scanned_at']) ? Carbon::parse($s['scanned_at']) : null,
|
|
);
|
|
$results[] = [
|
|
'index' => $i,
|
|
'serial' => $s['serial'],
|
|
'accepted' => $r->accepted,
|
|
'reason' => $r->rejectReason,
|
|
];
|
|
} catch (\Throwable $e) {
|
|
$results[] = ['index' => $i, 'serial' => $s['serial'], 'accepted' => false, 'reason' => 'exception'];
|
|
}
|
|
}
|
|
|
|
$accepted = collect($results)->where('accepted', true)->count();
|
|
|
|
return $this->ok([
|
|
'results' => $results,
|
|
'accepted_count' => $accepted,
|
|
'rejected_count' => count($results) - $accepted,
|
|
], "Processed {$accepted} of ".count($results).' scans');
|
|
}
|
|
}
|