Refactor vehicle management and add vehicle add-on functionality.
This update refactors vehicle-related routes to include consistent endpoints, enhanced functionality, and stricter permission checks. It introduces vehicle add-on management with toggling and retrieval APIs, enabling detailed customization and user control. Additionally, the new `asArray` methods and updated logic improve data handling and validation.
This commit is contained in:
@@ -0,0 +1,67 @@
|
||||
<?php
|
||||
|
||||
namespace objects;
|
||||
|
||||
use classes\db;
|
||||
use classes\object_property;
|
||||
use Exception;
|
||||
use traits\db_object_t;
|
||||
|
||||
class customer_vehicles_addons_o extends db
|
||||
{
|
||||
use db_object_t;
|
||||
|
||||
public object_property $vehicle_id; // The id of the vehicle
|
||||
public object_property $addon_id; // The id of the addon
|
||||
public object_property $amount; // The amount of the addon
|
||||
|
||||
|
||||
public function structure(): void
|
||||
{
|
||||
$this->setTable('customer_vehicles_addons');
|
||||
}
|
||||
|
||||
/**
|
||||
* Add a new vehicle addon
|
||||
* @param int $vehicle_id
|
||||
* @param int $addon_id
|
||||
* @param int $amount
|
||||
* @return void
|
||||
* @throws Exception If the object was not created successfully
|
||||
*/
|
||||
public function add(int $vehicle_id, int $addon_id, int $amount = 1): void
|
||||
{
|
||||
$tmp_id = self::add_object([
|
||||
'vehicle_id' => (int)$vehicle_id,
|
||||
'addon_id' => (int)$addon_id,
|
||||
'amount' => (int)$amount
|
||||
]);
|
||||
$this->id = $tmp_id;
|
||||
self::getObjectProperties();
|
||||
self::objectChanged();
|
||||
}
|
||||
|
||||
public function getObjectProperties(): void
|
||||
{
|
||||
$this->vehicle_id = new object_property($this->table, $this->id, 'vehicle_id', 'int', true);
|
||||
$this->addon_id = new object_property($this->table, $this->id, 'addon_id', 'int', true);
|
||||
$this->amount = new object_property($this->table, $this->id, 'amount', 'int', true);
|
||||
}
|
||||
|
||||
public function objectChanged(): void
|
||||
{
|
||||
//TODO: Add cache invalidation
|
||||
}
|
||||
|
||||
|
||||
public function asArray(): array
|
||||
{
|
||||
return [
|
||||
'id' => (int)$this->id,
|
||||
'vehicle_id' => (int)$this->vehicle_id->value(),
|
||||
'addon_id' => (int)$this->addon_id->value(),
|
||||
'amount' => (int)$this->amount->value(),
|
||||
];
|
||||
}
|
||||
|
||||
}
|
||||
@@ -4,7 +4,7 @@ namespace objects;
|
||||
|
||||
use classes\db;
|
||||
use classes\object_property;
|
||||
use classes\response;
|
||||
use Exception;
|
||||
use traits\db_object_t;
|
||||
|
||||
class customer_vehicles_o extends db
|
||||
@@ -14,118 +14,121 @@ class customer_vehicles_o extends db
|
||||
public object_property $customer_id; // The id of the customer
|
||||
public object_property $type; // The type of the vehicle
|
||||
public object_property $reg; // The registration number of the vehicle
|
||||
public object_property $wash_subscription; // The wash subscription of the vehicle (boolean)
|
||||
public object_property $notes; // The notes of the vehicle
|
||||
public object_property $deleted_at; // The timestamp of when the record was "deleted"
|
||||
|
||||
/**
|
||||
* @param array $getAddons [db_object_t child object]
|
||||
* @return array
|
||||
*/
|
||||
private static function transformObjectsToArray(array $getAddons): array
|
||||
{
|
||||
return array_map(function ($addon) {
|
||||
return $addon->asArray();
|
||||
}, $getAddons);
|
||||
}
|
||||
|
||||
/**
|
||||
* Convert the object to an array
|
||||
* @return array
|
||||
* @throws Exception If the object is not selected
|
||||
*/
|
||||
public function asArray(): array
|
||||
{
|
||||
self::requireSelected();
|
||||
$wash_subscription = (bool)$this->wash_subscription->value();
|
||||
$addons = self::transformObjectsToArray(
|
||||
self::getAddons()
|
||||
);
|
||||
$available_addons = self::transformObjectsToArray(
|
||||
self::getAvailableAddons()
|
||||
);
|
||||
return [
|
||||
'id' => (int)$this->id,
|
||||
'customer_id' => (int)$this->customer_id->value(),
|
||||
'type' => (int)$this->type->value(),
|
||||
'reg' => (string)$this->reg->value(),
|
||||
'wash_subscription' => $wash_subscription,
|
||||
'addons' => [
|
||||
'enabled' => count($addons),
|
||||
'available' => count($available_addons),
|
||||
]
|
||||
];
|
||||
}
|
||||
|
||||
/**
|
||||
* Get the addons for the vehicle
|
||||
* @return array{customer_vehicles_addons_o}
|
||||
* @throws Exception If the object is not selected
|
||||
*/
|
||||
public function getAddons(): array
|
||||
{
|
||||
self::requireSelected();
|
||||
$addons = (new customer_vehicles_addons_o())->getFieldsWhere([
|
||||
'vehicle_id' => (int)$this->id,
|
||||
], [
|
||||
'id',
|
||||
]);
|
||||
return array_map(function ($addon) {
|
||||
return (new customer_vehicles_addons_o())->select($addon['id']);
|
||||
}, $addons);
|
||||
}
|
||||
|
||||
/**
|
||||
* Get the available addons for the vehicle
|
||||
* @return array{customer_vehicles_addons_o}
|
||||
* @throws Exception If the object is not selected
|
||||
*/
|
||||
public function getAvailableAddons(): array
|
||||
{
|
||||
self::requireSelected();
|
||||
$available_addons = (new product_options_o())->getProductOptions((int)$this->type->value());
|
||||
// Filter the addons, to only show the ones that has "subscription_allowed" set to true
|
||||
$available_addons = array_filter($available_addons, function ($addon) {
|
||||
return (bool)$addon['product']['subscription_allowed'];
|
||||
});
|
||||
return array_map(function ($addon) {
|
||||
return (new product_options_o())->select($addon['id']);
|
||||
}, $available_addons);
|
||||
}
|
||||
|
||||
public function structure(): void
|
||||
{
|
||||
$this->setTable('customer_vehicles');
|
||||
}
|
||||
|
||||
/**
|
||||
* @throws Exception If the creation of the object fails
|
||||
*/
|
||||
public function add(int $customer_number, int $type, string $reg, bool $subscription): void
|
||||
{
|
||||
global $db, $response;
|
||||
$tmp_id = self::add_object([
|
||||
'customer_id' => (int)$customer_number,
|
||||
'type' => (int)$type,
|
||||
'reg' => (string)$reg,
|
||||
'wash_subscription' => (bool)$subscription ? 1 : 0,
|
||||
]);
|
||||
self::select((int)$tmp_id);
|
||||
self::objectChanged();
|
||||
|
||||
}
|
||||
|
||||
public function objectChanged(): void
|
||||
{
|
||||
// Since the customer_vehicles object is not cached, there is no need to invalidate the cache
|
||||
}
|
||||
|
||||
public function add(int $customer_id, string $type, string $reg, string $notes): customer_vehicles_o
|
||||
{
|
||||
global $db, $response;
|
||||
try {
|
||||
// Avoid SQL injection
|
||||
$type = $db->escape_string($type);
|
||||
$reg = $db->escape_string($reg);
|
||||
$notes = $db->escape_string($notes);
|
||||
// Create a new record in the database
|
||||
$sql = "INSERT INTO $this->table (customer_id, type, reg, notes) VALUES ($customer_id, '$type', '$reg', '$notes')";
|
||||
$db->query($sql);
|
||||
|
||||
// Get the id of the new record
|
||||
$this->id = $db->insert_id();
|
||||
|
||||
// Set the values of the object properties
|
||||
$this->getObjectProperties();
|
||||
} catch (\Exception $e) {
|
||||
$response->error($e->getMessage());
|
||||
}
|
||||
return $this;
|
||||
}
|
||||
|
||||
public function getObjectProperties(): void
|
||||
{
|
||||
$this->customer_id = new object_property($this->table, $this->id, 'customer_id', 'int', true);
|
||||
$this->type = new object_property($this->table, $this->id, 'type', 'string', true);
|
||||
$this->type = new object_property($this->table, $this->id, 'type', 'int', true);
|
||||
$this->reg = new object_property($this->table, $this->id, 'reg', 'string', true);
|
||||
$this->notes = new object_property($this->table, $this->id, 'notes', 'string', true);
|
||||
$this->wash_subscription = new object_property($this->table, $this->id, 'wash_subscription', 'bool', false);
|
||||
$this->deleted_at = new object_property($this->table, $this->id, 'deleted_at', 'timestamp', false);
|
||||
}
|
||||
|
||||
public function getCustomerVehiclesAsArray(int $customer_id): array
|
||||
{
|
||||
global $db;
|
||||
$sql = "SELECT * FROM $this->table WHERE customer_id = $customer_id AND deleted_at IS NULL";
|
||||
$result = $db->query($sql);
|
||||
$customer_notes = [];
|
||||
if ($result->num_rows > 0) {
|
||||
while ($row = $result->fetch_assoc()) {
|
||||
$customer_notes[] = $row;
|
||||
}
|
||||
}
|
||||
return $customer_notes;
|
||||
}
|
||||
|
||||
public function getCustomerVehiclesPaginated($customer_id, $page = 1, $limit = 10): array
|
||||
{
|
||||
global /** @var response $response */
|
||||
$db, $response;
|
||||
$sql = "SELECT * FROM $this->table WHERE customer_id = $customer_id AND deleted_at IS NULL LIMIT $limit OFFSET " . ($page - 1) * $limit;
|
||||
$result = $db->query($sql);
|
||||
$array = $db->fetch_all($result);
|
||||
$sql = "SELECT COUNT(*) as count FROM $this->table WHERE customer_id = $customer_id AND deleted_at IS NULL";
|
||||
$result = $db->query($sql);
|
||||
$row = $db->fetch_assoc($result);
|
||||
$total = $row['count'];
|
||||
$response->paginate($page, $limit, $total);
|
||||
return $array;
|
||||
}
|
||||
|
||||
public function getCustomerVehicleById(int $id): customer_vehicles_o
|
||||
{
|
||||
global $db;
|
||||
// Get the record from the database
|
||||
$sql = "SELECT * FROM $this->table WHERE id = $id";
|
||||
$result = $db->query($sql);
|
||||
if ($result->num_rows > 0) {
|
||||
$this->id = $id;
|
||||
$this->getObjectProperties();
|
||||
}
|
||||
return $this;
|
||||
}
|
||||
|
||||
public function delete(int $id): void
|
||||
{
|
||||
global $db;
|
||||
$this->id = $id;
|
||||
$sql = "UPDATE $this->table SET deleted_at = NOW() WHERE id = $this->id";
|
||||
$db->query($sql);
|
||||
}
|
||||
|
||||
public function restore(int $id): void
|
||||
{
|
||||
global $db;
|
||||
$this->id = $id;
|
||||
$sql = "UPDATE $this->table SET deleted_at = NULL WHERE id = $this->id";
|
||||
$db->query($sql);
|
||||
}
|
||||
|
||||
public function getArrayByObjectProperties(): array
|
||||
{
|
||||
return [
|
||||
'id' => $this->id,
|
||||
'customer_id' => $this->customer_id->value(),
|
||||
'type' => $this->type->value(),
|
||||
'reg' => $this->reg->value(),
|
||||
'notes' => $this->notes->value(),
|
||||
'deleted_at' => $this->deleted_at->value()
|
||||
];
|
||||
}
|
||||
}
|
||||
@@ -160,6 +160,24 @@ class product_options_o extends db
|
||||
return $tmp;
|
||||
}
|
||||
|
||||
/**
|
||||
* Get the product option as an array
|
||||
* @return array
|
||||
* @throws Exception If the object was not selected
|
||||
*/
|
||||
public function asArray(): array
|
||||
{
|
||||
self::requireSelected();
|
||||
return [
|
||||
'id' => (int)$this->id,
|
||||
'product_id' => (int)$this->product_id->value(),
|
||||
'option_id' => (int)$this->option_id->value(),
|
||||
'name' => (string)$this->name->value(),
|
||||
'min' => $this->min->value() === null ? null : (int)$this->min->value(),
|
||||
'max' => $this->max->value() === null ? null : (int)$this->max->value()
|
||||
];
|
||||
}
|
||||
|
||||
/**
|
||||
* Get all products with a specific option
|
||||
* @param int $product_id The id of the addon product
|
||||
|
||||
@@ -0,0 +1,142 @@
|
||||
<?php
|
||||
|
||||
namespace routes;
|
||||
|
||||
use classes\authentication;
|
||||
use objects\customer_vehicles_addons_o;
|
||||
use objects\customer_vehicles_o;
|
||||
use objects\product_options_o;
|
||||
use traits\route_t;
|
||||
|
||||
class vehicleAddonRoute
|
||||
{
|
||||
use route_t;
|
||||
|
||||
public function run(): void
|
||||
{
|
||||
$this->get('/vehicles/addons/available', function () {
|
||||
// Require the user to be logged in
|
||||
global $response;
|
||||
$this->requirePermission('list_vehicle_addon_own');
|
||||
$user = (new authentication())->get_user();
|
||||
if (!$user) {
|
||||
$response->error('Permission denied, invalid user.', 403);
|
||||
}
|
||||
// Require the parameter vehicle_id
|
||||
self::requireParameters(['id']);
|
||||
$vehicle_id = (int)self::getParameter('id');
|
||||
self::requireMinValue($vehicle_id, 1);
|
||||
// Check if the vehicle exists
|
||||
$vehicle = (new customer_vehicles_o())->select($vehicle_id);
|
||||
if (!$vehicle->exists()) {
|
||||
$response->error('Vehicle not found.', 404);
|
||||
}
|
||||
// Check if the vehicle belongs to the user
|
||||
if ($vehicle->customer_id->value() !== $user->customer_number->value()) {
|
||||
// Check if the user has the permission to add addons to other users vehicles
|
||||
if (!$this->hasPermission('list_vehicle_addon_other')) {
|
||||
$response->error('Vehicle does not belong to the user.', 403);
|
||||
}
|
||||
}
|
||||
// Get the addons for the vehicle
|
||||
$available_addons = (new product_options_o())->getProductOptions((int)$vehicle->type->value());
|
||||
// Filter the addons, to only show the ones that has "subscription_allowed" set to true
|
||||
$available_addons = array_filter($available_addons, function ($addon) {
|
||||
return (bool)$addon['product']['subscription_allowed'];
|
||||
});
|
||||
// Get the addons for the vehicle (Currently selected)
|
||||
|
||||
$enabled_addons = (new customer_vehicles_addons_o())->getFieldsWhere([
|
||||
'vehicle_id' => $vehicle_id,
|
||||
], [
|
||||
'id',
|
||||
'addon_id',
|
||||
'amount',
|
||||
]);
|
||||
|
||||
// Create a list of available addons, with the amount of each addon
|
||||
$available_addons = array_map(function ($addon) use ($enabled_addons) {
|
||||
$addon['amount'] = 0;
|
||||
foreach ( $enabled_addons as $enabled_addon ) {
|
||||
if ((int)$enabled_addon['addon_id'] === (int)$addon['id']) {
|
||||
$addon['amount'] = (int)$enabled_addon['amount'];
|
||||
}
|
||||
}
|
||||
return $addon;
|
||||
}, $available_addons);
|
||||
// Sort the addons by name
|
||||
usort($available_addons, function ($a, $b) {
|
||||
return strcmp($a['name'], $b['name']);
|
||||
});
|
||||
// Return the addons
|
||||
$response->success(
|
||||
$available_addons,
|
||||
);
|
||||
},
|
||||
[
|
||||
'list_vehicle_addon_own' => 'List own vehicles addons',
|
||||
'list_vehicles_addon_other' => 'List other vehicles addons',
|
||||
]
|
||||
);
|
||||
|
||||
$this->post('/vehicles/addons/toggle', function () {
|
||||
// Require the user to be logged in
|
||||
global $response;
|
||||
$this->requirePermission('toggle_vehicle_addon_own');
|
||||
$user = (new authentication())->get_user();
|
||||
if (!$user) {
|
||||
$response->error('Permission denied, invalid user.', 403);
|
||||
}
|
||||
// Require the parameter vehicle_id
|
||||
self::requireParameters(['vehicle_id', 'addon_id']);
|
||||
$vehicle_id = (int)self::getParameter('vehicle_id');
|
||||
self::requireMinValue($vehicle_id, 1);
|
||||
// Check if the vehicle exists
|
||||
$vehicle = (new customer_vehicles_o())->select($vehicle_id);
|
||||
if (!$vehicle->exists()) {
|
||||
$response->error('Vehicle not found.', 404);
|
||||
}
|
||||
// Check if the vehicle belongs to the user
|
||||
if ($vehicle->customer_id->value() !== $user->customer_number->value()) {
|
||||
// Check if the user has the permission to add addons to other users vehicles
|
||||
if (!$this->hasPermission('toggle_vehicle_addon_other')) {
|
||||
$response->error('Vehicle does not belong to the user.', 403);
|
||||
}
|
||||
}
|
||||
// Get the addon id
|
||||
$addon_id = (int)self::getParameter('addon_id');
|
||||
self::requireMinValue($addon_id, 1);
|
||||
|
||||
// Check if the addon exists
|
||||
$addon = (new product_options_o())->select($addon_id);
|
||||
if (!$addon->exists()) {
|
||||
$response->error('Addon not found.', 404);
|
||||
}
|
||||
|
||||
// Check if the addon is already added to the vehicle
|
||||
$existing_addon = (new customer_vehicles_addons_o())->getFieldsWhere([
|
||||
'vehicle_id' => $vehicle_id,
|
||||
'addon_id' => $addon_id,
|
||||
], ['id']);
|
||||
|
||||
if (count($existing_addon) > 0) {
|
||||
// If it exists, remove it
|
||||
$tmp_addon = (new customer_vehicles_addons_o())->select($existing_addon[0]['id']);
|
||||
$tmp_addon->delete();
|
||||
$response->success([
|
||||
'message' => 'Addon removed from vehicle.',
|
||||
]);
|
||||
}
|
||||
// If it doesn't exist, add it
|
||||
(new customer_vehicles_addons_o())->add($vehicle_id, $addon_id);
|
||||
$response->success([
|
||||
'message' => 'Addon added to vehicle.',
|
||||
]);
|
||||
},
|
||||
[
|
||||
'toggle_vehicle_addon_own' => 'Toggle own vehicles addons',
|
||||
'toggle_vehicle_addon_other' => 'Toggle other vehicles addons',
|
||||
]
|
||||
);
|
||||
}
|
||||
}
|
||||
@@ -5,6 +5,8 @@ namespace routes;
|
||||
use classes\authentication;
|
||||
use objects\customer_vehicles_o;
|
||||
use objects\logs_o;
|
||||
use objects\products_o;
|
||||
use objects\users_o;
|
||||
use traits\route_t;
|
||||
|
||||
class vehiclesRoute
|
||||
@@ -13,7 +15,7 @@ class vehiclesRoute
|
||||
|
||||
public function run(): void
|
||||
{
|
||||
$this->get('/user/vehicles', function () {
|
||||
$this->get('/vehicles', function () {
|
||||
// Require the user to be logged in
|
||||
global $response;
|
||||
$this->requirePermission('list_own_vehicles');
|
||||
@@ -24,11 +26,24 @@ class vehiclesRoute
|
||||
// Log the incident
|
||||
(new logs_o())->add('vehicles', 'global', 1, $user->id, 'LIST_OWN_VEHICLES', 'Successfully listed own vehicles');
|
||||
// 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(
|
||||
(new customer_vehicles_o())->getCustomerVehiclesPaginated(
|
||||
$user->id,
|
||||
($this->fromRequest('page') ?? 1),
|
||||
($this->fromRequest('limit') ?? 10)
|
||||
$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 {
|
||||
@@ -39,11 +54,12 @@ class vehiclesRoute
|
||||
}
|
||||
},
|
||||
[
|
||||
'list_own_vehicles' => 'List own vehicles'
|
||||
'list_own_vehicles' => 'List own vehicles',
|
||||
'list_vehicles_other' => 'List other users vehicles',
|
||||
]
|
||||
);
|
||||
|
||||
$this->post('/user/vehicles', function () {
|
||||
$this->post('/vehicles', function () {
|
||||
// Require the user to be logged in
|
||||
global $response;
|
||||
$this->requirePermission('add_vehicle');
|
||||
@@ -51,27 +67,65 @@ class vehiclesRoute
|
||||
$user = (new authentication())->get_user();
|
||||
// Check if the request was successful
|
||||
if ($user) {
|
||||
// Get the post data
|
||||
$data = json_decode(file_get_contents('php://input'), true);
|
||||
// Check if the required fields are set
|
||||
$data = $this->getData($data, $response);
|
||||
// Make sure the registration number is valid
|
||||
$this->validateRegistrationNumber($data['reg'], $response);
|
||||
// Make sure the type is valid
|
||||
$this->validateType($data['type'], $response);
|
||||
// Make sure the notes are valid
|
||||
$this->validateNotes($data['notes'], $response);
|
||||
// Create a new vehicle
|
||||
$vehicle = (new customer_vehicles_o())->add(
|
||||
$user->id,
|
||||
$data['type'],
|
||||
$data['reg'],
|
||||
$data['notes']
|
||||
// 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'));
|
||||
}
|
||||
}
|
||||
}
|
||||
// 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
|
||||
);
|
||||
|
||||
// Log the incident
|
||||
(new logs_o())->add('vehicles', 'global', 1, $user->id, 'ADD_VEHICLE', 'Successfully added vehicle');
|
||||
// Return the new vehicle
|
||||
$response->success($vehicle->getArrayByObjectProperties());
|
||||
$response->success(
|
||||
$vehicle->asArray()
|
||||
);
|
||||
} else {
|
||||
// Log the incident
|
||||
(new logs_o())->add('vehicles', 'global', 1, 0, 'ADD_VEHICLE', 'No user found, or invalid session');
|
||||
@@ -80,7 +134,149 @@ class vehiclesRoute
|
||||
}
|
||||
},
|
||||
[
|
||||
'add_vehicle' => 'Add a vehicle to own vehicles'
|
||||
'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 ($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, 1);
|
||||
// Make sure the type is a valid type
|
||||
$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
|
||||
);
|
||||
}
|
||||
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());
|
||||
// Set the wash subscription
|
||||
$vehicle->wash_subscription->set($subscription ? 1 : 0);
|
||||
}
|
||||
// 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 ($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'
|
||||
]
|
||||
);
|
||||
}
|
||||
|
||||
@@ -96,6 +96,21 @@ trait route_t
|
||||
return 'Y-m-d';
|
||||
}
|
||||
|
||||
/**
|
||||
* TYPE_BOOL
|
||||
* @note This is used to check if the value is a boolean, this is used in routes to validate input
|
||||
* @return string
|
||||
*/
|
||||
public function TYPE_BOOL(): string
|
||||
{
|
||||
return 'boolean';
|
||||
}
|
||||
|
||||
/**
|
||||
* @param string $date
|
||||
* @param string $format
|
||||
* @return void
|
||||
*/
|
||||
public function requireDateFormat(string $date, string $format): void
|
||||
{
|
||||
global $response;
|
||||
|
||||
Reference in New Issue
Block a user