Add new endpoints and support for fetching user keys

Introduced functionality to fetch all user keys with associated data. Implemented new endpoints for deleting user bookings and retrieving user IDs, alongside permission checks and validation. Minor adjustments made for better error handling and improved debug logging behavior.
This commit is contained in:
Jepp9350
2025-02-07 10:33:41 +01:00
parent 588c9d68ef
commit f8cd736154
6 changed files with 96 additions and 4 deletions
@@ -53,8 +53,8 @@ class economic_customer_mo
if ($user->exists()) {
$user->cache('economic_customer', $this);
}
// Add the customer to the debug log
$response->add_debug_list('economic_customer', $this->customer_number, $customer);
// Add the customer to the debug log (IF DEBUG IS ENABLED)
//$response->add_debug_list('economic_customer', $this->customer_number, $customer);
return $this;
}
@@ -74,4 +74,18 @@ class user_key_value_pairs_o extends db
return $this;
}
public function getAllKeys(): array
{
global $db;
// Get all the keys from the database
$sql = "SELECT var, val FROM $this->table WHERE user_id = $this->user_id";
$result = $db->query($sql);
$result = $db->fetch_all($result);
$keys = [];
foreach ( $result as $row ) {
$keys[$row['var']] = $row['val'];
}
return $keys;
}
}
+17
View File
@@ -25,6 +25,7 @@ class users_o extends db
public array $attributes;
public array $discounts;
public array $orders_not_invoiced;
public array $all_keys;
public customer_codes_o $customer_codes;
public user_key_value_pairs_o $keys;
public user_price_overrides_o $price_overrides;
@@ -255,6 +256,10 @@ class users_o extends db
if (isset($this->orders_not_invoiced)) {
$array['orders_not_invoiced'] = $this->orders_not_invoiced;
}
// If the keys are set, add them to the array
if (isset($this->all_keys)) {
$array['keys'] = $this->all_keys;
}
return $array;
}
@@ -459,6 +464,12 @@ class users_o extends db
if ($includeEverything || $response->getRequestParameter('includeOrdersNotInvoiced') === 'true' || in_array('ordersNotInvoiced', $includes)) {
$this->getUserOrdersNotInvoiced();
}
/**
* Key value pairs
*/
if ($includeEverything || $response->getRequestParameter('includeKeys') === 'true' || in_array('keys', $includes)) {
$this->getAllKeys();
}
return $this;
}
@@ -506,6 +517,12 @@ class users_o extends db
$this->orders_not_invoiced = $db->fetch_all($result);
}
public function getAllKeys(): void
{
// Get all keys (key = 'key_value_pair')
$this->all_keys = $this->keys->setUser($this->id)->getAllKeys();
}
public function getCode(): string|null
{
// Get the customer code
@@ -281,5 +281,34 @@ class bookingsRoute
["message" => "Wash completed without wash certificate"]
);
});
$this->post('/user/bookings/delete', function () {
// Require the user to be logged in
global /** @var response $response */
$response;
$this->requirePermission('delete_own_booking');
// Get the user object
$user = (new authentication())->get_user();
// Check if the request was successful
if (!$user->exists()) {
$response->error('User not found', 400);
}
// Check if the required fields are set
$id = $response->getRequestParameter('id');
// Make sure the id is a number
if (!is_numeric($id)) {
$response->error('id parameter must be a number got: ' . $id, 400);
}
// Make sure the user is allowed to delete the booking
if (!$user->hasAccessToBooking($id)) {
$response->error('You are not allowed to delete this booking', 400);
}
// Delete the booking
(new bookings_o())->delete($id);
// Return success
$response->success(
["message" => "Booking deleted"]
);
});
}
}
@@ -313,12 +313,12 @@ class economicInvoiceRoute
// Get the department
$department = $order->getDepartmentByOrderId($order->id);
$economic_department_id = $department['economic_department_id'];
$economic_dimension_id = $department['economic_dimension_id'];
$economic_dimension_id = $department['economic_dimension_id'] ?? 0;
// Add the line to the invoice
$economic_invoice_draft->addLine(
(string)$order_item['product']['economic_product_id'],
(string)$order_item['product']['name'] . ' #' . $order_item['id'],
(string)$order_item['product']['name'],
(int)$quantity,
(int)$order_item['price'],
0, // Since we can't be specific about the discount, we set it to 0. (The API has a limit of 2 decimals, and that's not enough for our needs)
+32
View File
@@ -132,5 +132,37 @@ class userRoute
$response->error('Invalid session', 400);
}
});
$this->get('/admin/customer/getUserId', function () {
// Require the user to be logged in
global $response;
$this->requirePermission('get_user_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('users', 'global', 1, $user->id, 'GET_USER_ID', 'Successfully fetched user id');
$targetUser = (new users_o())->automaticGetTargetUserFromRequest();
// Check if the user was found
if (!$targetUser->exists()) {
// Log the incident
(new logs_o())->add('users', 'global', 1, $user->id, 'GET_USER_ID', 'No user found');
// Return an error
$response->error('User not found', 404);
}
// Return the list of users
$response->success(
[
'user_id' => $targetUser->id
]
);
} else {
// Log the incident
(new logs_o())->add('users', 'global', 1, 0, 'GET_USER_ID', 'No user found, or invalid session');
// Return an error
$response->error('Invalid session', 400);
}
});
}
}