Add import usage logs functionality to XL Vask module

This commit is contained in:
Jeppe Bundgaard
2025-07-16 10:38:06 +02:00
parent 7a4c53df1b
commit 648a58b724
6 changed files with 87 additions and 55 deletions
@@ -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
);
}
/**
@@ -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
@@ -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;
}
}
@@ -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
@@ -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'
]
);
}
}
@@ -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());
}
}