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.
This commit is contained in:
Jepp9350
2025-03-05 11:54:17 +01:00
parent a02729d5c6
commit 0d87b7acdd
2 changed files with 50 additions and 0 deletions
@@ -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
@@ -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'
]
);
}
}