@fleetbase/fleetops-engine 0.6.36 → 0.6.37
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
package/composer.json
CHANGED
package/extension.json
CHANGED
package/package.json
CHANGED
|
@@ -45,6 +45,17 @@ class DriverController extends FleetOpsController
|
|
|
45
45
|
{
|
|
46
46
|
$input = $request->input('driver');
|
|
47
47
|
|
|
48
|
+
// Normalize vehicle field - the frontend may send the full vehicle object,
|
|
49
|
+
// a UUID string, or a public_id string. Normalize to a single identifier
|
|
50
|
+
// so the ResolvableVehicle rule can validate it correctly.
|
|
51
|
+
if (isset($input['vehicle']) && is_array($input['vehicle'])) {
|
|
52
|
+
$input['vehicle'] = data_get($input['vehicle'], 'id')
|
|
53
|
+
?? data_get($input['vehicle'], 'public_id')
|
|
54
|
+
?? data_get($input['vehicle'], 'uuid')
|
|
55
|
+
?? null;
|
|
56
|
+
$request->merge(['driver' => $input]);
|
|
57
|
+
}
|
|
58
|
+
|
|
48
59
|
// create validation request
|
|
49
60
|
$createDriverRequest = CreateDriverRequest::createFrom($request);
|
|
50
61
|
$rules = $createDriverRequest->rules();
|
|
@@ -262,6 +273,17 @@ class DriverController extends FleetOpsController
|
|
|
262
273
|
// get input data
|
|
263
274
|
$input = $request->input('driver');
|
|
264
275
|
|
|
276
|
+
// Normalize vehicle field - the frontend may send the full vehicle object,
|
|
277
|
+
// a UUID string, or a public_id string. Normalize to a single identifier
|
|
278
|
+
// so the ResolvableVehicle rule can validate it correctly.
|
|
279
|
+
if (isset($input['vehicle']) && is_array($input['vehicle'])) {
|
|
280
|
+
$input['vehicle'] = data_get($input['vehicle'], 'id')
|
|
281
|
+
?? data_get($input['vehicle'], 'public_id')
|
|
282
|
+
?? data_get($input['vehicle'], 'uuid')
|
|
283
|
+
?? null;
|
|
284
|
+
$request->merge(['driver' => $input]);
|
|
285
|
+
}
|
|
286
|
+
|
|
265
287
|
// create validation request
|
|
266
288
|
$updateDriverRequest = UpdateDriverRequest::createFrom($request);
|
|
267
289
|
$rules = $updateDriverRequest->rules();
|
|
@@ -4,6 +4,7 @@ namespace Fleetbase\FleetOps\Http\Requests\Internal;
|
|
|
4
4
|
|
|
5
5
|
use Fleetbase\FleetOps\Http\Requests\CreateDriverRequest as CreateDriverApiRequest;
|
|
6
6
|
use Fleetbase\FleetOps\Rules\ResolvablePoint;
|
|
7
|
+
use Fleetbase\FleetOps\Rules\ResolvableVehicle;
|
|
7
8
|
use Fleetbase\Support\Auth;
|
|
8
9
|
use Illuminate\Validation\Rule;
|
|
9
10
|
|
|
@@ -49,7 +50,7 @@ class CreateDriverRequest extends CreateDriverApiRequest
|
|
|
49
50
|
'internal_id' => 'nullable|string|max:255',
|
|
50
51
|
'country' => 'nullable|string|size:2',
|
|
51
52
|
'city' => 'nullable|string|max:255',
|
|
52
|
-
'vehicle' => 'nullable
|
|
53
|
+
'vehicle' => ['nullable', new ResolvableVehicle()],
|
|
53
54
|
'status' => 'nullable|string|in:active,inactive',
|
|
54
55
|
'vendor' => 'nullable|exists:vendors,public_id',
|
|
55
56
|
'job' => 'nullable|exists:orders,public_id',
|
|
@@ -0,0 +1,90 @@
|
|
|
1
|
+
<?php
|
|
2
|
+
|
|
3
|
+
namespace Fleetbase\FleetOps\Rules;
|
|
4
|
+
|
|
5
|
+
use Fleetbase\FleetOps\Models\Vehicle;
|
|
6
|
+
use Illuminate\Contracts\Validation\Rule;
|
|
7
|
+
use Illuminate\Support\Str;
|
|
8
|
+
|
|
9
|
+
class ResolvableVehicle implements Rule
|
|
10
|
+
{
|
|
11
|
+
/**
|
|
12
|
+
* The resolved vehicle instance, if found.
|
|
13
|
+
*/
|
|
14
|
+
protected ?Vehicle $resolved = null;
|
|
15
|
+
|
|
16
|
+
/**
|
|
17
|
+
* Determine if the validation rule passes.
|
|
18
|
+
*
|
|
19
|
+
* Accepts:
|
|
20
|
+
* - A public_id string (e.g. "vehicle_abc123")
|
|
21
|
+
* - A UUID string (e.g. "550e8400-e29b-41d4-a716-446655440000")
|
|
22
|
+
* - An array/object containing an "id", "public_id", or "uuid" key
|
|
23
|
+
*
|
|
24
|
+
* @param string $attribute
|
|
25
|
+
*
|
|
26
|
+
* @return bool
|
|
27
|
+
*/
|
|
28
|
+
public function passes($attribute, $value)
|
|
29
|
+
{
|
|
30
|
+
$identifier = $this->extractIdentifier($value);
|
|
31
|
+
|
|
32
|
+
if (empty($identifier)) {
|
|
33
|
+
return true; // nullable — let the nullable rule handle empty values
|
|
34
|
+
}
|
|
35
|
+
|
|
36
|
+
if (Str::isUuid($identifier)) {
|
|
37
|
+
$this->resolved = Vehicle::where('uuid', $identifier)->first();
|
|
38
|
+
} else {
|
|
39
|
+
$this->resolved = Vehicle::where('public_id', $identifier)->first();
|
|
40
|
+
}
|
|
41
|
+
|
|
42
|
+
return $this->resolved !== null;
|
|
43
|
+
}
|
|
44
|
+
|
|
45
|
+
/**
|
|
46
|
+
* Get the validation error message.
|
|
47
|
+
*
|
|
48
|
+
* @return string
|
|
49
|
+
*/
|
|
50
|
+
public function message()
|
|
51
|
+
{
|
|
52
|
+
return 'The :attribute must be a valid vehicle public ID, UUID, or vehicle object.';
|
|
53
|
+
}
|
|
54
|
+
|
|
55
|
+
/**
|
|
56
|
+
* Extract a string identifier from the given value.
|
|
57
|
+
*
|
|
58
|
+
* Handles a plain string, an associative array, or a stdClass object.
|
|
59
|
+
*/
|
|
60
|
+
protected function extractIdentifier($value): ?string
|
|
61
|
+
{
|
|
62
|
+
if (is_string($value)) {
|
|
63
|
+
return $value;
|
|
64
|
+
}
|
|
65
|
+
|
|
66
|
+
if (is_array($value)) {
|
|
67
|
+
return data_get($value, 'id')
|
|
68
|
+
?? data_get($value, 'public_id')
|
|
69
|
+
?? data_get($value, 'uuid')
|
|
70
|
+
?? null;
|
|
71
|
+
}
|
|
72
|
+
|
|
73
|
+
if (is_object($value)) {
|
|
74
|
+
return data_get($value, 'id')
|
|
75
|
+
?? data_get($value, 'public_id')
|
|
76
|
+
?? data_get($value, 'uuid')
|
|
77
|
+
?? null;
|
|
78
|
+
}
|
|
79
|
+
|
|
80
|
+
return null;
|
|
81
|
+
}
|
|
82
|
+
|
|
83
|
+
/**
|
|
84
|
+
* Get the resolved Vehicle model instance after validation passes.
|
|
85
|
+
*/
|
|
86
|
+
public function getResolved(): ?Vehicle
|
|
87
|
+
{
|
|
88
|
+
return $this->resolved;
|
|
89
|
+
}
|
|
90
|
+
}
|