Integrate Virkdata module with new routes, helpers, and configurations

- Added `virkdata` class to handle API operations for company information retrieval and config validation.
- Introduced `virkdata_i` interface and supporting helpers: `virkdata_request_parameters`, `virkdata_response`, and enums for formats, countries, and error codes.
- Implemented new routes: `/worker/debug`, `/worker/licenseplates`, `/cvr/lookup`, and `/economic/doesCustomerExist`.
- Added module `Virkdata` route for searching company data and fetching/updating configurations (`moduleVirkDataRoute`).
- Created `virkdata` module configurations: `enabled`, `secret_key`, and `monthly_limit`.
- Integrated virkdata initialization in `index.php`.
This commit is contained in:
Jeppe Bundgaard
2025-10-12 12:50:57 +02:00
parent 6f68713c7a
commit ca29ded6be
16 changed files with 841 additions and 0 deletions
+47
View File
@@ -2,7 +2,10 @@
namespace routes;
use classes\economic;
use classes\router;
use classes\virkdata;
use objects\users_o;
use traits\route_t;
class workerRoute
@@ -16,6 +19,12 @@ class workerRoute
$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;
@@ -48,6 +57,44 @@ class workerRoute
}
$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);
}
$ch = curl_init();
curl_setopt($ch, CURLOPT_URL, 'https://cvrapi.dk/api?search=' . $cvr . '&country=dk');
curl_setopt($ch, CURLOPT_RETURNTRANSFER, true);
curl_setopt($ch, CURLOPT_USERAGENT, 'Truck Wash ApS (truckwash.dk) - Kundeimporterings-modul');
$result = curl_exec($ch);
curl_close($ch);
$result = json_decode($result);
$response->success(['cvr' => $cvr, 'result' => $result, 'phone' => $result->phone, 'email' => $result->email], 200);
});
}
protected function FORMAT_LICENSE_PLATE(string $plate): string