Add vehicle type handling in customer_vehicles_o, including vehicle type retrieval, setting, and creation in XLVask. Extend route logic to support vehicle type updates and auto-start on LPR configurations, incorporating enhanced validation and permission checks. Refactor and streamline helper classes for consistency and functionality expansion.

This commit is contained in:
Jepp9350
2025-06-19 15:01:22 +02:00
parent 75e3efcb89
commit 29aa63bb08
7 changed files with 328 additions and 29 deletions
@@ -4,5 +4,13 @@ namespace helpers;
abstract class xlvask_helper
{
/**
* Convert the helper object to an array representation.
* This method returns an associative array containing all properties of the object.
* @return array
*/
public function toArray(): array
{
return (array)$this;
}
}
@@ -362,16 +362,7 @@ class xlvask_usage_log extends xlvask_helper
// Check if the parameter is empty or matches the default values
return $param === null || $param === '' || $param === $this->default_string || $param === $this->default_int || $param === $this->default_bool || $param === $this->default_int_nullable || $param === $this->default_string_nullable || $param === $this->default_bool_nullable;
}
/**
* Convert the customer object to an array representation.
* This method returns an associative array containing all properties of the object.
* @return array
*/
public function toArray(): array
{
return (array)$this;
}
/**
* Get the formatted date of the wash start time.
@@ -59,9 +59,5 @@ class xlvask_vehicle extends xlvask_helper
* @var array
*/
public array $multilineVehicleServices;
public function toArray(): array
{
return (array)$this;
}
}
@@ -229,16 +229,7 @@ class xlvask_wash_item extends xlvask_helper
// Check if the parameter is empty or matches the default values
return $param === null || $param === '' || $param === $this->default_string || $param === $this->default_int || $param === $this->default_bool || $param === $this->default_int_nullable || $param === $this->default_string_nullable || $param === $this->default_bool_nullable;
}
/**
* Convert the customer object to an array representation.
* This method returns an associative array containing all properties of the object.
* @return array
*/
public function toArray(): array
{
return (array)$this;
}
/**
* Get the product associated with this wash item.
@@ -6,7 +6,9 @@ use classes\db;
use classes\object_property;
use classes\xlvask;
use Exception;
use helpers\xlvask_customer;
use helpers\xlvask_vehicle;
use helpers\xlvask_vehicle_type;
use helpers\xlvask_vehicles;
use traits\db_object_t;
@@ -68,6 +70,9 @@ class customer_vehicles_o extends db
],
'last_order_id' => ($last_order_id ? (int)$last_order_id : null),
'xlvask' => ($xlvask?->toArray()),
'vehicle_types' => array_map(function ($vehicle_type) {
return $vehicle_type->toArray();
}, $this->getVehicleTypes()),
];
}
@@ -160,6 +165,22 @@ class customer_vehicles_o extends db
return $vehicles->getVehicleByRegistrationNumber((string)$this->reg->value());
}
/**
* Get the vehicle types applicable to the vehicle (Wash types)
* @return array{xlvask_vehicle_type}
* @note This method retrieves the vehicle types from the XLVask system that are applicable to the vehicle.
* @return array of xlvask_vehicle_type objects
* @throws Exception If the object is not selected or if there is an error retrieving the vehicle types
* @see xlvask_vehicle_type
*/
public function getVehicleTypes(): array
{
self::requireSelected();
return (new xlvask())->helpers->xlvask_vehicle_types::getVehicleTypesByProductId(
(int)$this->type->value()
);
}
/**
* Add the default addons to the vehicle
* @return void
@@ -292,4 +313,121 @@ class customer_vehicles_o extends db
return (new customer_vehicles_o())->select((int)$result[0]['id']);
}
/**
* Set the vehicle type id for the vehicle in the XLVask system
* @param string $vehicleTypeId The vehicle type id to set in the XLVask system
* @note This method updates the vehicle type in the XLVask system for the vehicle.
* @throws Exception If the associated customer is not registered in the XLVask system
* @throws Exception If the vehicle is not registered in the XLVask system
* @throws Exception If the vehicle type id is not valid or if there is an error updating the vehicle type in the XLVask system
* @throws Exception If the object is not selected
* @see xlvask_vehicle
* @example
* $vehicle = new customer_vehicles_o();
* $vehicle->select(1); // Select the vehicle with id 1
* $vehicleTypeId = 'some-vehicle-type-id'; // The vehicle type id to set
* $vehicle->setVehicleTypeId($vehicleTypeId);
* @see xlvask_vehicle_type
*/
public function setVehicleTypeId(string $vehicleTypeId): void
{
self::requireSelected();
$xlvask = new xlvask();
// Check if the vehicle is registered in the XLVask system
if (!$this->hasXLVask()) {
// Create a new XLVask vehicle
throw new Exception('Vehicle is not registered in the XLVask system. Please register the vehicle first.');
}
// Set the vehicle type id
$xlvask_vehicle = $this->getXLVask();
$xlvask_vehicle->vehicleTypeId = $vehicleTypeId;
// Update the XLVask vehicle
$xlvask->updateVehicle($xlvask_vehicle);
// Cache the updated vehicle
$xlvask->getCache()->setVehicleCache($xlvask_vehicle->registrationNumber, $xlvask_vehicle);
}
/**
* Create a new vehicle in the XLVask system
* * @param string $registrationNumber The registration number of the vehicle
* @param int $customerId The customer id of the vehicle
* @param string|null $vehicleTypeId The vehicle type id of the vehicle, if not provided, the default vehicle type will be used
* @return xlvask_vehicle The created XLVask vehicle object
* @throws Exception If the customer is not registered in the XLVask system
* @throws Exception If the vehicle is already registered in the XLVask system
* @throws Exception If the vehicle type id is not valid or if there is an error creating the vehicle in the XLVask system
* @throws Exception If the object is not selected
* @note This method creates a new vehicle in the XLVask system for the vehicle.
* @see xlvask_vehicle
* @see xlvask_vehicle_type
* @example
* $vehicle = new customer_vehicles_o();
* $vehicle->select(1); // Select the vehicle with id 1
* $vehicleTypeId = 'some-vehicle-type-id'; // The vehicle type id to set, if not provided, the default vehicle type will be used
* $vehicle->createXLVaskVehicle($vehicleTypeId);
*/
public function createXLVaskVehicle(string $vehicleTypeId): xlvask_vehicle
{
self::requireSelected();
$xlvask = new xlvask();
// Check if the customer is registered in the XLVask system
$xlvask_customer = $this->getXLVaskCustomer();
// Make sure the vehicle type id is valid for this vehicle type
$valid_vehicle_types = $xlvask->helpers->xlvask_vehicle_types::getVehicleTypesByProductId(
(int)$this->type->value()
);
if (!in_array($vehicleTypeId, array_map(fn($type) => $type->vehicleTypeId, $valid_vehicle_types))) {
throw new Exception('Invalid vehicle type id provided. Please provide a valid vehicle type id for this vehicle type.');
}
// Create a new XLVask vehicle
$xlvask_vehicle = new xlvask_vehicle();
$xlvask_vehicle->registrationNumber = (string)$this->reg->value();
$xlvask_vehicle->customerId = $xlvask_customer->customerId;
$xlvask_vehicle->vehicleTypeId = $vehicleTypeId;
$xlvask_vehicle->active = true; // Set the vehicle as active
$xlvask_vehicle->autoStartOnLpr = true; // Enable auto start on LPR
// Add the vehicle to the XLVask system
$xlvask->createVehicle($xlvask_vehicle);
// Cache the vehicle
$vehicles = $xlvask->helpers->new($xlvask->helpers->xlvask_vehicles);
/** @var xlvask_vehicles $vehicles */
$xlvask_vehicle = $vehicles::getVehicleByRegistrationNumber((string)$this->reg->value());
$xlvask->getCache()->setVehicleCache($xlvask_vehicle->registrationNumber, $xlvask_vehicle);
return $xlvask_vehicle;
}
/**
* Get the XLVask customer object for the vehicle
* @return xlvask_customer The XLVask customer object for the vehicle
* @throws Exception If the object is not selected
* @note This method retrieves the XLVask customer object for the vehicle.
*/
public function getXLVaskCustomer(): xlvask_customer
{
self::requireSelected();
$customer_number = (int)$this->customer_id->value();
$xlvask = new xlvask();
if ($xlvask->getCache()->isCustomerCached($customer_number)) {
return $xlvask->getCache()->getCustomerCache($customer_number);
}
// If the customer is not cached, throw an exception
throw new Exception('Customer is not registered in the XLVask system. Please register the customer first.');
}
/**
* Set the auto start on LPR for the vehicle in the XLVask system
* @param bool $autoStartOnLpr Whether to enable auto start on LPR for the vehicle
* @throws Exception If the object is not selected
* @note This method updates the auto start on LPR setting for the vehicle in the XLVask system.
*/
public function setAutoStartOnLpr(bool $autoStartOnLpr): void
{
self::requireSelected();
$xlvask_vehicle = $this->getXLVask();
$xlvask_vehicle->autoStartOnLpr = $autoStartOnLpr;
$xlvask = new xlvask();
$xlvask->updateVehicle($xlvask_vehicle);
// Cache the updated vehicle
$xlvask->getCache()->setVehicleCache($xlvask_vehicle->registrationNumber, $xlvask_vehicle);
}
}
@@ -196,10 +196,12 @@ class moduleXLVaskRoute
$xlvask = new xlvask();
$user = new users_o();
$user->getUserByCustomerNumber(12345679);
$result = $xlvask->getTasks()->runSyncVehicles(false);
//$result = $xlvask->getTasks()->runSyncVehicles(false);
$vehicles = $xlvask->new($xlvask->helpers->xlvask_vehicles);
//print_r($vehicles::getVehicleByRegistrationNumber('BW93159'));
// Response
$response->success(
$result,
'Debugging xlvask tasks',
200
);
},
+174 -1
View File
@@ -27,6 +27,37 @@ class vehiclesRoute
if ($user) {
// Log the incident
(new logs_o())->add('vehicles', 'global', 1, $user->id, 'LIST_OWN_VEHICLES', 'Successfully listed own vehicles');
// Check if the id parameter is set
if ($this->isParametersSet(['id'])) {
// Get the id parameter
$id = (int)$this->getParameter('id');
$this->requireType($id, self::type_int());
$this->requireMinValue($id, 1);
$this->requireMaxValue($id, 9999999999);
// Get the vehicle object
$vehicle = (new customer_vehicles_o())->select($id);
// Check if the vehicle exists
if (!$vehicle->exists()) {
// Log the incident
(new logs_o())->add('vehicles', 'global', 1, $user->id, 'LIST_OWN_VEHICLES', 'Vehicle not found');
// Return an error
$response->error('Vehicle not found', 404);
}
// Check if the user is allowed to list the vehicle
if ((int)$vehicle->customer_id->value() !== (int)$user->customer_number->value()) {
// Check if the user has permission to list other users vehicles
if (!$user->hasPermission('list_vehicles_other')) {
// Log the incident
(new logs_o())->add('vehicles', 'global', 1, $user->id, 'LIST_OWN_VEHICLES', 'User tried to list a vehicle from another user');
// Return an error
$response->error('You are not allowed to list vehicles from other users', 403);
}
}
// Return the vehicle as an array
$response->success(
[...$vehicle->asArray()]
);
}
// Return the list of the user's vehicles
$vehicles_o = new customer_vehicles_o();
// Check if the user is allowed to list other user's vehicles
@@ -212,7 +243,7 @@ class vehiclesRoute
$response->error('Vehicle not found', 404);
}
// Check if the user is allowed to edit the vehicle
if ($vehicle->customer_id->value() !== (int)$user->customer_number->value()) {
if ((int)$vehicle->customer_id->value() !== (int)$user->customer_number->value()) {
// Check if the user has permission to edit other users vehicles
if (!$user->hasPermission('edit_vehicle_other')) {
// Log the incident
@@ -418,6 +449,148 @@ class vehiclesRoute
]
);
$this->post('/vehicles/set-auto-start-on-lpr', function () {
// Require the user to be logged in
global $response;
$this->requirePermission('set_auto_start_on_lpr');
// Get the user object
$user = (new authentication())->get_user();
// Check if the request was successful
if ($user) {
// Log the incident
(new logs_o())->add('vehicles', 'global', 1, $user->id, 'SET_AUTO_START_ON_LPR', 'User set auto start on LPR');
// Get the request data
self::requireParameters([
'id',
'active',
]);
$id = (int)self::getParameter('id');
self::requireType($id, self::type_int());
self::requireMinValue($id, 1);
self::requireMaxValue($id, 9999999999);
// Validate the autoStartOnLpr (active) parameter
$autoStartOnLpr = (bool)self::getParameter('active');
self::requireType($autoStartOnLpr, self::type_bool());
// Get the vehicle object
$vehicle = (new customer_vehicles_o())->select($id);
// Check if the vehicle exists
if (!$vehicle->exists()) {
// Log the incident
(new logs_o())->add('vehicles', 'global', 1, $user->id, 'SET_AUTO_START_ON_LPR', 'Vehicle not found');
// Return an error
$response->error('Vehicle not found', 404);
}
// Check if the user is allowed to edit the vehicle
if ((int)$vehicle->customer_id->value() !== (int)$user->customer_number->value()) {
// Check if the user has permission to edit other users vehicles
if (!$user->hasPermission('edit_vehicle_other')) {
// Log the incident
(new logs_o())->add('vehicles', 'global', 1, $user->id, 'SET_AUTO_START_ON_LPR', 'User tried to set auto start on LPR from another user');
// Return an error
$response->error('You are not allowed to edit vehicles from other users', 403);
}
}
// Set the auto start on LPR
$vehicle->setAutoStartOnLpr(
$autoStartOnLpr
);
// Return the vehicle as an array
$response->success(
[...$vehicle->asArray()]
);
} else {
// Log the incident
(new logs_o())->add('vehicles', 'global', 1, 0, 'SET_AUTO_START_ON_LPR', 'No user found, or invalid session');
// Return an error
$response->error('Invalid session', 401);
}
},
[
'set_auto_start_on_lpr' => 'Set auto start on LPR',
'set_auto_start_on_lpr_other' => 'Set auto start on LPR for another user\'s vehicle',
]
);
$this->post('/vehicles/set-vehicle-type-id', function () {
// Require the user to be logged in
global $response;
$this->requirePermission('set_vehicle_type_id');
// Get the user object
$user = (new authentication())->get_user();
// Check if the request was successful
if ($user) {
// Log the incident
(new logs_o())->add('vehicles', 'global', 1, $user->id, 'SET_VEHICLE_TYPE_ID', 'User set vehicle type ID');
// Get the request data
self::requireParameters([
'id',
'vehicleTypeId',
]);
$id = (int)self::getParameter('id');
self::requireType($id, self::type_int());
self::requireMinValue($id, 1);
self::requireMaxValue($id, 9999999999);
// Validate the vehicleTypeId
$vehicleTypeId = (string)self::getParameter('vehicleTypeId');
self::requireType($vehicleTypeId, self::type_string());
self::requireMinLength('vehicleTypeId', 1);
self::requireMaxLength('vehicleTypeId', 50);
// Get the vehicle object
$vehicle = (new customer_vehicles_o())->select($id);
// Check if the vehicle exists
if (!$vehicle->exists()) {
// Log the incident
(new logs_o())->add('vehicles', 'global', 1, $user->id, 'SET_VEHICLE_TYPE_ID', 'Vehicle not found');
// Return an error
$response->error('Vehicle not found', 404);
}
// Check if the user is allowed to edit the vehicle
if ((int)$vehicle->customer_id->value() !== (int)$user->customer_number->value()) {
// Check if the user has permission to edit other users vehicles
if (!$user->hasPermission('edit_vehicle_other')) {
// Log the incident
(new logs_o())->add('vehicles', 'global', 1, $user->id, 'SET_VEHICLE_TYPE_ID', 'User tried to set vehicle type ID from another user');
// Return an error
$response->error('You are not allowed to edit vehicles from other users', 403);
}
}
// Get the customer object
$customer = (new users_o())->getUserByCustomerNumber((int)$vehicle->customer_id->value());
// Check if the vehicle is registered in the XL Vask system
if (!$vehicle->hasXLVask()) {
// Check if the customer has an XL Vask customer account
if (!$customer->hasXLVaskCustomerAccount()) {
// Log the incident
(new logs_o())->add('vehicles', 'global', 1, $user->id, 'SET_VEHICLE_TYPE_ID', 'User tried to set vehicle type ID on a vehicle that is not registered in the XL Vask system, without a customer account');
// Return an error
$response->error('Vehicle is not registered in the XL Vask system', 400);
} else {
$vehicle->createXLVaskVehicle($vehicleTypeId);
}
} else {
// Set the vehicle type ID
$vehicle->setVehicleTypeId(
$vehicleTypeId
);
}
// Return the vehicle as an array
$response->success(
[...$vehicle->asArray()]
);
} else {
// Log the incident
(new logs_o())->add('vehicles', 'global', 1, 0, 'SET_VEHICLE_TYPE_ID', 'No user found, or invalid session');
// Return an error
$response->error('Invalid session', 401);
}
},
[
'set_vehicle_type_id' => 'Set vehicle type ID',
'set_vehicle_type_id_other' => 'Set vehicle type ID for another user\'s vehicle',
]
);
$this->get('/superuser/users-with-vehicle-subscriptions', function () {
// Require the user to be logged in
global $response;