@fleetbase/fleetops-engine 0.6.55 → 0.6.56

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.
Files changed (109) hide show
  1. package/addon/components/admin/map-settings.hbs +1 -1
  2. package/addon/components/device/details.js +1 -1
  3. package/addon/components/device-event/details.hbs +192 -0
  4. package/addon/components/device-event/details.js +151 -0
  5. package/addon/components/driver/schedule.js +1 -1
  6. package/addon/components/layout/fleet-ops-sidebar.js +15 -9
  7. package/addon/components/telematic/details.hbs +1 -1
  8. package/addon/components/telematic/form.hbs +2 -2
  9. package/addon/components/telematic/settings.hbs +1 -1
  10. package/addon/controllers/connectivity/devices/index/details/events.js +1 -1
  11. package/addon/controllers/connectivity/devices/index/details/vehicle.js +2 -2
  12. package/addon/controllers/connectivity/events/details.js +68 -1
  13. package/addon/controllers/connectivity/sensors/index.js +4 -0
  14. package/addon/controllers/connectivity/telematics/details/events.js +1 -1
  15. package/addon/controllers/connectivity/telematics/details/sensors.js +4 -0
  16. package/addon/controllers/settings/map.js +15 -0
  17. package/addon/instance-initializers/register-vroom-allocation.js +1 -1
  18. package/addon/routes/connectivity/events/details.js +26 -1
  19. package/addon/routes/management/vehicles/index/details.js +3 -1
  20. package/addon/services/map-adapter/google.js +25 -0
  21. package/addon/services/map-adapter/leaflet.js +1 -1
  22. package/addon/services/map-adapter-interface.js +1 -1
  23. package/addon/services/map-manager.js +4 -1
  24. package/addon/services/map-settings.js +15 -0
  25. package/addon/styles/fleetops-engine.css +1 -1
  26. package/addon/templates/connectivity/devices/index/details/vehicle.hbs +2 -1
  27. package/addon/templates/connectivity/events/details.hbs +40 -1
  28. package/addon/templates/settings/map.hbs +38 -9
  29. package/addon/utils/leaflet-plugin-loader.js +5 -5
  30. package/addon/utils/route-colors.js +1 -1
  31. package/app/components/device-event/details.hbs +192 -0
  32. package/app/components/device-event/details.js +1 -0
  33. package/composer.json +1 -1
  34. package/extension.json +1 -1
  35. package/package.json +2 -2
  36. package/server/migrations/2026_06_23_000001_delete_bad_afaqy_telematics_sensors.php +34 -0
  37. package/server/src/Exports/ContactExport.php +12 -5
  38. package/server/src/Exports/DeviceExport.php +92 -0
  39. package/server/src/Exports/DriverExport.php +28 -3
  40. package/server/src/Exports/EquipmentExport.php +94 -0
  41. package/server/src/Exports/FleetExport.php +22 -7
  42. package/server/src/Exports/FuelReportExport.php +16 -1
  43. package/server/src/Exports/IssueExport.php +25 -3
  44. package/server/src/Exports/MaintenanceExport.php +106 -0
  45. package/server/src/Exports/MaintenanceScheduleExport.php +99 -0
  46. package/server/src/Exports/OrderExport.php +24 -5
  47. package/server/src/Exports/PartExport.php +99 -0
  48. package/server/src/Exports/PlaceExport.php +33 -7
  49. package/server/src/Exports/SensorExport.php +94 -0
  50. package/server/src/Exports/ServiceAreaExport.php +31 -7
  51. package/server/src/Exports/ServiceRateExport.php +41 -1
  52. package/server/src/Exports/TelematicExport.php +90 -0
  53. package/server/src/Exports/VehicleExport.php +274 -8
  54. package/server/src/Exports/VendorExport.php +15 -2
  55. package/server/src/Exports/WorkOrderExport.php +92 -0
  56. package/server/src/Http/Controllers/Api/v1/Concerns/ResolvesFleetOpsApiResources.php +176 -0
  57. package/server/src/Http/Controllers/Api/v1/DeviceController.php +225 -0
  58. package/server/src/Http/Controllers/Api/v1/EquipmentController.php +114 -0
  59. package/server/src/Http/Controllers/Api/v1/FuelTransactionController.php +185 -0
  60. package/server/src/Http/Controllers/Api/v1/PartController.php +120 -0
  61. package/server/src/Http/Controllers/Api/v1/SensorController.php +132 -0
  62. package/server/src/Http/Controllers/Api/v1/WorkOrderController.php +161 -0
  63. package/server/src/Http/Controllers/Internal/v1/DeviceController.php +18 -0
  64. package/server/src/Http/Controllers/Internal/v1/EquipmentController.php +17 -0
  65. package/server/src/Http/Controllers/Internal/v1/MaintenanceController.php +17 -0
  66. package/server/src/Http/Controllers/Internal/v1/MaintenanceScheduleController.php +17 -0
  67. package/server/src/Http/Controllers/Internal/v1/PartController.php +17 -0
  68. package/server/src/Http/Controllers/Internal/v1/SensorController.php +18 -0
  69. package/server/src/Http/Controllers/Internal/v1/SettingController.php +29 -5
  70. package/server/src/Http/Controllers/Internal/v1/TelematicController.php +18 -0
  71. package/server/src/Http/Controllers/Internal/v1/WorkOrderController.php +17 -0
  72. package/server/src/Http/Filter/DeviceFilter.php +33 -10
  73. package/server/src/Http/Filter/EquipmentFilter.php +23 -0
  74. package/server/src/Http/Filter/FuelProviderTransactionFilter.php +57 -2
  75. package/server/src/Http/Filter/PartFilter.php +47 -0
  76. package/server/src/Http/Filter/SensorFilter.php +34 -6
  77. package/server/src/Http/Filter/WorkOrderFilter.php +23 -0
  78. package/server/src/Http/Requests/CreateDeviceRequest.php +51 -0
  79. package/server/src/Http/Requests/CreateEquipmentRequest.php +35 -0
  80. package/server/src/Http/Requests/CreateFuelTransactionRequest.php +50 -0
  81. package/server/src/Http/Requests/CreatePartRequest.php +40 -0
  82. package/server/src/Http/Requests/CreateSensorRequest.php +47 -0
  83. package/server/src/Http/Requests/CreateWorkOrderRequest.php +42 -0
  84. package/server/src/Http/Requests/UpdateDeviceRequest.php +7 -0
  85. package/server/src/Http/Requests/UpdateEquipmentRequest.php +7 -0
  86. package/server/src/Http/Requests/UpdateFuelTransactionRequest.php +7 -0
  87. package/server/src/Http/Requests/UpdatePartRequest.php +7 -0
  88. package/server/src/Http/Requests/UpdateSensorRequest.php +7 -0
  89. package/server/src/Http/Requests/UpdateWorkOrderRequest.php +7 -0
  90. package/server/src/Http/Resources/v1/Device.php +72 -0
  91. package/server/src/Http/Resources/v1/Equipment.php +57 -0
  92. package/server/src/Http/Resources/v1/FuelTransaction.php +69 -0
  93. package/server/src/Http/Resources/v1/Part.php +64 -0
  94. package/server/src/Http/Resources/v1/Sensor.php +68 -0
  95. package/server/src/Jobs/SyncTelematicDevicesJob.php +194 -38
  96. package/server/src/Models/Order.php +2 -1
  97. package/server/src/Observers/PurchaseRateObserver.php +2 -1
  98. package/server/src/Support/Analytics/RevenueTrend.php +3 -8
  99. package/server/src/Support/Metrics/ActiveRevenueQuery.php +121 -0
  100. package/server/src/Support/Metrics/AvgOrderValueMetric.php +1 -7
  101. package/server/src/Support/Metrics/EarningsMetric.php +1 -10
  102. package/server/src/Support/Telematics/Providers/AfaqyProvider.php +104 -5
  103. package/server/src/Support/Telematics/Providers/SafeeProvider.php +550 -54
  104. package/server/src/Support/Telematics/TelematicService.php +28 -9
  105. package/server/src/routes.php +67 -14
  106. package/server/tests/ExportCoverageTest.php +79 -0
  107. package/server/tests/MetricsRegistryTest.php +45 -5
  108. package/server/tests/PublicFleetOpsRestEndpointsTest.php +191 -0
  109. package/server/tests/TelematicsHardeningTest.php +636 -1
@@ -0,0 +1,72 @@
1
+ <?php
2
+
3
+ namespace Fleetbase\FleetOps\Http\Resources\v1;
4
+
5
+ use Fleetbase\FleetOps\Support\Utils;
6
+ use Fleetbase\Http\Resources\FleetbaseResource;
7
+ use Fleetbase\Support\Http;
8
+ use Fleetbase\Support\Resolve;
9
+
10
+ class Device extends FleetbaseResource
11
+ {
12
+ public function toArray($request)
13
+ {
14
+ return $this->withCustomFields([
15
+ 'id' => $this->when(Http::isInternalRequest(), $this->id, $this->public_id),
16
+ 'uuid' => $this->when(Http::isInternalRequest(), $this->uuid),
17
+ 'public_id' => $this->when(Http::isInternalRequest(), $this->public_id),
18
+ 'company_uuid' => $this->when(Http::isInternalRequest(), $this->company_uuid),
19
+ 'telematic_uuid' => $this->when(Http::isInternalRequest(), $this->telematic_uuid),
20
+ 'attachable_uuid' => $this->when(Http::isInternalRequest(), $this->attachable_uuid),
21
+ 'attachable_type' => $this->when(Http::isInternalRequest(), $this->attachable_type ? Utils::toEmberResourceType($this->attachable_type) : null),
22
+ 'warranty_uuid' => $this->when(Http::isInternalRequest(), $this->warranty_uuid),
23
+ 'photo_uuid' => $this->when(Http::isInternalRequest(), $this->photo_uuid),
24
+ 'telematic' => $this->whenLoaded('telematic', fn () => $this->resolveLoadedRelation($this->telematic)),
25
+ 'attachable' => $this->whenLoaded('attachable', fn () => $this->resolveLoadedRelation($this->attachable)),
26
+ 'warranty' => $this->whenLoaded('warranty', fn () => $this->resolveLoadedRelation($this->warranty)),
27
+ 'photo' => $this->whenLoaded('photo', fn () => $this->resolveLoadedRelation($this->photo)),
28
+ 'type' => $this->type,
29
+ 'device_id' => $this->device_id,
30
+ 'internal_id' => $this->internal_id,
31
+ 'imei' => $this->imei,
32
+ 'imsi' => $this->imsi,
33
+ 'firmware_version' => $this->firmware_version,
34
+ 'provider' => $this->provider,
35
+ 'name' => $this->name,
36
+ 'model' => $this->model,
37
+ 'location' => $this->location,
38
+ 'manufacturer' => $this->manufacturer,
39
+ 'serial_number' => $this->serial_number,
40
+ 'last_position' => $this->last_position,
41
+ 'installation_date' => $this->installation_date,
42
+ 'last_maintenance_date' => $this->last_maintenance_date,
43
+ 'meta' => data_get($this, 'meta', Utils::createObject()),
44
+ 'data' => data_get($this, 'data', Utils::createObject()),
45
+ 'options' => data_get($this, 'options', Utils::createObject()),
46
+ 'online' => $this->online,
47
+ 'status' => $this->status,
48
+ 'data_frequency' => $this->data_frequency,
49
+ 'notes' => $this->notes,
50
+ 'last_online_at' => $this->last_online_at,
51
+ 'warranty_name' => $this->warranty_name,
52
+ 'telematic_name' => $this->telematic_name,
53
+ 'is_online' => $this->is_online,
54
+ 'attached_to_name' => $this->attached_to_name,
55
+ 'connection_status' => $this->connection_status,
56
+ 'photo_url' => $this->photo_url,
57
+ 'sensors_count' => $this->when(isset($this->sensors_count), $this->sensors_count),
58
+ 'slug' => $this->slug,
59
+ 'updated_at' => $this->updated_at,
60
+ 'created_at' => $this->created_at,
61
+ ]);
62
+ }
63
+
64
+ protected function resolveLoadedRelation($model)
65
+ {
66
+ if (!$model) {
67
+ return null;
68
+ }
69
+
70
+ return Http::isInternalRequest() ? Resolve::httpResourceForModel($model) : $model->public_id;
71
+ }
72
+ }
@@ -0,0 +1,57 @@
1
+ <?php
2
+
3
+ namespace Fleetbase\FleetOps\Http\Resources\v1;
4
+
5
+ use Fleetbase\FleetOps\Support\Utils;
6
+ use Fleetbase\Http\Resources\FleetbaseResource;
7
+ use Fleetbase\Support\Http;
8
+ use Fleetbase\Support\Resolve;
9
+
10
+ class Equipment extends FleetbaseResource
11
+ {
12
+ public function toArray($request)
13
+ {
14
+ return $this->withCustomFields([
15
+ 'id' => $this->when(Http::isInternalRequest(), $this->id, $this->public_id),
16
+ 'uuid' => $this->when(Http::isInternalRequest(), $this->uuid),
17
+ 'public_id' => $this->when(Http::isInternalRequest(), $this->public_id),
18
+ 'company_uuid' => $this->when(Http::isInternalRequest(), $this->company_uuid),
19
+ 'warranty_uuid' => $this->when(Http::isInternalRequest(), $this->warranty_uuid),
20
+ 'photo_uuid' => $this->when(Http::isInternalRequest(), $this->photo_uuid),
21
+ 'equipable_uuid' => $this->when(Http::isInternalRequest(), $this->equipable_uuid),
22
+ 'equipable_type' => $this->when(Http::isInternalRequest(), $this->equipable_type ? Utils::toEmberResourceType($this->equipable_type) : null),
23
+ 'warranty' => $this->whenLoaded('warranty', fn () => $this->resolveLoadedRelation($this->warranty)),
24
+ 'photo' => $this->whenLoaded('photo', fn () => $this->resolveLoadedRelation($this->photo)),
25
+ 'equipable' => $this->whenLoaded('equipable', fn () => $this->resolveLoadedRelation($this->equipable)),
26
+ 'name' => $this->name,
27
+ 'code' => $this->code,
28
+ 'type' => $this->type,
29
+ 'status' => $this->status,
30
+ 'serial_number' => $this->serial_number,
31
+ 'manufacturer' => $this->manufacturer,
32
+ 'model' => $this->model,
33
+ 'purchased_at' => $this->purchased_at,
34
+ 'purchase_price' => $this->purchase_price,
35
+ 'currency' => $this->currency,
36
+ 'warranty_name' => $this->warranty_name,
37
+ 'photo_url' => $this->photo_url,
38
+ 'equipped_to_name' => $this->equipped_to_name,
39
+ 'is_equipped' => $this->is_equipped,
40
+ 'age_in_days' => $this->age_in_days,
41
+ 'depreciated_value' => $this->depreciated_value,
42
+ 'meta' => data_get($this, 'meta', Utils::createObject()),
43
+ 'slug' => $this->slug,
44
+ 'updated_at' => $this->updated_at,
45
+ 'created_at' => $this->created_at,
46
+ ]);
47
+ }
48
+
49
+ protected function resolveLoadedRelation($model)
50
+ {
51
+ if (!$model) {
52
+ return null;
53
+ }
54
+
55
+ return Http::isInternalRequest() ? Resolve::httpResourceForModel($model) : $model->public_id;
56
+ }
57
+ }
@@ -0,0 +1,69 @@
1
+ <?php
2
+
3
+ namespace Fleetbase\FleetOps\Http\Resources\v1;
4
+
5
+ use Fleetbase\Http\Resources\FleetbaseResource;
6
+ use Fleetbase\Support\Http;
7
+ use Fleetbase\Support\Resolve;
8
+
9
+ class FuelTransaction extends FleetbaseResource
10
+ {
11
+ public function toArray($request)
12
+ {
13
+ return [
14
+ 'id' => $this->when(Http::isInternalRequest(), $this->id, $this->public_id),
15
+ 'uuid' => $this->when(Http::isInternalRequest(), $this->uuid),
16
+ 'public_id' => $this->when(Http::isInternalRequest(), $this->public_id),
17
+ 'fuel_provider_connection_uuid' => $this->when(Http::isInternalRequest(), $this->fuel_provider_connection_uuid),
18
+ 'fuel_report_uuid' => $this->when(Http::isInternalRequest(), $this->fuel_report_uuid),
19
+ 'vehicle_uuid' => $this->when(Http::isInternalRequest(), $this->vehicle_uuid),
20
+ 'driver_uuid' => $this->when(Http::isInternalRequest(), $this->driver_uuid),
21
+ 'order_uuid' => $this->when(Http::isInternalRequest(), $this->order_uuid),
22
+ 'connection' => $this->whenLoaded('connection', fn () => $this->resolveLoadedRelation($this->connection)),
23
+ 'fuel_report' => $this->whenLoaded('fuelReport', fn () => $this->resolveLoadedRelation($this->fuelReport)),
24
+ 'vehicle' => $this->whenLoaded('vehicle', fn () => $this->resolveLoadedRelation($this->vehicle)),
25
+ 'driver' => $this->whenLoaded('driver', fn () => $this->resolveLoadedRelation($this->driver)),
26
+ 'order' => $this->whenLoaded('order', fn () => $this->resolveLoadedRelation($this->order)),
27
+ 'provider' => $this->provider,
28
+ 'provider_transaction_id' => $this->provider_transaction_id,
29
+ 'provider_vehicle_id' => $this->provider_vehicle_id,
30
+ 'vehicle_card_id' => $this->vehicle_card_id,
31
+ 'internal_number' => $this->internal_number,
32
+ 'structure_number' => $this->structure_number,
33
+ 'plate_number' => $this->plate_number,
34
+ 'vin' => $this->vin,
35
+ 'serial_number' => $this->serial_number,
36
+ 'call_sign' => $this->call_sign,
37
+ 'trip_number' => $this->trip_number,
38
+ 'station_name' => $this->station_name,
39
+ 'station_latitude' => $this->station_latitude,
40
+ 'station_longitude' => $this->station_longitude,
41
+ 'station_location' => $this->station_location,
42
+ 'transaction_at' => $this->transaction_at,
43
+ 'volume' => $this->volume,
44
+ 'metric_unit' => $this->metric_unit,
45
+ 'amount' => $this->amount,
46
+ 'currency' => $this->currency,
47
+ 'odometer' => $this->odometer,
48
+ 'sync_status' => $this->sync_status,
49
+ 'matched_at' => $this->matched_at,
50
+ 'vehicle_name' => $this->vehicle_name,
51
+ 'driver_name' => $this->driver_name,
52
+ 'fuel_report_id' => $this->fuel_report_id,
53
+ 'normalized_payload' => $this->when(Http::isInternalRequest(), $this->normalized_payload),
54
+ 'raw_payload' => $this->when(Http::isInternalRequest(), $this->raw_payload),
55
+ 'meta' => $this->when(Http::isInternalRequest(), $this->meta),
56
+ 'updated_at' => $this->updated_at,
57
+ 'created_at' => $this->created_at,
58
+ ];
59
+ }
60
+
61
+ protected function resolveLoadedRelation($model)
62
+ {
63
+ if (!$model) {
64
+ return null;
65
+ }
66
+
67
+ return Http::isInternalRequest() ? Resolve::httpResourceForModel($model) : $model->public_id;
68
+ }
69
+ }
@@ -0,0 +1,64 @@
1
+ <?php
2
+
3
+ namespace Fleetbase\FleetOps\Http\Resources\v1;
4
+
5
+ use Fleetbase\FleetOps\Support\Utils;
6
+ use Fleetbase\Http\Resources\FleetbaseResource;
7
+ use Fleetbase\Support\Http;
8
+ use Fleetbase\Support\Resolve;
9
+
10
+ class Part extends FleetbaseResource
11
+ {
12
+ public function toArray($request)
13
+ {
14
+ return [
15
+ 'id' => $this->when(Http::isInternalRequest(), $this->id, $this->public_id),
16
+ 'uuid' => $this->when(Http::isInternalRequest(), $this->uuid),
17
+ 'public_id' => $this->when(Http::isInternalRequest(), $this->public_id),
18
+ 'company_uuid' => $this->when(Http::isInternalRequest(), $this->company_uuid),
19
+ 'vendor_uuid' => $this->when(Http::isInternalRequest(), $this->vendor_uuid),
20
+ 'warranty_uuid' => $this->when(Http::isInternalRequest(), $this->warranty_uuid),
21
+ 'photo_uuid' => $this->when(Http::isInternalRequest(), $this->photo_uuid),
22
+ 'asset_uuid' => $this->when(Http::isInternalRequest(), $this->asset_uuid),
23
+ 'asset_type' => $this->when(Http::isInternalRequest(), $this->asset_type ? Utils::toEmberResourceType($this->asset_type) : null),
24
+ 'vendor' => $this->whenLoaded('vendor', fn () => $this->resolveLoadedRelation($this->vendor)),
25
+ 'warranty' => $this->whenLoaded('warranty', fn () => $this->resolveLoadedRelation($this->warranty)),
26
+ 'photo' => $this->whenLoaded('photo', fn () => $this->resolveLoadedRelation($this->photo)),
27
+ 'asset' => $this->whenLoaded('asset', fn () => $this->resolveLoadedRelation($this->asset)),
28
+ 'sku' => $this->sku,
29
+ 'name' => $this->name,
30
+ 'manufacturer' => $this->manufacturer,
31
+ 'model' => $this->model,
32
+ 'serial_number' => $this->serial_number,
33
+ 'barcode' => $this->barcode,
34
+ 'description' => $this->description,
35
+ 'quantity_on_hand' => $this->quantity_on_hand,
36
+ 'unit_cost' => $this->unit_cost,
37
+ 'msrp' => $this->msrp,
38
+ 'currency' => $this->currency,
39
+ 'type' => $this->type,
40
+ 'status' => $this->status,
41
+ 'specs' => data_get($this, 'specs', Utils::createObject()),
42
+ 'meta' => data_get($this, 'meta', Utils::createObject()),
43
+ 'vendor_name' => $this->vendor_name,
44
+ 'warranty_name' => $this->warranty_name,
45
+ 'photo_url' => $this->photo_url,
46
+ 'total_value' => $this->total_value,
47
+ 'is_in_stock' => $this->is_in_stock,
48
+ 'is_low_stock' => $this->is_low_stock,
49
+ 'asset_name' => $this->asset_name,
50
+ 'slug' => $this->slug,
51
+ 'updated_at' => $this->updated_at,
52
+ 'created_at' => $this->created_at,
53
+ ];
54
+ }
55
+
56
+ protected function resolveLoadedRelation($model)
57
+ {
58
+ if (!$model) {
59
+ return null;
60
+ }
61
+
62
+ return Http::isInternalRequest() ? Resolve::httpResourceForModel($model) : $model->public_id;
63
+ }
64
+ }
@@ -0,0 +1,68 @@
1
+ <?php
2
+
3
+ namespace Fleetbase\FleetOps\Http\Resources\v1;
4
+
5
+ use Fleetbase\FleetOps\Support\Utils;
6
+ use Fleetbase\Http\Resources\FleetbaseResource;
7
+ use Fleetbase\Support\Http;
8
+ use Fleetbase\Support\Resolve;
9
+
10
+ class Sensor extends FleetbaseResource
11
+ {
12
+ public function toArray($request)
13
+ {
14
+ return $this->withCustomFields([
15
+ 'id' => $this->when(Http::isInternalRequest(), $this->id, $this->public_id),
16
+ 'uuid' => $this->when(Http::isInternalRequest(), $this->uuid),
17
+ 'public_id' => $this->when(Http::isInternalRequest(), $this->public_id),
18
+ 'company_uuid' => $this->when(Http::isInternalRequest(), $this->company_uuid),
19
+ 'device_uuid' => $this->when(Http::isInternalRequest(), $this->device_uuid),
20
+ 'warranty_uuid' => $this->when(Http::isInternalRequest(), $this->warranty_uuid),
21
+ 'telematic_uuid' => $this->when(Http::isInternalRequest(), $this->telematic_uuid),
22
+ 'photo_uuid' => $this->when(Http::isInternalRequest(), $this->photo_uuid),
23
+ 'sensorable_uuid' => $this->when(Http::isInternalRequest(), $this->sensorable_uuid),
24
+ 'sensorable_type' => $this->when(Http::isInternalRequest(), $this->sensorable_type ? Utils::toEmberResourceType($this->sensorable_type) : null),
25
+ 'device' => $this->whenLoaded('device', fn () => $this->resolveLoadedRelation($this->device)),
26
+ 'warranty' => $this->whenLoaded('warranty', fn () => $this->resolveLoadedRelation($this->warranty)),
27
+ 'telematic' => $this->whenLoaded('telematic', fn () => $this->resolveLoadedRelation($this->telematic)),
28
+ 'photo' => $this->whenLoaded('photo', fn () => $this->resolveLoadedRelation($this->photo)),
29
+ 'sensorable' => $this->whenLoaded('sensorable', fn () => $this->resolveLoadedRelation($this->sensorable)),
30
+ 'name' => $this->name,
31
+ 'type' => $this->type,
32
+ 'internal_id' => $this->internal_id,
33
+ 'imei' => $this->imei,
34
+ 'imsi' => $this->imsi,
35
+ 'firmware_version' => $this->firmware_version,
36
+ 'serial_number' => $this->serial_number,
37
+ 'last_position' => $this->last_position,
38
+ 'unit' => $this->unit,
39
+ 'min_threshold' => $this->min_threshold,
40
+ 'max_threshold' => $this->max_threshold,
41
+ 'threshold_inclusive' => $this->threshold_inclusive,
42
+ 'last_reading_at' => $this->last_reading_at,
43
+ 'last_value' => $this->last_value,
44
+ 'calibration' => data_get($this, 'calibration', Utils::createObject()),
45
+ 'report_frequency_sec' => $this->report_frequency_sec,
46
+ 'status' => $this->status,
47
+ 'meta' => data_get($this, 'meta', Utils::createObject()),
48
+ 'is_active' => $this->is_active,
49
+ 'threshold_status' => $this->threshold_status,
50
+ 'photo_url' => $this->photo_url,
51
+ 'device_name' => $this->device_name,
52
+ 'warranty_name' => $this->warranty_name,
53
+ 'attached_to_name' => $this->attached_to_name,
54
+ 'slug' => $this->slug,
55
+ 'updated_at' => $this->updated_at,
56
+ 'created_at' => $this->created_at,
57
+ ]);
58
+ }
59
+
60
+ protected function resolveLoadedRelation($model)
61
+ {
62
+ if (!$model) {
63
+ return null;
64
+ }
65
+
66
+ return Http::isInternalRequest() ? Resolve::httpResourceForModel($model) : $model->public_id;
67
+ }
68
+ }
@@ -28,8 +28,9 @@ class SyncTelematicDevicesJob implements ShouldQueue
28
28
  public Telematic $telematic;
29
29
  public array $options;
30
30
  public string $jobId;
31
- public int $tries = 1;
32
- public int $timeout = 300;
31
+ public int $tries = 1;
32
+ public int $timeout = 3600;
33
+ public bool $failOnTimeout = true;
33
34
 
34
35
  /**
35
36
  * Create a new job instance.
@@ -79,12 +80,22 @@ class SyncTelematicDevicesJob implements ShouldQueue
79
80
  $cursor = null;
80
81
  $totalFetched = 0;
81
82
  $totalLinked = 0;
83
+ $totalLinkAttempts = 0;
82
84
  $totalEvents = 0;
83
85
  $totalSensors = 0;
84
86
  $totalSkipped = 0;
85
87
  $pageCount = 0;
86
88
  $lastProviderAllCount = null;
87
89
  $lastProviderFiltersCount = null;
90
+ $providerSyncMeta = [];
91
+ $linkedDeviceKeys = [];
92
+ $inventoryPayloads = [];
93
+ $inventoryFetched = 0;
94
+ $inventoryLinked = 0;
95
+ $inventorySkipped = 0;
96
+ $totalEnrichment = 0;
97
+ $totalEnrichmentCompleted = 0;
98
+ $totalEnrichmentFailures = 0;
88
99
 
89
100
  try {
90
101
  $provider = $registry->resolve($this->telematic->provider);
@@ -97,10 +108,12 @@ class SyncTelematicDevicesJob implements ShouldQueue
97
108
  'filters' => $this->options['filters'] ?? [],
98
109
  ]);
99
110
 
100
- $devices = $response['devices'] ?? [];
101
- $pagination = $response['pagination'] ?? [];
111
+ $devices = $response['devices'] ?? [];
112
+ $pagination = $response['pagination'] ?? [];
113
+ $providerSyncMeta = array_merge($providerSyncMeta, $response['sync_meta'] ?? []);
102
114
  $pageCount++;
103
115
  $totalFetched += count($devices);
116
+ $inventoryPayloads = array_merge($inventoryPayloads, $devices);
104
117
 
105
118
  if (isset($pagination['allCount'])) {
106
119
  $lastProviderAllCount = $pagination['allCount'];
@@ -128,10 +141,15 @@ class SyncTelematicDevicesJob implements ShouldQueue
128
141
  $normalizedDevice = $provider->normalizeDevice($devicePayload);
129
142
  try {
130
143
  $result = $service->ingestDeviceSnapshot($this->telematic, $provider, $devicePayload);
131
- $totalLinked++;
132
- if ($result['event']) {
133
- $totalEvents++;
144
+ $totalLinkAttempts++;
145
+ $linkedDeviceKey = $this->resolveLinkedDeviceKey($result, $normalizedDevice);
146
+
147
+ if ($linkedDeviceKey && !isset($linkedDeviceKeys[$linkedDeviceKey])) {
148
+ $linkedDeviceKeys[$linkedDeviceKey] = true;
149
+ $totalLinked++;
134
150
  }
151
+
152
+ $totalEvents += count($result['events'] ?? array_filter([$result['event'] ?? null]));
135
153
  $totalSensors += $result['sensors'] ?? 0;
136
154
  } catch (\Illuminate\Validation\ValidationException $e) {
137
155
  $totalSkipped++;
@@ -152,6 +170,7 @@ class SyncTelematicDevicesJob implements ShouldQueue
152
170
  'correlation_id' => $correlationId,
153
171
  'fetched' => $totalFetched,
154
172
  'linked' => $totalLinked,
173
+ 'link_attempts' => $totalLinkAttempts,
155
174
  'events' => $totalEvents,
156
175
  'sensors' => $totalSensors,
157
176
  'skipped' => $totalSkipped,
@@ -161,10 +180,102 @@ class SyncTelematicDevicesJob implements ShouldQueue
161
180
  // Broadcast progress (TODO: implement WebSocket broadcasting)
162
181
  } while (($response['has_more'] ?? false) && $cursor);
163
182
 
183
+ $inventoryFetched = $totalFetched;
184
+ $inventoryLinked = $totalLinked;
185
+ $inventorySkipped = $totalSkipped;
186
+
187
+ $this->telematic->status = 'synchronizing';
188
+ $this->telematic->meta = array_merge($this->telematic->meta ?? [], $providerSyncMeta, [
189
+ 'last_sync_job_id' => $this->jobId,
190
+ 'last_sync_result' => 'inventory_synced',
191
+ 'last_sync_total' => $totalLinked,
192
+ 'last_sync_inventory_completed_at' => now()->toDateTimeString(),
193
+ 'last_sync_inventory_total' => $inventoryFetched,
194
+ 'last_sync_inventory_linked_total' => $inventoryLinked,
195
+ 'last_sync_inventory_skipped_total' => $inventorySkipped,
196
+ 'last_sync_fetched_total' => $totalFetched,
197
+ 'last_sync_linked_total' => $totalLinked,
198
+ 'last_sync_link_attempts_total' => $totalLinkAttempts,
199
+ 'last_sync_skipped_total' => $totalSkipped,
200
+ 'last_sync_page_count' => $pageCount,
201
+ 'last_sync_provider_total' => $lastProviderFiltersCount ?? $lastProviderAllCount,
202
+ 'last_sync_provider_all_count' => $lastProviderAllCount,
203
+ 'last_sync_provider_filters_count' => $lastProviderFiltersCount,
204
+ ]);
205
+ $this->telematic->save();
206
+
207
+ Log::info($this->telematic->provider === 'safee' ? 'Safee inventory sync completed' : 'Telematics inventory sync completed', [
208
+ 'correlation_id' => $correlationId,
209
+ 'telematic_uuid' => $this->telematic->uuid,
210
+ 'provider' => $this->telematic->provider,
211
+ 'inventory_total' => $inventoryFetched,
212
+ 'inventory_linked' => $inventoryLinked,
213
+ 'skipped' => $inventorySkipped,
214
+ ]);
215
+
216
+ if (method_exists($provider, 'fetchDeviceTelemetrySnapshots') && !empty($inventoryPayloads)) {
217
+ Log::info($this->telematic->provider === 'safee' ? 'Safee telemetry enrichment started' : 'Telematics telemetry enrichment started', [
218
+ 'correlation_id' => $correlationId,
219
+ 'telematic_uuid' => $this->telematic->uuid,
220
+ 'provider' => $this->telematic->provider,
221
+ 'device_count' => count($inventoryPayloads),
222
+ ]);
223
+
224
+ $enrichmentResponse = $provider->fetchDeviceTelemetrySnapshots($inventoryPayloads, [
225
+ 'limit' => $this->options['limit'] ?? null,
226
+ 'filters' => $this->options['filters'] ?? [],
227
+ ]);
228
+ $providerSyncMeta = array_merge($providerSyncMeta, $enrichmentResponse['sync_meta'] ?? []);
229
+ $enrichedDevices = $enrichmentResponse['devices'] ?? [];
230
+ $totalEnrichment += count($enrichedDevices);
231
+
232
+ foreach ($enrichedDevices as $devicePayload) {
233
+ $normalizedDevice = $provider->normalizeDevice($devicePayload);
234
+ try {
235
+ $result = $service->ingestDeviceSnapshot($this->telematic, $provider, $devicePayload);
236
+ $totalLinkAttempts++;
237
+ $linkedDeviceKey = $this->resolveLinkedDeviceKey($result, $normalizedDevice);
238
+
239
+ if ($linkedDeviceKey && !isset($linkedDeviceKeys[$linkedDeviceKey])) {
240
+ $linkedDeviceKeys[$linkedDeviceKey] = true;
241
+ $totalLinked++;
242
+ }
243
+
244
+ $totalEnrichmentCompleted++;
245
+ $totalEvents += count($result['events'] ?? array_filter([$result['event'] ?? null]));
246
+ $totalSensors += $result['sensors'] ?? 0;
247
+ } catch (\Illuminate\Validation\ValidationException $e) {
248
+ $totalSkipped++;
249
+ $totalEnrichmentFailures++;
250
+ Log::warning('Skipping telematics enrichment without provider identity', [
251
+ 'correlation_id' => $correlationId,
252
+ 'provider' => $this->telematic->provider,
253
+ 'provider_unit_id' => $devicePayload['_id'] ?? $devicePayload['id'] ?? null,
254
+ 'device_id' => $normalizedDevice['device_id'] ?? null,
255
+ 'name' => $normalizedDevice['name'] ?? $devicePayload['name'] ?? null,
256
+ 'imei' => $devicePayload['imei'] ?? null,
257
+ ]);
258
+ }
259
+ }
260
+
261
+ Log::info($this->telematic->provider === 'safee' ? 'Safee telemetry enrichment completed' : 'Telematics telemetry enrichment completed', [
262
+ 'correlation_id' => $correlationId,
263
+ 'telematic_uuid' => $this->telematic->uuid,
264
+ 'provider' => $this->telematic->provider,
265
+ 'total' => $totalEnrichment,
266
+ 'completed' => $totalEnrichmentCompleted,
267
+ 'failures' => $totalEnrichmentFailures,
268
+ ]);
269
+ }
270
+
164
271
  Log::info('Device discovery completed', [
165
272
  'correlation_id' => $correlationId,
166
273
  'total_fetched' => $totalFetched,
167
274
  'total_linked' => $totalLinked,
275
+ 'total_link_attempts' => $totalLinkAttempts,
276
+ 'total_enrichment' => $totalEnrichment,
277
+ 'enrichment_completed' => $totalEnrichmentCompleted,
278
+ 'enrichment_failures' => $totalEnrichmentFailures,
168
279
  'total_events' => $totalEvents,
169
280
  'total_sensors' => $totalSensors,
170
281
  'total_skipped' => $totalSkipped,
@@ -174,22 +285,29 @@ class SyncTelematicDevicesJob implements ShouldQueue
174
285
  ]);
175
286
 
176
287
  $this->telematic->status = 'active';
177
- $this->telematic->meta = array_merge($this->telematic->meta ?? [], [
178
- 'last_sync_job_id' => $this->jobId,
179
- 'last_sync_completed_at' => now()->toDateTimeString(),
180
- 'last_sync_result' => 'success',
181
- 'last_sync_total' => $totalLinked,
182
- 'last_sync_fetched_total' => $totalFetched,
183
- 'last_sync_linked_total' => $totalLinked,
184
- 'last_sync_events_total' => $totalEvents,
185
- 'last_sync_sensors_total' => $totalSensors,
186
- 'last_sync_skipped_total' => $totalSkipped,
187
- 'last_sync_page_count' => $pageCount,
188
- 'last_sync_provider_total' => $lastProviderFiltersCount ?? $lastProviderAllCount,
189
- 'last_sync_provider_all_count' => $lastProviderAllCount,
190
- 'last_sync_provider_filters_count' => $lastProviderFiltersCount,
191
- 'last_sync_error' => null,
192
- 'last_sync_error_context' => null,
288
+ $this->telematic->meta = array_merge($this->telematic->meta ?? [], $providerSyncMeta, [
289
+ 'last_sync_job_id' => $this->jobId,
290
+ 'last_sync_completed_at' => now()->toDateTimeString(),
291
+ 'last_sync_result' => 'success',
292
+ 'last_sync_total' => $totalLinked,
293
+ 'last_sync_fetched_total' => $totalFetched,
294
+ 'last_sync_linked_total' => $totalLinked,
295
+ 'last_sync_link_attempts_total' => $totalLinkAttempts,
296
+ 'last_sync_inventory_total' => $inventoryFetched,
297
+ 'last_sync_inventory_linked_total' => $inventoryLinked,
298
+ 'last_sync_inventory_skipped_total' => $inventorySkipped,
299
+ 'last_sync_enrichment_total' => $totalEnrichment,
300
+ 'last_sync_enrichment_completed' => $totalEnrichmentCompleted,
301
+ 'last_sync_enrichment_failures' => $totalEnrichmentFailures,
302
+ 'last_sync_events_total' => $totalEvents,
303
+ 'last_sync_sensors_total' => $totalSensors,
304
+ 'last_sync_skipped_total' => $totalSkipped,
305
+ 'last_sync_page_count' => $pageCount,
306
+ 'last_sync_provider_total' => $lastProviderFiltersCount ?? $lastProviderAllCount,
307
+ 'last_sync_provider_all_count' => $lastProviderAllCount,
308
+ 'last_sync_provider_filters_count' => $lastProviderFiltersCount,
309
+ 'last_sync_error' => null,
310
+ 'last_sync_error_context' => null,
193
311
  ]);
194
312
  $this->telematic->save();
195
313
  } catch (\Exception $e) {
@@ -205,21 +323,28 @@ class SyncTelematicDevicesJob implements ShouldQueue
205
323
 
206
324
  $this->telematic->status = 'error';
207
325
  $this->telematic->meta = array_merge($this->telematic->meta ?? [], [
208
- 'last_sync_job_id' => $this->jobId,
209
- 'last_sync_result' => 'failed',
210
- 'last_sync_fetched_total' => $totalFetched,
211
- 'last_sync_linked_total' => $totalLinked,
212
- 'last_sync_events_total' => $totalEvents,
213
- 'last_sync_sensors_total' => $totalSensors,
214
- 'last_sync_skipped_total' => $totalSkipped,
215
- 'last_sync_page_count' => $pageCount,
216
- 'last_sync_provider_total' => $lastProviderFiltersCount ?? $lastProviderAllCount,
217
- 'last_sync_provider_all_count' => $lastProviderAllCount,
218
- 'last_sync_provider_filters_count' => $lastProviderFiltersCount,
219
- 'last_sync_error' => $failureMessage,
220
- 'last_sync_error_type' => class_basename($e),
221
- 'last_sync_error_context' => $failureContext,
222
- 'last_sync_failed_at' => now()->toDateTimeString(),
326
+ 'last_sync_job_id' => $this->jobId,
327
+ 'last_sync_result' => 'failed',
328
+ 'last_sync_fetched_total' => $totalFetched,
329
+ 'last_sync_linked_total' => $totalLinked,
330
+ 'last_sync_link_attempts_total' => $totalLinkAttempts,
331
+ 'last_sync_inventory_total' => $inventoryFetched,
332
+ 'last_sync_inventory_linked_total' => $inventoryLinked,
333
+ 'last_sync_inventory_skipped_total' => $inventorySkipped,
334
+ 'last_sync_enrichment_total' => $totalEnrichment,
335
+ 'last_sync_enrichment_completed' => $totalEnrichmentCompleted,
336
+ 'last_sync_enrichment_failures' => $totalEnrichmentFailures,
337
+ 'last_sync_events_total' => $totalEvents,
338
+ 'last_sync_sensors_total' => $totalSensors,
339
+ 'last_sync_skipped_total' => $totalSkipped,
340
+ 'last_sync_page_count' => $pageCount,
341
+ 'last_sync_provider_total' => $lastProviderFiltersCount ?? $lastProviderAllCount,
342
+ 'last_sync_provider_all_count' => $lastProviderAllCount,
343
+ 'last_sync_provider_filters_count' => $lastProviderFiltersCount,
344
+ 'last_sync_error' => $failureMessage,
345
+ 'last_sync_error_type' => class_basename($e),
346
+ 'last_sync_error_context' => $failureContext,
347
+ 'last_sync_failed_at' => now()->toDateTimeString(),
223
348
  ]);
224
349
  $this->telematic->save();
225
350
 
@@ -238,6 +363,37 @@ class SyncTelematicDevicesJob implements ShouldQueue
238
363
  return $this->jobId;
239
364
  }
240
365
 
366
+ public function failed(\Throwable $e): void
367
+ {
368
+ $this->telematic->refresh();
369
+ $this->telematic->status = 'error';
370
+ $this->telematic->meta = array_merge($this->telematic->meta ?? [], [
371
+ 'last_sync_job_id' => $this->jobId,
372
+ 'last_sync_result' => 'failed',
373
+ 'last_sync_error' => $this->safeSyncErrorMessage($e),
374
+ 'last_sync_error_type' => class_basename($e),
375
+ 'last_sync_failed_at' => now()->toDateTimeString(),
376
+ 'last_sync_failed_reason' => $e instanceof \Illuminate\Queue\TimeoutExceededException ? 'job_timeout' : null,
377
+ ]);
378
+ $this->telematic->save();
379
+ }
380
+
381
+ protected function resolveLinkedDeviceKey(array $result, array $normalizedDevice): ?string
382
+ {
383
+ $device = $result['device'] ?? null;
384
+ $value = null;
385
+
386
+ if (is_object($device)) {
387
+ $value = $device->uuid ?? $device->device_id ?? null;
388
+ }
389
+
390
+ $value ??= $normalizedDevice['device_id']
391
+ ?? $normalizedDevice['external_id']
392
+ ?? null;
393
+
394
+ return $value === null || $value === '' ? null : (string) $value;
395
+ }
396
+
241
397
  protected function safeSyncErrorMessage(\Throwable $e): string
242
398
  {
243
399
  $message = $e->getMessage();