From 0d87b7acdda4e87152db3e1c1e79b3abe360b14c Mon Sep 17 00:00:00 2001 From: Jepp9350 <2jepp9350@gmail.com> Date: Wed, 5 Mar 2025 11:54:17 +0100 Subject: [PATCH] Add product option deletion functionality Implemented endpoint and logic for deleting product options, ensuring user authentication, parameter validation, and proper logging. Added a delete method in the product options object to permanently remove records. --- .../nginx/app/objects/product_options_o.php | 12 ++++++ .../nginx/app/routes/productOptionsRoute.php | 38 +++++++++++++++++++ 2 files changed, 50 insertions(+) diff --git a/services/nginx/app/objects/product_options_o.php b/services/nginx/app/objects/product_options_o.php index d77b9808..55c665fc 100644 --- a/services/nginx/app/objects/product_options_o.php +++ b/services/nginx/app/objects/product_options_o.php @@ -50,6 +50,18 @@ class product_options_o extends db //TODO: Add cache invalidation } + /** + * There's no need to keep track of when a product option is deleted, + * so we just forcefully delete it. + * @throws Exception If the object was not selected + * @throws Exception If the object was not deleted successfully + */ + public function delete(): void + { + self::requireSelected(); + self::deletePermanently(); + } + /** * Set the name of the product option * @param string $name diff --git a/services/nginx/app/routes/productOptionsRoute.php b/services/nginx/app/routes/productOptionsRoute.php index fcd5ee76..98d9c97c 100644 --- a/services/nginx/app/routes/productOptionsRoute.php +++ b/services/nginx/app/routes/productOptionsRoute.php @@ -135,5 +135,43 @@ class productOptionsRoute 'edit_product_option' => 'Edit a product option' ] ); + + $this->delete('/product/options', function () { + // Require the user to be logged in + global $response; + $this->requirePermission('delete_product_option'); + // Get the user object + $user = (new authentication())->get_user(); + // Check if the request was successful + if ($user) { + // Check if the required parameters are set + self::requireParameters(['id']); + // Check if the parameters are of the correct type + self::requireType((int)self::getParameter('id'), self::TYPE_INT()); + // Require the minimum value to be above 0 + self::requireMinValue((int)self::getParameter('id'), 1); + // Create the object + $product_options = new product_options_o(); + // Select the object + $product_options->select((int)self::getParameter('id')); + // Delete the object + $product_options->delete(); + // Log the incident + (new logs_o())->add('product_options', 'global', 1, $user->id, 'DELETE_PRODUCT_OPTION', 'User deleted a product option'); + // Return the object + $response->success( + 'Product option deleted' + ); + } else { + // Log the incident + (new logs_o())->add('product_options', 'global', 1, 0, 'DELETE_PRODUCT_OPTION', 'User tried to delete a product option without being logged in'); + // Return an error + $response->error('Invalid session', 400); + } + }, + [ + 'delete_product_option' => 'Delete a product option' + ] + ); } } \ No newline at end of file