A robust backend API for syncing WooCommerce products and orders with automated scheduling and comprehensive data management.
- WooCommerce Integration: Seamless sync with WooCommerce REST API
- Automated Scheduling: Daily cron jobs for data synchronization
- Data Management: Intelligent product and order management with cleanup
- RESTful API: Well-structured endpoints for frontend integration
- Error Handling: Comprehensive error handling and logging
- Testing: Unit tests with Jest and Supertest
- Security: Rate limiting, CORS, and security headers
- Performance: Optimized database queries and caching strategies
- Node.js - Runtime environment
- Express.js - Web framework
- MongoDB - Database (Native driver, no Mongoose)
- Jest - Testing framework
- Winston - Logging
- Joi - Validation
- Cron - Scheduled tasks
src/
├── config/
│ ├── database.ts # MongoDB connection setup
│ └── woocommerce.ts # WooCommerce API client setup
│
├── constant/
│ ├── cors.constant.ts # CORS-related constants
│ └── logger.constant.ts # Logger and message constants
│
├── controller/
│ ├── order.controller.ts # Order controller
│ ├── products.controller.ts # Product controller
│ └── sync.controller.ts # WooCommerce sync controller
│
├── middleware/
│ ├── error-handler.middleware.ts # Global error handler
│ ├── rate-limit.middleware.ts # Rate limiting middleware
│ └── validate-query.middleware.ts # Query validation middleware
│
├── routes/
│ └── v1/
│ ├── index.ts # Base router for all v1 endpoints
│ ├── order.routes.ts # Routes for order endpoints
│ ├── products.routes.ts # Routes for product endpoints
│ └── sync.routes.ts # Routes for sync operations
│
├── services/
│ ├── order.services.ts # Order business logic
│ ├── product.services.ts # Product business logic
│ └── sync.services.ts # Sync orchestration logic
│
├── types/
│ ├── logger.types.ts # Logger-related types
│ ├── order.types.ts # Order interfaces/types
│ ├── product.types.ts # Product interfaces/types
│ ├── query-params.types.ts # Query params validation types
│ └── sync.types.ts # Types related to syncing logic
│
├── utils/
│ └── logger.utils.ts # Custom logger utility
│
├── validations/
│ └── schema.ts # Zod validation schemas
│
├── app.ts # Express app configuration
└── server.ts # Server startup script
-
Clone the repository
git clone <repository-url> cd woocommerce-sync-backend
-
Install dependencies
npm install
-
Environment Setup
cp .env.example .env # Edit .env with your configuration
-
Start MongoDB
# Using Docker docker run -d -p 27017:27017 --name mongodb mongo:latest # Or use MongoDB Atlas (recommended for production)
-
Run the application
# Development npm run dev # Production npm start
Variable | Description | Default |
---|---|---|
NODE_ENV |
Environment | development |
PORT |
Server port | 5000 |
DB_NAME |
Database name | nodejstuts |
WOOCOMMERCE_API_BASE_URL |
WooCommerce store URL | https://interview-test.matat.io |
WOOCOMMERCE_CONSUMER_KEY |
WooCommerce API key | Required |
WOOCOMMERCE_CONSUMER_SECRET |
WooCommerce API secret | Required |
SYNC_CRON_SCHEDULE |
Cron schedule for sync | 0 12 * * * |
ORDER_RETENTION_DAYS |
Order retention period | 90 |
ORDER_FETCH_DAYS |
Order fetch period | 30 |
GET /api/v1/orders
- Get paginated orders- Query params:
page
,limit
,search
,status
,sort
,order
- Query params:
GET /api/v1/orders/:id
- Get specific orderGET /api/v1/orders/product/:productId
- Get orders for specific product
GET /api/v1/products
- Get paginated products- Query params:
page
,limit
,search
,sort
,order
- Query params:
GET /api/v1/products/:id
- Get specific product
GET /api/v1/sync/status
- Get sync statusPOST /api/v1/sync/trigger
- Trigger manual sync
Before proceeding, make sure the API is running — since Render uses a free tier, the backend may go to sleep or be temporarily unavailable.
-
🌐 Live Health Check URL:
https://woocommerce-sync-backend.onrender.com/health -
📥 Health Check Endpoint:
GET /api/v1/health
— Returns a status response to confirm the WooCommerce Sync API is live.
# Run all tests
npm test
# Run tests in watch mode
npm run test:watch
# Run tests with coverage
npm run test:coverage
{
id: Number, // WooCommerce order ID
number: String, // Order number
order_key: String, // Order key
status: String, // Order status
date_created: Date, // Order creation date
total: Number, // Order total
customer_id: Number, // Customer ID
customer_note: String, // Customer note
billing: Object, // Billing information
shipping: Object, // Shipping information
line_items: Array, // Order line items
updatedAt: Date, // Last update timestamp
syncedAt: Date // Last sync timestamp
}
{
id: Number, // WooCommerce product ID
name: String, // Product name
sku: String, // Product SKU
price: Number, // Current price
regular_price: Number, // Regular price
sale_price: Number, // Sale price
description: String, // Product description
short_description: String, // Short description
images: Array, // Product images
stock_quantity: Number, // Stock quantity
in_stock: Boolean, // Stock status
updatedAt: Date, // Last update timestamp
syncedAt: Date // Last sync timestamp
}
The API implements comprehensive error handling:
- Validation Errors: 400 Bad Request
- Not Found: 404 Not Found
- Conflict: 409 Conflict (duplicate entries)
- Rate Limiting: 429 Too Many Requests
- Server Errors: 500 Internal Server Error
All errors are logged with Winston and include:
- Error message and stack trace
- Request information (URL, method, IP)
- Timestamp and severity level
Logs are written to:
logs/error.log
- Error level logslogs/combined.log
- All logs- Console output in development
- Database Indexing: Optimized indexes for search and filtering
- Connection Pooling: MongoDB connection pooling
- Compression: Gzip compression for responses
- Rate Limiting: Prevents API abuse
- Caching: Intelligent product caching to avoid duplicates
- Helmet: Security headers
- CORS: Cross-origin resource sharing
- Rate Limiting: Request rate limiting
- Input Validation: Joi validation for all inputs
- Error Sanitization: Sanitized error responses
The application is ready for deployment on platforms like:
- Heroku
- AWS EC2/ECS
- Google Cloud Platform
- DigitalOcean
- Database: Use MongoDB Atlas for production
- Environment: Set
NODE_ENV=production
- Process Management: Use PM2 for process management
- Monitoring: Implement health checks and monitoring
- Backup: Regular database backups
- Rate Limiting: WooCommerce API rate limits may affect large syncs
- Memory Usage: Large datasets may require memory optimization
- Timezone: All dates are stored in UTC
- Fork the repository
- Create a feature branch
- Add tests for new features
- Run the test suite
- Submit a pull request
MIT License - see LICENSE file for details.
👉 Full Project Description: Check out the docs/overview.md file for detailed information.