diff --git a/services/nginx/app/objects/order_bookings_o.php b/services/nginx/app/objects/order_bookings_o.php index c2051f1c..ca42d969 100644 --- a/services/nginx/app/objects/order_bookings_o.php +++ b/services/nginx/app/objects/order_bookings_o.php @@ -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 */ diff --git a/services/nginx/app/routes/userRoute.php b/services/nginx/app/routes/userRoute.php index b74c0509..a447c849 100644 --- a/services/nginx/app/routes/userRoute.php +++ b/services/nginx/app/routes/userRoute.php @@ -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;