From 648a58b7243521adf6b3048e0d2fc5d57e7bc107 Mon Sep 17 00:00:00 2001 From: Jeppe Bundgaard Date: Wed, 16 Jul 2025 10:38:06 +0200 Subject: [PATCH] Add import usage logs functionality to XL Vask module --- .../modules/xlvask/helpers/xlvask_helper.php | 10 +++- .../xlvask/helpers/xlvask_usage_log.php | 12 ++--- .../nginx/app/objects/xlvask_usage_logs_o.php | 53 +++++++++++++++++++ .../nginx/app/objects/xlvask_vehicles_o.php | 47 ---------------- .../nginx/app/routes/moduleXLVaskRoute.php | 18 +++++++ services/nginx/app/traits/db_object_t.php | 2 + 6 files changed, 87 insertions(+), 55 deletions(-) diff --git a/services/nginx/app/modules/xlvask/helpers/xlvask_helper.php b/services/nginx/app/modules/xlvask/helpers/xlvask_helper.php index 4aa16119..362b24b2 100644 --- a/services/nginx/app/modules/xlvask/helpers/xlvask_helper.php +++ b/services/nginx/app/modules/xlvask/helpers/xlvask_helper.php @@ -6,12 +6,18 @@ abstract class xlvask_helper { /** * Convert the helper object to an array representation. - * This method returns an associative array containing all properties of the object. + * This method returns an associative array containing all non-private properties of the object. * @return array */ public function toArray(): array { - return (array)$this; + return array_filter( + get_object_vars($this), + function ($property) { + return !str_starts_with($property, "\0"); + }, + ARRAY_FILTER_USE_KEY + ); } /** diff --git a/services/nginx/app/modules/xlvask/helpers/xlvask_usage_log.php b/services/nginx/app/modules/xlvask/helpers/xlvask_usage_log.php index 0a17c79d..8d314623 100644 --- a/services/nginx/app/modules/xlvask/helpers/xlvask_usage_log.php +++ b/services/nginx/app/modules/xlvask/helpers/xlvask_usage_log.php @@ -136,12 +136,12 @@ class xlvask_usage_log extends xlvask_helper */ public array $WashItems; - protected string $default_string = 'DEFAULT_STRING_1'; - protected string $default_int = 'DEFAULT_INT_1'; - protected string $default_bool = 'DEFAULT_BOOL_1'; - protected string $default_int_nullable = 'DEFAULT_INT_NULLABLE_1'; - protected string $default_string_nullable = 'DEFAULT_STRING_NULLABLE_1'; - protected string $default_bool_nullable = 'DEFAULT_BOOL_NULLABLE_1'; + private string $default_string = 'DEFAULT_STRING_1'; + private string $default_int = 'DEFAULT_INT_1'; + private string $default_bool = 'DEFAULT_BOOL_1'; + private string $default_int_nullable = 'DEFAULT_INT_NULLABLE_1'; + private string $default_string_nullable = 'DEFAULT_STRING_NULLABLE_1'; + private string $default_bool_nullable = 'DEFAULT_BOOL_NULLABLE_1'; /** * Constructor to initialize the customer object with default values diff --git a/services/nginx/app/objects/xlvask_usage_logs_o.php b/services/nginx/app/objects/xlvask_usage_logs_o.php index 00b15bec..b5bfbf07 100644 --- a/services/nginx/app/objects/xlvask_usage_logs_o.php +++ b/services/nginx/app/objects/xlvask_usage_logs_o.php @@ -7,6 +7,7 @@ use classes\object_property; use classes\xlvask; use Exception; use helpers\xlvask_customer; +use helpers\xlvask_usage_log; use helpers\xlvask_vehicle; use traits\db_object_t; @@ -82,4 +83,56 @@ class xlvask_usage_logs_o extends db { //TODO: Add cache invalidation } + + /** + * Import the usage logs from XL Vask + * @throws Exception If the objects were not successfully added. + * @returns void + */ + public function importUsageLogs(): void + { + if (!empty($this->id)) { + throw new Exception('To prevent issues, having a selected object is not allowed.'); + } + $usage_logs = $this->getUsageLogsFromXLVask(); + /** @var string[] $known_usage_logIds The XL Vask usage logIds currently known */ + $known_usage_logIds = array_map(function ($log) { + return $log['WashId']; + }, self::getFields(['WashId'])); + /** The XL Vask usage logs without a matching WashId in the database */ + $new_usage_logs = array_filter($usage_logs, function ($log) use ($known_usage_logIds) { + return !in_array($log->WashId, $known_usage_logIds); + }); + unset($known_usage_logIds); + unset($usage_logs); + /** Adding the new usage logs */ + foreach ($new_usage_logs as $log) { + if (!$log->isValid()) { + echo $log->formattedDetails(); + throw new Exception('A usage log from XL Vask is not valid, have the structure changed?'); + } + } + // Actually save the usage log + foreach ($new_usage_logs as $log) { + /** @var xlvask_usage_log $log */ + $this->add($log->toArray()); + } + unset($new_usage_logs); + } + + /** + * @returns xlvask_usage_log[] + * @throws Exception + */ + public function getUsageLogsFromXLVask(): array + { + /** Get the vehicles from XL Vask */ + $xlvask = new xlvask(); + $vehicle_class = $xlvask->new($xlvask->helpers->xlvask_usage_log); + /** @var xlvask_usage_log[] $vehicles */ + $vehicles = $vehicle_class->toObjects($xlvask->getUsageLog( + '2025-07-01T00:00:00.000', // TODO: Make this dynamic, after the first import + )); + return $vehicles; + } } \ No newline at end of file diff --git a/services/nginx/app/objects/xlvask_vehicles_o.php b/services/nginx/app/objects/xlvask_vehicles_o.php index d647e0c6..5c443b04 100644 --- a/services/nginx/app/objects/xlvask_vehicles_o.php +++ b/services/nginx/app/objects/xlvask_vehicles_o.php @@ -96,53 +96,6 @@ class xlvask_vehicles_o extends db unset($new_vehicles); } - /** - * Import the customers from XL Vask - * @throws Exception If the objects were not successfully added. - * @returns void - */ - public function importCustomers(): void - { - if (!empty($this->id)) { - throw new Exception('To prevent issues, having a selected object is not allowed.'); - } - $customers = $this->getCustomersFromXLVask(); - /** @var string[] $known_customerIds The XL Vask customerIds currently known */ - $known_customerIds = array_map(function ($customer) { - return $customer['customerId']; - }, self::getFields(['customerId'])); - /** The XL Vask customers without a matching customerId in the database */ - $new_customers = array_filter($customers, function ($customer) use ($known_customerIds) { - return !in_array($customer->customerId, $known_customerIds); - }); - unset($known_customerIds); - unset($customers); - /** Adding the new customers */ - foreach ($new_customers as $customer) { - if (!$customer->isValid()) { - echo $customer->formattedDetails(); - throw new Exception('A customer from XL Vask is not valid, have the structure changed?'); - } - // Actually save the customer - $this->add($customer->toArray()); - } - unset($new_customers); - } - - /** - * @return xlvask_customer[] - * @throws Exception - */ - public function getCustomersFromXLVask(): array - { - /** Get the customers from XL Vask */ - $xlvask = new xlvask(); - $customer_class = $xlvask->new($xlvask->helpers->xlvask_customer); - /** @var xlvask_customer[] $customers */ - $customers = $customer_class->toObjects($xlvask->getCustomers()); - return $customers; - } - /** * @returns xlvask_vehicle[] * @throws Exception diff --git a/services/nginx/app/routes/moduleXLVaskRoute.php b/services/nginx/app/routes/moduleXLVaskRoute.php index 209e43e8..d8b52a6d 100644 --- a/services/nginx/app/routes/moduleXLVaskRoute.php +++ b/services/nginx/app/routes/moduleXLVaskRoute.php @@ -248,5 +248,23 @@ class moduleXLVaskRoute 'modules_xlvask_import_vehicles' => 'Import vehicles from the xlvask module' ] ); + + $this->get('/modules/xlvask/tasks/import-usage', function () { + global $response; + self::requirePermission('modules_xlvask_import_usage'); + // Create the xlvask_usage_logs_o object + $xlvask_usage_logs_o = new \objects\xlvask_usage_logs_o(); + // Import usage logs + $xlvask_usage_logs_o->importUsageLogs(); + // Response + $response->success( + 'Usage logs imported', + 200 + ); + }, + [ + 'modules_xlvask_import_usage' => 'Import usage from the xlvask module' + ] + ); } } \ No newline at end of file diff --git a/services/nginx/app/traits/db_object_t.php b/services/nginx/app/traits/db_object_t.php index 87f207cb..40bd74ae 100644 --- a/services/nginx/app/traits/db_object_t.php +++ b/services/nginx/app/traits/db_object_t.php @@ -994,6 +994,8 @@ trait db_object_t $db->query($sql); return $db->insert_id(); } catch (Exception $e) { + echo "Error adding object to $this->table: " . $e->getMessage() . "\n"; + echo "SQL: $sql\n"; // Debugging line, can be removed in production throw new Exception($e->getMessage()); } }