Skip to main content
DELETE
/
transactions
/
{id}
curl -X DELETE https://api.example.com/transactions/1 \
  -H "Authorization: Bearer ADMIN_TOKEN"
{
  "message": "sale successfully deleted"
}

Authentication

Requires JWT authentication with admin role. This endpoint uses the @Roles('admin') decorator.

Path Parameters

id
number
required
Transaction ID to deleteMust be a valid integer (validated by IdValidationPipe)

Response

message
string
Success message
curl -X DELETE https://api.example.com/transactions/1 \
  -H "Authorization: Bearer ADMIN_TOKEN"
{
  "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 RoleAccess
Admin✓ Allowed
Regular User✗ Forbidden (403)
Unauthenticated✗ Unauthorized (401)

Behavior

Inventory Restoration

When a transaction is deleted, the following operations occur:
  1. Fetch Transaction: Retrieves the transaction with all line items (contents)
  2. Restore Inventory: For each line item:
    • Finds the associated product
    • Restores inventory: product.inventory += quantity
    • Saves the updated product
  3. Delete Contents: Removes all transaction line items
  4. 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

Valid ID Formats

DELETE /transactions/1      # ✓ Valid
DELETE /transactions/42     # ✓ Valid
DELETE /transactions/1000   # ✓ Valid

Invalid ID Formats

DELETE /transactions/abc    # ✗ Invalid (not numeric)
DELETE /transactions/1.5    # ✗ Invalid (not an integer)
DELETE /transactions/-1     # ✗ Invalid (negative)

Build docs developers (and LLMs) love