Overview
The Orders API handles order creation, tracking, status management, and PDF report generation. It includes automatic stock validation and email notifications. Base URL:/api/pedidos
Source: PedidoController.java
Authentication
- Customer endpoints: Create, view own orders - requires authentication
- Admin endpoints: List all, update status, delete, PDF reports - requires
ROLE_ADMIN
Order States
Orders progress through the following states:PENDIENTE- Order placed, awaiting paymentPAGADO- Payment confirmedENVIADO- Order shippedENTREGADO- Order deliveredCANCELADO- Order cancelled (stock restored)
Endpoints
Create Order
User object with
id fieldArray of order detail objects
Shipping address (optional, can use user’s default address)
cURL
201 Created
Order ID
User object
Calculated order total (sum of precio * cantidad for all details)
Order status (initially “PENDIENTE”)
Timestamp of order creation
Array of order detail objects with product, cantidad, precio
400 Bad Request- Stock insufficient or user not found500 Internal Server Error- Server error
The
createPedido service method automatically:- Validates stock availability for each product
- Deducts ordered quantities from product stock
- Calculates the total amount
- Sets order state to “PENDIENTE”
Get Order by ID
Order ID
200 OK- Order object with user, detalles, and calculated total404 Not Found- Order does not exist
List Orders by User
User ID
200 OK - Array of order objects
Example Request:
cURL
List All Orders (Admin)
ROLE_ADMIN
Response: 200 OK - Array of all order objects
Use Case: Admin dashboard to view and manage all customer orders.
Update Order Status
ROLE_ADMIN
Order ID
New order status:
PENDIENTE, PAGADO, ENVIADO, ENTREGADO, or CANCELADO200 OK - Updated order object
Example Request:
cURL
Generate PDF Report
ROLE_ADMIN
Request Body: Array of order objects (filtered by date, status, etc.)
Response: 200 OK - Binary PDF file
Headers:
Content-Type: application/pdfContent-Disposition: attachment; filename="reporte-ventas.pdf"
JavaScript
The PDF generation uses the OpenPDF library (version 1.3.30). The report includes order details, totals, and summary statistics.
Delete Order
ROLE_ADMIN
Order ID
200 OK- “Pedido eliminado correctamente”500 Internal Server Error- “Error al eliminar el pedido”
Order Workflow
Customer Creates Order
Customer adds items to cart and proceeds to checkout. The cart is converted to an order via
POST /api/pedidos.Stock Validation
System validates that sufficient stock exists for each product. If stock is insufficient, the order is rejected with a
400 error.Order Processing
Admin reviews the order and updates status:
PENDIENTE → PAGADO → ENVIADO → ENTREGADO.Integration Examples
Customer: Place Order
JavaScript
Admin: Update Order Status
JavaScript
Error Handling
| Status Code | Scenario | Solution |
|---|---|---|
400 Bad Request | Insufficient stock | Reduce order quantity or restock product |
400 Bad Request | User not found | Verify user ID exists |
401 Unauthorized | Missing/invalid token | Re-authenticate user |
403 Forbidden | Non-admin accessing admin endpoints | Check user role |
404 Not Found | Order does not exist | Verify order ID |
500 Internal Server Error | Database or service error | Check server logs |
Related Endpoints
Cart API
Convert cart to order
Products API
Manage product stock
Users API
Manage customer accounts