Add admin endpoint for fetching customer names and enhance order booking object with parsed product names
- Introduced `GET /admin/customer/name` endpoint in `userRoute` to retrieve customer names by user ID. - Enhanced `order_bookings_o` with product name parsing for booking items. - Added `customer_name` retrieval to `asArray` method in `order_bookings_o`. - Implemented private `parseProductNames` method for item details parsing in `order_bookings_o`.
This commit is contained in:
@@ -228,6 +228,9 @@ class order_bookings_o extends db
|
||||
}
|
||||
|
||||
|
||||
/**
|
||||
* @throws Exception
|
||||
*/
|
||||
public function asArray(): array
|
||||
{
|
||||
$order_id = $this->order_id->value();
|
||||
@@ -243,13 +246,38 @@ class order_bookings_o extends db
|
||||
'reference' => $this->reference->value(),
|
||||
'po' => $this->po->value(),
|
||||
'pickup' => (bool)$this->pickup->value(),
|
||||
'items' => $this->items->value(),
|
||||
'items' => $this->parseProductNames($this->items->value()),
|
||||
'order_id' => $order_id === null ? null : (int)$order_id,
|
||||
'customer_name' => (new users_o())->getUserByCustomerNumber((int)$this->customer_number->value())->getCustomerName((int)$this->customer_number->value()),
|
||||
'created_at' => $this->created_at->value(),
|
||||
'updated_at' => $this->updated_at->value(),
|
||||
];
|
||||
}
|
||||
|
||||
/**
|
||||
* @throws Exception
|
||||
*/
|
||||
private function parseProductNames(array $items): array
|
||||
{
|
||||
$parsedItems = [];
|
||||
foreach ($items as $item) {
|
||||
if (!isset($item['id']) || !isset($item['quantity'])) {
|
||||
continue;
|
||||
}
|
||||
$product = (new products_o())->select((int)$item['id']);
|
||||
if (!$product->exists()) {
|
||||
continue;
|
||||
}
|
||||
$parsedItems[] = [
|
||||
...$item,
|
||||
'id' => (int)$product->id,
|
||||
'name' => (string)$product->name->value(),
|
||||
'quantity' => (int)$item['quantity'],
|
||||
];
|
||||
}
|
||||
return $parsedItems;
|
||||
}
|
||||
|
||||
/**
|
||||
* @throws Exception
|
||||
*/
|
||||
|
||||
@@ -191,6 +191,42 @@ class userRoute
|
||||
]
|
||||
);
|
||||
|
||||
$this->get('/admin/customer/name', function () {
|
||||
// Require the user to be logged in
|
||||
global $response;
|
||||
$this->requirePermission('get_user_name');
|
||||
// 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_NAME', 'Successfully fetched user name');
|
||||
$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_NAME', 'No user found');
|
||||
// Return an error
|
||||
$response->error('User not found', 404);
|
||||
}
|
||||
// Return the list of users
|
||||
$response->success(
|
||||
[
|
||||
'name' => (string)$targetUser->getCustomerName((int)$targetUser->customer_number->value())
|
||||
]
|
||||
);
|
||||
} else {
|
||||
// Log the incident
|
||||
(new logs_o())->add('users', 'global', 1, 0, 'GET_USER_NAME', 'No user found, or invalid session');
|
||||
// Return an error
|
||||
$response->error('Invalid session', 400);
|
||||
}
|
||||
},
|
||||
[
|
||||
'get_user_name' => 'Get user full name from user ID.'
|
||||
]
|
||||
);
|
||||
|
||||
$this->get('/superuser/user/keys', function () {
|
||||
// Require the user to be logged in
|
||||
global $response;
|
||||
|
||||
Reference in New Issue
Block a user