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:
@@ -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'
|
||||
]
|
||||
);
|
||||
}
|
||||
}
|
||||
Reference in New Issue
Block a user