v2/examples/05-swarm-apps/rest-api-advanced/README-PRODUCTS-ORDERS.md
This REST API now includes a complete product and order management system with advanced features for e-commerce applications.
src/models/product.model.js){
name: String,
slug: String,
description: String,
price: Number,
comparePrice: Number,
category: String,
subcategory: String,
brand: String,
sku: String,
inventory: {
quantity: Number,
trackInventory: Boolean,
allowBackorder: Boolean,
lowStockThreshold: Number
},
images: [{
url: String,
alt: String,
position: Number,
isMain: Boolean
}],
reviews: [{
user: ObjectId,
rating: Number,
comment: String,
isVerifiedPurchase: Boolean,
helpful: { yes: Number, no: Number }
}],
rating: {
average: Number,
count: Number,
distribution: Object
},
tags: [String],
specifications: [{ key: String, value: String }],
status: String,
featured: Boolean,
relatedProducts: [ObjectId]
}
src/models/order.model.js){
orderNumber: String,
user: ObjectId,
items: [{
product: ObjectId,
name: String,
price: Number,
quantity: Number,
subtotal: Number
}],
status: String,
totalAmount: Number,
shippingAddress: Object,
billingAddress: Object,
payment: {
method: String,
status: String,
transactionId: String
},
tracking: {
carrier: String,
number: String,
url: String,
deliveredAt: Date
},
history: [{ status: String, timestamp: Date }]
}
GET /api/products - Search and list products with filtersGET /api/products/:id - Get product detailsPOST /api/products - Create new product (admin)PUT /api/products/:id - Update product (admin)DELETE /api/products/:id - Delete product (admin)GET /api/products/category/:category - Get products by categoryGET /api/products/featured - Get featured productsGET /api/products/popular - Get popular productsPOST /api/products/:id/reviews - Add product reviewPUT /api/products/:id/inventory - Update inventory (admin)GET /api/products/inventory/report - Get inventory report (admin)GET /api/orders - Get user ordersGET /api/orders/:id - Get order detailsPOST /api/orders - Create new orderPUT /api/orders/:id/status - Update order status (admin)DELETE /api/orders/:id - Cancel orderPOST /api/orders/:id/tracking - Add tracking info (admin)POST /api/orders/:id/refund - Process refund (admin)GET /api/orders/statistics/summary - Get order statisticsGET /api/orders/reports/sales - Get sales report (admin)GET /api/orders/:id/invoice - Get order invoicePopulate your database with sample data:
# Seed all data
npm run seed
# Seed only products
npm run seed:products
# Seed only orders (requires products)
npm run seed:orders
GET /api/products?q=wireless&category=Electronics&minPrice=50&maxPrice=200&sort=-rating
POST /api/orders
{
"items": [
{
"product": "65abc123...",
"quantity": 2
}
],
"shippingAddress": {
"fullName": "John Doe",
"phone": "+1234567890",
"email": "[email protected]",
"street": "123 Main St",
"city": "New York",
"state": "NY",
"zipCode": "10001",
"country": "USA"
},
"paymentMethod": "credit_card"
}
POST /api/products/:id/reviews
{
"rating": 5,
"comment": "Excellent product! Highly recommended.",
"images": ["https://example.com/review1.jpg"]
}
PUT /api/products/:id/inventory
{
"quantity": 50,
"operation": "set" // or "increment", "decrement"
}
src/services/product.service.js)src/services/order.service.js)All endpoints include comprehensive validation:
Inventory Management
Review System
Order Processing
Analytics
The system includes comprehensive test coverage:
Run tests with:
npm test
Add these to your .env file:
# Redis (for caching)
REDIS_URL=redis://localhost:6379
# Payment Gateway (when implementing)
STRIPE_SECRET_KEY=your_stripe_key
PAYPAL_CLIENT_ID=your_paypal_id
# Shipping (when implementing)
SHIPPING_API_KEY=your_shipping_key