/orders/create
🔐 Requiere autenticación JWT.
POST /api/orders/create
bulkWrite
y se guarda la orden. En caso de fallos tras el descuento, se revierte el stock.verificarAutenticacion
Authorization: Bearer <token>
req.SellerBDD
).verificarAutenticacion
validateCreateOrder
validateRequest
application/json
){
"customer": 12345678901,
"products": [
{
"productId": "101",
"quantity": 5
}
],
"discountApplied": 10.0,
"netTotal": 100.0,
"totalWithTax": 118.0,
"comment": "Entrega en 48 horas"
}
Campo | Tipo | Requerido | Descripción |
---|---|---|---|
customer | number | ✅ | RUC del cliente |
products | array | ✅ | Productos a incluir (con productId y quantity ) |
discountApplied | number | ✅ | Descuento aplicado (≥ 0) |
netTotal | number | ✅ | Total sin impuestos (≥ 0) |
totalWithTax | number | ✅ | Total con impuestos (≥ 0) |
comment | string | ❌ | Comentario adicional |
{
"status": "success",
"code": "ORDER_CREATED",
"msg": "Orden creada con éxito y stock actualizado.",
"data": {
"_id": "ObjectId",
"customer": 12345678901,
"products": [
{
"productId": "101",
"quantity": 5
}
],
"discountApplied": 10.0,
"netTotal": 100.0,
"totalWithTax": 118.0,
"status": "Pendiente",
"comment": "Entrega en 48 horas",
"registrationDate": "2025-04-24T10:30:00Z",
"lastUpdate": "2025-04-24T10:30:00Z",
"seller": "ObjectId"
},
"info": {
"stockUpdateDetails": {
"attempted": 1,
"modified": 1,
"status": "Éxito",
"message": "Stock actualizado correctamente"
}
}
}
{
"status": "error",
"code": "INVALID_FORMAT",
"msg": "Campos requeridos: customer (RUC), products (array no vacío), discountApplied, netTotal, totalWithTax.",
"errors": [
{
"field": "products[0].quantity",
"msg": "La cantidad debe ser mayor que 0."
}
]
}
{
"status": "error",
"code": "INSUFFICIENT_STOCK",
"msg": "Stock insuficiente para el producto 101. Stock actual: 3, Cantidad solicitada: 5"
}
{
"status": "error",
"code": "AUTH_ERROR",
"msg": "Acceso no autorizado. Se requiere token válido."
}
{
"status": "error",
"code": "NOT_FOUND",
"msg": "Cliente con RUC 12345678901 no encontrado."
}
{
"status": "error",
"code": "NOT_FOUND",
"msg": "Producto con ID 101 no encontrado."
}
{
"status": "error",
"code": "STOCK_CONFLICT",
"msg": "Conflicto de stock: Se intentaron actualizar 2 productos, pero solo 1 se modificaron. Inténtalo de nuevo.",
"info": {
"attempted": 2,
"modified": 1,
"status": "Conflicto",
"message": "No se logró modificar el stock de algunos productos.",
"reversion": "Reversión de stock completada con éxito"
}
}
{
"status": "error",
"code": "SERVER_ERROR",
"msg": "Ha ocurrido un error inesperado al registrar la orden. Intente de nuevo más tarde.",
"info": {
"detail": "Error de conexión con la base de datos",
"reversionStatus": "Intentando revertir stock..."
}
}
curl --location --request POST 'https://back-prima-s-a.onrender.com/api/orders/create' \
--header 'Authorization: Bearer eyJhbGciOiJIUzI1NiIsInR5cCI6IkpXVCJ9.eyJpZCI6IjY3NWNkZGEzMTJkYmM5YjM2NzkwNDliNiIsInJvbCI6IlNlbGxlciIsImlhdCI6MTczODE4OTczOCwiZXhwIjoxNzM4MjE4NTM4fQ.pqhvNo0o_kP3oK8hrJsRhdIPoXUnReaZT-x3_FVN6HA' \
--header 'Content-Type: application/json' \
--data-raw '{
"customer": 1767890123001,
"products": [
{
"productId": 55055012,
"quantity": 1
},
{
"productId": 55055025,
"quantity": 5
}
],
"discountApplied": 10.5,
"netTotal": 7.79,
"totalWithTax": 10.67
}'
{}