Files
api/services/nginx/app/routes/vehicleProductSuggestionRoute.php
T
Jeppe Bundgaard 81aa406fb6 Add License Plate Recognizer integration and vehicle product suggestion route
- Implemented `licensePlateRecognizer` module with configuration classes (`enabled`, `api_key`) and API handling.
- Introduced `moduleConfigRoute` endpoints for fetching and updating `licensePlateRecognizer` configurations.
- Added `vehicleProductSuggestionRoute` with logic to fetch product suggestions based on vehicle plates.
- Extended `moduleScannerRoute` with license plate recognition test and exception handling.
- Updated `index.php` to include `licensePlateRecognizer` class.
2025-09-01 10:11:37 +02:00

80 lines
2.8 KiB
PHP

<?php
namespace routes;
use classes\authentication;
use classes\motorapi;
use Exception;
use objects\logs_o;
use objects\motorapi_lookups_o;
use objects\orders_o;
use traits\route_t;
class vehicleProductSuggestionRoute
{
use route_t;
public function run(): void
{
$this->get(
'/vehicle/product-suggestions',
function () {
// Require the user to be logged in
global $response;
$this->requirePermission('vehicle_product_suggestions');
// Get the user object
$user = (new authentication())->get_user();
// Check if the request was successful
if ($user) {
// Make sure the vehicle plate(s) is provided
$reg_1 = null; $reg_2 = null; $reg_3 = null;
foreach (['reg_1', 'reg_2', 'reg_3'] as $key) {
if ($this->isParametersSet([$key])) {
${$key} = (string)$this->fromRequest($key);
self::requireType(${$key}, self::type_string());
self::requireMinLength($key, 1);
self::requireMaxLength($key, 10);
}
}
if (!$reg_1 && !$reg_2 && !$reg_3) {
$response->error('At least one vehicle plate must be provided', 400);
}
// Check if the vehicle has been previously added to an order.
// Check if the vehicle has been previously looked up.
} else {
// Log the incident
(new logs_o())->add('products', 'global', 1, 0, 'FETCH_VEHICLE_PRODUCT_SUGGESTIONS', 'No user found, or invalid session');
// Return an error
$response->error('Invalid session', 400);
}
},
[
'vehicle_product_suggestions' => 'Access to vehicle product suggestions based on internal and external data',
]
);
}
/**
*
* @throws Exception If the MotorAPI request fails, if the registration is invalid, or if the response is malformed
*/
protected function getVehicleMotorAPIValue(string $reg): ?object
{
// Get the MotorAPI response for the vehicle reg
$MotorAPI = new motorapi();
return $MotorAPI->getLicensePlateInformation($reg, false);
}
protected function getSimilarVehicles(array $values): array
{
$tmp_result = (new motorapi_lookups_o())->getCachedResultsBySubstring($values);
return array_map(
function ($item) {
return $item->result->value();
},
$tmp_result
);
}
}