Authentication
Requires JWT authentication with admin role . This endpoint uses the @Roles('admin') decorator.
Path Parameters
Transaction ID to delete Must be a valid integer (validated by IdValidationPipe)
Response
curl -X DELETE https://api.example.com/transactions/1 \
-H "Authorization: Bearer ADMIN_TOKEN"
Success Response
Error - Not Found
Error - Forbidden (Non-Admin)
Error - Product Not Found
Error - Invalid ID Format
{
"message" : "sale successfully deleted"
}
Authorization
Admin Role Required
This endpoint is protected by the @Roles('admin') decorator and can only be accessed by users with admin privileges:
// In the controller
@ Delete ( ':id' )
@ Roles ( 'admin' ) // Only admins can delete transactions
remove (@ Param ( 'id' , IdValidationPipe ) id : string ) {
return this . transactionsService . remove ( + id );
}
Access Control
User Role Access Admin ✓ Allowed Regular User ✗ Forbidden (403) Unauthenticated ✗ Unauthorized (401)
Behavior
Inventory Restoration
When a transaction is deleted, the following operations occur:
Fetch Transaction : Retrieves the transaction with all line items (contents)
Restore Inventory : For each line item:
Finds the associated product
Restores inventory: product.inventory += quantity
Saves the updated product
Delete Contents : Removes all transaction line items
Delete Transaction : Removes the transaction record
Example Inventory Restoration
Before deletion:
// Transaction contains:
[
{ "productId" : 1 , "quantity" : 2 }, // Wireless Mouse
{ "productId" : 5 , "quantity" : 1 } // USB Cable
]
// Product inventory:
// Product 1: inventory = 48
// Product 5: inventory = 199
After deletion:
// Product inventory restored:
// Product 1: inventory = 50 (48 + 2)
// Product 5: inventory = 200 (199 + 1)
Error Handling
The deletion process validates:
Transaction exists : Returns 404 if transaction not found
Products exist : Returns 404 if any product in the transaction line items is not found
Valid ID format : Returns 400 if ID is not a valid integer
Data Integrity
The deletion process ensures:
All transaction contents are removed
Product inventory is correctly restored for all items
If a product is not found during restoration, an error is thrown and the operation fails
Use Cases
When to Delete Transactions
Correcting accidental sales
Handling returns or refunds
Removing test transactions
Fixing data entry errors
Important Considerations
Deleting a transaction is a destructive operation that:
Permanently removes the transaction record
Restores product inventory levels
Cannot be undone
Consider implementing a “void” or “refund” status instead of deletion for audit trail purposes.
Unlike the GET and LIST endpoints, the DELETE operation does not check if the transaction belongs to the current user. Any admin can delete any transaction.
The IdValidationPipe ensures the ID parameter is a valid numeric string before processing the deletion.
ID Validation
The IdValidationPipe ensures that:
The ID parameter is a numeric string
The ID can be converted to a valid integer
Invalid formats (e.g., “abc”, “1.5”) are rejected with a 400 error
DELETE /transactions/1 # ✓ Valid
DELETE /transactions/42 # ✓ Valid
DELETE /transactions/1000 # ✓ Valid
DELETE /transactions/abc # ✗ Invalid (not numeric)
DELETE /transactions/1.5 # ✗ Invalid (not an integer)
DELETE /transactions/-1 # ✗ Invalid (negative)