Files
api/services/nginx/app/routes/vehiclesRoute.php
T

681 lines
32 KiB
PHP

<?php
namespace routes;
use classes\authentication;
use customers\economic_customer_mo;
use objects\customer_vehicles_o;
use objects\logs_o;
use objects\orders_o;
use objects\products_o;
use objects\users_o;
use traits\route_t;
class vehiclesRoute
{
use route_t;
public function run(): void
{
$this->get('/vehicles', function () {
// Require the user to be logged in
global $response;
$this->requirePermission('list_own_vehicles');
// 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, '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
if (!$user->hasPermission('list_vehicles_other')) {
$restrict = [
'customer_id' => (int)$user->customer_number->value(),
];
}
$response->success(
$vehicles_o->listObjectsWithPaginationIfSet(
function ($vehicle) use ($user) {
// Return the object as an array
return [
...(new customer_vehicles_o())->select($vehicle['id'])->asArray(),
];
},
$vehicles_o->forceRestrictFilters([
...$restrict ?? []
])
)
);
} else {
// Log the incident
(new logs_o())->add('vehicles', 'global', 1, 0, 'LIST_OWN_VEHICLES', 'No user found, or invalid session');
// Return an error
$response->error('Invalid session', 400);
}
},
[
'list_own_vehicles' => 'List own vehicles',
'list_vehicles_other' => 'List other users vehicles',
]
);
$this->get('/department/vehicles/unknown-customer', function () {
// Require the user to be logged in
global $response;
$this->requirePermission('list_unknown_customer_vehicles');
// 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, 'LIST_UNKNOWN_CUSTOMER_VEHICLES', 'Successfully listed unknown customer vehicles');
// Return the list of the user's vehicles
$vehicles_o = new orders_o();
$vehicles_o->setView('unique_order_reg_1');
$response->success($vehicles_o
->setSearchableFields([
'reg_1'
])
->listObjectsWithPaginationIfSet()
);
} else {
// Log the incident
(new logs_o())->add('vehicles', 'global', 1, 0, 'LIST_UNKNOWN_CUSTOMER_VEHICLES', 'No user found, or invalid session');
// Return an error
$response->error('Invalid session', 400);
}
},
[
'list_unknown_customer_vehicles' => 'List unknown customer vehicles',
]
);
$this->post('/vehicles', function () {
// Require the user to be logged in
global $response;
$this->requirePermission('add_vehicle');
// Get the user object
$user = (new authentication())->get_user();
// Check if the request was successful
if ($user) {
// Require the parameters
self::requireParameters([
'type',
'reg',
'wash_subscription',
]);
// Set the customer_id to the one from the user
$target_user = $user;
// Check if customer_id is set
if (self::isParametersSet([
'customer_id',
])) {
// Check if the customer_id is the same as the current user
if ((int)$user->customer_number->value() !== (int)self::getParameter('customer_id')) {
// Check if the user has permission to add vehicles to other users
if (!$user->hasPermission('add_vehicle_other')) {
// Log the incident
(new logs_o())->add('vehicles', 'global', 1, $user->id, 'ADD_VEHICLE', 'User tried to add a vehicle to another user');
// Return an error
$response->error('You are not allowed to add vehicles to other users', 403);
} else {
// Set the customer_id to the one from the request
$target_user = (new users_o());
$target_user->getUserByCustomerNumber((int)self::getParameter('customer_id'));
}
}
}
$reference = null;
// Check if the reference is set
if (self::isParametersSet([
'reference',
])) {
// If the reference is not empty, check if it is valid
if (!empty(self::getParameter('reference'))) {
// Check if the reference is valid
$reference = (string)self::getParameter('reference');
self::requireType($reference, self::type_string());
self::requireMinLength('reference', 1);
self::requireMaxLength('reference', 255);
}
}
// Validate the parameters
self::requireType(
self::getParameter('reg'),
self::type_string()
);
self::requireType(
self::getParameter('type'),
self::type_int()
);
self::requireType(
self::getParameter('wash_subscription'),
self::type_bool()
);
// Get the parameters
$reg = (string)self::getParameter('reg');
$type = (int)self::getParameter('type');
$subscription = (bool)self::getParameter('wash_subscription');
// Create a new vehicle
$vehicle = new customer_vehicles_o();
$vehicle->add(
$target_user->customer_number->value(),
$type,
$reg,
$subscription ? 1 : 0,
$reference
);
// Log the incident
(new logs_o())->add('vehicles', 'global', 1, $user->id, 'ADD_VEHICLE', 'Successfully added vehicle');
// Return the new vehicle
$response->success(
$vehicle->asArray()
);
} else {
// Log the incident
(new logs_o())->add('vehicles', 'global', 1, 0, 'ADD_VEHICLE', 'No user found, or invalid session');
// Return an error
$response->error('Invalid session', 401);
}
},
[
'add_vehicle' => 'Add a vehicle to own vehicles',
'add_vehicle_other' => 'Add a vehicle to another users vehicles',
]
);
$this->put('/vehicles', function () {
// Require the user to be logged in
global $response;
$this->requirePermission('edit_vehicle');
// 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, 'EDIT_VEHICLE', 'User edited a vehicle');
// Get the request data
self::requireParameters([
'id'
]);
$id = (int)self::getParameter('id');
// 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, 'EDIT_VEHICLE', '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, 'EDIT_VEHICLE', 'User tried to edit a vehicle from another user');
// Return an error
$response->error('You are not allowed to edit vehicles from other users', 403);
}
}
// Check all the fields, and if they are set, validate and set them
if (self::isParametersSet(['type'])) {
$type = (int)self::getParameter('type');
// Make sure the type is an integer
self::requireType($type, self::type_int());
self::requireMinValue($type, 0);
// Make sure the type is a valid type (If it isn't 0)
if ($type === 0) {
// Set the type to null
$vehicle->type->set(0);
// Turn off the subscription
$vehicle->wash_subscription->set(0);
return;
} else {
$products_o = new products_o();
$products_o->select((int)$type);
if (!$products_o->exists() || !$products_o->subscription_allowed->value()) {
// Log the incident
(new logs_o())->add('vehicles', 'global', 1, $user->id, 'EDIT_VEHICLE', 'Invalid type');
// Return an error
$response->error('Invalid type', 400);
}
// Set the type
$vehicle->type->set(
(int)$type
);
// TODO: Make the potential vehicleTypeId reflect the correct type.
// It's currently possible to set the vehicleTypeId to a type that is not allowed for the vehicle.
}
}
if (self::isParametersSet(['reg'])) {
$reg = (string)self::getParameter('reg');
self::requireType($reg, self::type_string());
self::requireMinLength('reg', 2);
self::requireMaxLength('reg', 12);
// Set the registration number
$vehicle->reg->set($reg);
}
if (self::isParametersSet(['wash_subscription'])) {
$subscription = (bool)self::getParameter('wash_subscription');
self::requireType($subscription, self::type_bool());
// Check if the type is a valid type (If it isn't 0)
if ((int)$vehicle->type->value() === 0 && $subscription) {
// Set the subscription to null
$response->error('Unable to set subscription, type is not set', 400);
}
// Set the wash subscription
$vehicle->wash_subscription->set($subscription ? 1 : 0);
}
if (self::isParametersSet(['reference'])) {
// Check if the reference is set to null/empty
if (empty(self::getParameter('reference'))) {
// Set the reference to null
$vehicle->reference->nullify();
} else {
// Check if the reference is valid
$reference = (string)self::getParameter('reference');
self::requireType($reference, self::type_string());
self::requireMinLength('reference', 1);
self::requireMaxLength('reference', 255);
// Set the reference
$vehicle->reference->set($reference);
}
}
$vehicle->objectChanged();
// Return the vehicle
$response->success(
$vehicle->asArray()
);
} else {
// Log the incident
(new logs_o())->add('vehicles', 'global', 1, 0, 'EDIT_VEHICLE', 'No user found, or invalid session');
// Return an error
$response->error('Invalid session', 401);
}
},
[
'edit_vehicle' => 'Edit a vehicle',
'edit_vehicle_other' => 'Edit a vehicle from another user'
]
);
$this->delete('/vehicles', function () {
// Require the user to be logged in
global $response;
$this->requirePermission('delete_vehicle');
// 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, 'DELETE_VEHICLE', 'User deleted a vehicle');
// Get the request data
self::requireParameters([
'id'
]);
$id = (int)self::getParameter('id');
// 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, 'DELETE_VEHICLE', 'Vehicle not found');
// Return an error
$response->error('Vehicle not found', 404);
}
// Check if the user is allowed to delete the vehicle
if ((int)$vehicle->customer_id->value() !== (int)$user->customer_number->value()) {
// Check if the user has permission to delete other users vehicles
if (!$user->hasPermission('delete_vehicle_other')) {
// Log the incident
(new logs_o())->add('vehicles', 'global', 1, $user->id, 'DELETE_VEHICLE', 'User tried to delete a vehicle from another user');
// Return an error
$response->error('You are not allowed to delete vehicles from other users', 403);
}
}
// Delete the vehicle
$vehicle->delete();
// Return success
$response->success(
[
'success' => true,
'message' => 'Vehicle deleted successfully'
]
);
} else {
// Log the incident
(new logs_o())->add('vehicles', 'global', 1, 0, 'DELETE_VEHICLE', 'No user found, or invalid session');
// Return an error
$response->error('Invalid session', 401);
}
},
[
'delete_vehicle' => 'Delete a vehicle',
'delete_vehicle_other' => 'Remove (delete) a vehicle from another user'
]
);
$this->get('/department/vehicle/customer-suggestions', function () {
// Require the user to be logged in
global $response;
$this->requirePermission('list_vehicle_customer_suggestions');
// Get the user object
$user = (new authentication())->get_user();
// Check if the request was successful
if ($user) {
self::requireParameters(['reg_1']);
self::requireType((string)self::getParameter('reg_1'), self::type_string());
$plate = (string)self::getParameter('reg_1');
self::requireMinLength('reg_1', 1);
self::requireMaxLength('reg_1', 12);
// Return the list of the user's vehicles
$orders_o = new orders_o();
// Check if the user is allowed to list other user's vehicles
// Get all the customer_ids that has orders with the same plate
$customer_ids = $orders_o->getFieldsWhere([
'reg_1' => $plate,
'deleted_at' => null,
], [
'customer_id',
]);
// Get all the customer_ids that has orders with the same plate
$customer_ids = array_map(function ($customer_id) {
return (int)$customer_id['customer_id'];
}, $customer_ids);
// Remove duplicates
$customer_ids = array_unique($customer_ids);
// Get all the customers that has orders with the same plate
$customers = [];
foreach ( $customer_ids as $customer_id ) {
$customers[] = (new users_o())->getUserByCustomerNumber($customer_id);
}
// Log the incident
(new logs_o())->add('vehicles', 'global', 1, $user->id, 'LIST_VEHICLE_CUSTOMER_SUGGESTIONS', 'Successfully listed customer suggestions for a vehicle');
// Format the response
$response->success(
array_map(function ($customer) {
/** @var users_o $customer */
$customer_number = (int)$customer->customer_number->value();
return [
'id' => (int)$customer->id,
'customer_name' => (string)$customer->getCustomerName((int)$customer_number),
'customer_number' => (int)$customer_number,
'barred' => (new economic_customer_mo())->getCustomerByCustomerNumber((int)$customer_number)->asArray()['barred'],
];
}, $customers)
);
} else {
// Log the incident
(new logs_o())->add('vehicles', 'global', 1, 0, 'LIST_VEHICLE_CUSTOMER_SUGGESTIONS', 'No user found, or invalid session');
// Return an error
$response->error('Invalid session', 400);
}
},
[
'list_vehicle_customer_suggestions' => 'List customer suggestions for a vehicle',
]
);
$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;
$this->requirePermission('list_users_with_vehicle_subscriptions');
// 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, 'LIST_USERS_WITH_VEHICLE_SUBSCRIPTIONS', 'Successfully listed users with vehicle subscriptions');
// Return the list of users with vehicle subscriptions
$vehicles_o = new customer_vehicles_o();
$customer_ids = $vehicles_o->getFieldsWhere(
[
'wash_subscription' => 1,
],
['customer_id']
);
// Get all the customer_ids that has vehicle subscriptions
$customer_ids = array_map(function ($customer_id) {
return (int)$customer_id['customer_id'];
}, $customer_ids);
// Remove duplicates
$customer_ids = array_unique($customer_ids);
$response->success(
$customer_ids ? array_map(function ($customer_id) {
$tmp_user = new users_o();
$tmp_user->getUserByCustomerNumber((int)$customer_id);
return [...$tmp_user->asArray()];
}, $customer_ids) : []
);
} else {
// Log the incident
(new logs_o())->add('vehicles', 'global', 1, 0, 'LIST_USERS_WITH_VEHICLE_SUBSCRIPTIONS', 'No user found, or invalid session');
// Return an error
$response->error('Invalid session', 400);
}
},
[
'list_users_with_vehicle_subscriptions' => 'List users with vehicle subscriptions',
]
);
}
private function getData(mixed $data, $response)
{
if (!isset($data['type'])) {
$response->error('Type is required', 400);
}
if (!isset($data['reg'])) {
$response->error('Registration number is required', 400);
}
if (!isset($data['notes'])) {
$response->error('Notes is required', 400);
}
return $data;
}
private function validateRegistrationNumber(mixed $reg, $response): void
{
if (!preg_match('/^[A-Z0-9]{4,10}$/', $reg)) {
$response->error('Invalid registration number, it must be 4-10 characters long, and only contain uppercase letters and numbers', 400);
}
}
private function validateType(mixed $type, $response): void
{
// Make sure the type is more than 2 characters
if (strlen($type) < 2) {
$response->error('Type is too short, it must be at least 2 characters', 400);
}
// Make sure the type is less than 50 characters
if (strlen($type) > 50) {
$response->error('Type is too long, it must be less than 50 characters', 400);
}
}
private function validateNotes(mixed $notes, $response): void
{
// If the notes are set, make sure they are less than 250 characters
if (isset($notes) && strlen($notes) > 250) {
$response->error('Notes are too long, they must be less than 250 characters', 400);
}
}
}