Files
api/services/nginx/app/routes/workerRoute.php
T
Jeppe Bundgaard ce9913429f Integrate caching for CVR lookup, add economic customer creation endpoint, and enhance license plate recognition validation
- Implemented Redis caching for CVR lookups to improve performance and reduce API calls.
- Introduced economic customer creation functionality with new `createCustomer` method and corresponding route.
- Enhanced license plate recognition validation to handle plates shorter than 3 characters.
2025-10-13 13:32:11 +02:00

112 lines
4.9 KiB
PHP

<?php
namespace routes;
use classes\economic;
use classes\router;
use classes\virkdata;
use modules\virkdata\helpers\virkdata_response;
use objects\users_o;
use traits\route_t;
class workerRoute
{
use route_t;
public function run(): void
{
$this->get('/worker/status', function () {
global /** @var router $router */
$response, $router;
$response->success(['message' => 'Worker is running', 'status' => 'OK', 'time' => date('Y-m-d H:i:s'), 'timezone' => date_default_timezone_get(), 'host' => gethostname(), 'version' => '1.0.1', 'routes' => $router->countRoutes()]);
});
$this->get('/worker/debug', function () {
global /** @var router $router */
$response, $router;
$virkdata = new virkdata();
$response->success('test');
});
$this->get('/worker/licenseplates', function () {
global /** @var router $router */
$response, $db;
$response->error('This endpoint is disabled for security reasons.', 403);
$counted_plates = []; // Array to hold counted license plates
// This is used to fetch all UNIQUE license plates from the database tables:
// 'customer_vehicles' -> 'reg' column
// 'orders' -> 'reg_1', 'reg_2', 'reg_3' columns
// 'bookings' -> 'regNrTraekker', 'regNrTrailer' columns
// 'plate_scans' -> 'plate' column
$tables_and_columns = [
'customer_vehicles' => ['reg'],
'orders' => ['reg_1', 'reg_2', 'reg_3'],
'bookings' => ['regNrTraekker', 'regNrTrailer'],
'plate_scans' => ['plate'],
];
foreach ($tables_and_columns as $table => $columns) {
foreach ($columns as $column) {
$results = $db->query("SELECT DISTINCT $column FROM $table WHERE $column IS NOT NULL AND $column != ''");
foreach ($results as $row) {
$formatted_plate = $this->FORMAT_LICENSE_PLATE($row[$column]);
if ($formatted_plate !== '') {
if (!isset($counted_plates[$formatted_plate])) {
$counted_plates[$formatted_plate] = 0;
}
$counted_plates[$formatted_plate]++;
}
}
}
}
$response->success(['counted_license_plates' => $counted_plates, 'total_unique_plates' => count($counted_plates)]);
});
$this->get('/economic/doesCustomerExist', function () {
global $response;
self::requirePermission( 'economic_does_customer_exist'); // TODO: Remove this
self::requireParameters(['cvr']);
$cvr = self::getParameter('cvr');
// Check if the customer exists in E-conomic.
if (!is_numeric($cvr)) {
$response->error('Invalid CVR number', 400);
}
$economic = new economic();
$results = $economic->customers->customers->search([
'corporateIdentificationNumber' => (string)$cvr,
],[
'skipPages' => 0,
'pageSize' => 1000, // Since the limit is 1000, we need to set the page size to 1000.
])->collection;
if (count($results) === 0) {
$response->error('Customer not found', 404);
}
$response->success(['cvr' => $cvr, 'result' => $results], 200);
});
$this->get('/cvr/lookup', function () {
global $response;
self::requirePermission('cvr_lookup'); // TODO: Remove this
self::requireParameters(['cvr']);
$cvr = self::getParameter('cvr');
if (!is_numeric($cvr)) {
$response->error('Invalid CVR number', 400);
}
// Check if the result is cached
if (!empty(redis->get('cvr_lookup_' . $cvr))) {
$result = (new virkdata_response())->populate((array)json_decode(redis->get('cvr_lookup_' . $cvr)));
} else {
$virkdata = new virkdata();
$result = $virkdata->getCompanyInformation($cvr, '', []);
// Cache the result
redis->set('cvr_lookup_' . $cvr, json_encode($result->asArray()));
// Set the cache expiration time (3 days)
redis->expire('cvr_lookup_' . $cvr, (60 * 60 * 24 * 4));
}
$response->success(['cvr' => $cvr, 'result' => (object)$result->asArray(), 'phone' => $result->phone, 'email' => $result->email], 200);
});
}
protected function FORMAT_LICENSE_PLATE(string $plate): string
{
// Remove all non-alphanumeric characters
$cleaned = preg_replace('/[^A-Za-z0-9]/', '', $plate);
// Convert to uppercase
return strtoupper($cleaned);
}
}