diff --git a/services/nginx/app/modules/xlvask/helpers/xlvask_helper.php b/services/nginx/app/modules/xlvask/helpers/xlvask_helper.php index f8f5b463..4aa16119 100644 --- a/services/nginx/app/modules/xlvask/helpers/xlvask_helper.php +++ b/services/nginx/app/modules/xlvask/helpers/xlvask_helper.php @@ -66,4 +66,12 @@ abstract class xlvask_helper } return $tmp; } + + /** + * Default is valid + */ + public function isValid(): bool + { + return true; + } } \ 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 new file mode 100644 index 00000000..d647e0c6 --- /dev/null +++ b/services/nginx/app/objects/xlvask_vehicles_o.php @@ -0,0 +1,159 @@ +setTable('xlvask_vehicles'); + } + + /** + * Add a new XL Vask customer + * @param array $data The properties + * @returns void + * @throws Exception If the object was not created successfully + */ + public function add(array $data): void + { + $tmp_id = self::add_object($data); + $this->id = $tmp_id; + self::getObjectProperties(); + self::objectChanged(); + } + + public function getObjectProperties(): void + { + $this->vehicleId = new object_property($this->table, $this->id, 'vehicleId', 'string', false); + $this->registrationNumber = new object_property($this->table, $this->id, 'registrationNumber', 'string', false); + $this->customerId = new object_property($this->table, $this->id, 'customerId', 'string', false); + $this->vehicleTypeId = new object_property($this->table, $this->id, 'vehicleTypeId', 'string', false); + $this->active = new object_property($this->table, $this->id, 'active', 'string', false); + $this->autoStartOnLpr = new object_property($this->table, $this->id, 'autoStartOnLpr', 'string', false); + $this->updated = new object_property($this->table, $this->id, 'updated', 'string', false); + $this->createDate = new object_property($this->table, $this->id, 'createDate', 'string', false); + $this->externId = new object_property($this->table, $this->id, 'externId', 'string', false); + } + + public function objectChanged(): void + { + //TODO: Add cache invalidation + } + + /** + * Import the vehicles from XL Vask + * @throws Exception If the objects were not successfully added. + * @returns void + */ + public function importVehicles(): void + { + if (!empty($this->id)) { + throw new Exception('To prevent issues, having a selected object is not allowed.'); + } + $vehicles = $this->getVehiclesFromXLVask(); + /** @var string[] $known_vehicleIds The XL Vask vehicleIds currently known */ + $known_vehicleIds = array_map(function ($vehicle) { + return $vehicle['vehicleId']; + }, self::getFields(['vehicleId'])); + /** The XL Vask customers without a matching vehicleId in the database */ + $new_vehicles = array_filter($vehicles, function ($vehicle) use ($known_vehicleIds) { + return !in_array($vehicle->vehicleId, $known_vehicleIds); + }); + unset($known_vehicleIds); + unset($vehicles); + /** Adding the new vehicles */ + foreach ($new_vehicles as $vehicle) { + if (!$vehicle->isValid()) { + echo $vehicle->formattedDetails(); + throw new Exception('A vehicle from XL Vask is not valid, have the structure changed?'); + } + } + // Actually save the vehicle + foreach ($new_vehicles as $vehicle) { + $this->add($vehicle->toArray()); + } + 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 + */ + public function getVehiclesFromXLVask(): array + { + /** Get the vehicles from XL Vask */ + $xlvask = new xlvask(); + $vehicle_class = $xlvask->new($xlvask->helpers->xlvask_vehicle); + /** @var xlvask_vehicle[] $vehicles */ + $vehicles = $vehicle_class->toObjects($xlvask->getVehicles()); + return $vehicles; + } +} \ No newline at end of file diff --git a/services/nginx/app/routes/moduleXLVaskRoute.php b/services/nginx/app/routes/moduleXLVaskRoute.php index 926a1405..209e43e8 100644 --- a/services/nginx/app/routes/moduleXLVaskRoute.php +++ b/services/nginx/app/routes/moduleXLVaskRoute.php @@ -232,5 +232,21 @@ class moduleXLVaskRoute 'modules_xlvask_import_customers' => 'Import customers from the xlvask module' ] ); + + $this->get('/modules/xlvask/tasks/import-vehicles', function () { + global $response; + self::requirePermission('modules_xlvask_import_vehicles'); + // Create the xlvask_vehicles_o object + $xlvask_vehicles_o = new \objects\xlvask_vehicles_o(); + $xlvask_vehicles_o->importVehicles(); + $response->success( + 'Vehicles imported', + 200 + ); + }, + [ + 'modules_xlvask_import_vehicles' => 'Import vehicles from the xlvask module' + ] + ); } } \ No newline at end of file