From 8ae3a35a91a9b0dfb3fc7e79c047a6215365cab5 Mon Sep 17 00:00:00 2001 From: Jepp9350 <2jepp9350@gmail.com> Date: Wed, 30 Apr 2025 14:37:53 +0200 Subject: [PATCH] Add endpoints for unknown customer vehicles and suggestions Introduced two new endpoints: one for listing unknown customer vehicles and another for fetching customer suggestions based on a vehicle registration. These changes include authentication, input validation, and proper logging for each new route. --- services/nginx/app/routes/vehiclesRoute.php | 93 +++++++++++++++++++++ 1 file changed, 93 insertions(+) diff --git a/services/nginx/app/routes/vehiclesRoute.php b/services/nginx/app/routes/vehiclesRoute.php index ad000aa7..8ddcae1b 100644 --- a/services/nginx/app/routes/vehiclesRoute.php +++ b/services/nginx/app/routes/vehiclesRoute.php @@ -5,6 +5,7 @@ namespace routes; use classes\authentication; 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; @@ -59,6 +60,37 @@ class vehiclesRoute ] ); + $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; @@ -279,6 +311,67 @@ class vehiclesRoute '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, + ]; + }, $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', + ] + ); } private function getData(mixed $data, $response)