- Name: Luis Martinez
- Clan: NodeJS/NestJS
REST API built with Node.js, Express, TypeScript, JWT, and Sequelize (PostgreSQL) for managing delivery orders for FHL logistics company. The system handles the complete lifecycle of delivery orders, from client registration to order tracking and warehouse management.
- Node.js (v18+)
- TypeScript
- Express.js - Web framework
- PostgreSQL - Relational database
- Sequelize - ORM
- JWT - Authentication
- Bcrypt - Password hashing
- Swagger - API documentation
- Nodemon - Development server
- User registration with two roles: administrator and analyst
- JWT-based authentication
- Protected routes based on user roles
- Full CRUD operations on clients, warehouses, products, and orders
- Can activate/deactivate warehouses
- Complete order management
- View orders
- Update order status (pending, in_transit, delivered)
- Read-only access to other resources
- Register clients with ID card, name, email, and delivery address
- List all clients
- Search client by ID card
- Prevent duplicate ID card registration
- Activate/deactivate warehouses
- List active warehouses with stock information
- Manage warehouse inventory
- CRUD operations for products
- Search products by code
- Logical deletion (soft delete)
- Stock management per warehouse
- Create orders with products from specific warehouses
- Validate stock availability before order creation
- Update order status
- Track order history
- Filter orders by client
- Node.js >= 18.x
- PostgreSQL >= 12.x
- npm or yarn
git clone <repository-url>
cd "Order Management System"npm installCreate a .env file in the root directory based on .env.example:
# PostgreSQL Configuration
PGHOST=localhost
PGPORT=5432
PGDATABASE=deliverydb
PGUSER=postgres
PGPASSWORD=your_secure_password_here
# MongoDB Configuration (optional)
MONGO_URI=mongodb://localhost:27017/deliverydb
# JWT Configuration
JWT_SECRET=change_this_to_a_super_secure_secret_in_production
JWT_EXPIRES_IN=24h
# Server Configuration
PORT=3001# Connect to PostgreSQL
psql -U postgres
# Create database
CREATE DATABASE deliverydb;
# Exit psql
\qAfter configuring the environment, install the required packages:
npm installPopulate the database with initial data (users, clients, warehouses, products):
npm run seedThis will create:
- 2 users (admin and analyst)
- 5 test clients
- 4 warehouses
- 10 products with stock distribution
npm run startThe API will be available at: http://localhost:3001
Important: Make sure PostgreSQL is running before starting the application.
If you prefer to use Docker, you can deploy the entire stack with one command:
# Copy Docker environment file
cp .env.docker .env
# Start all services (PostgreSQL, MongoDB, API)
docker compose up -d
# Run seeders
docker compose exec api npm run seed:force
# View logs
docker compose logs -f apiThe API will be available at: http://localhost:3001
- PostgreSQL - Database (port 5432)
- MongoDB - Logs (port 27017)
- API - Node.js application (port 3001)
# Stop all services
docker compose down
# Restart API only
docker compose restart api
# View all containers
docker compose ps
# Access API container
docker compose exec api shFor complete Docker documentation, see DOCKER_GUIDE.md
Once the server is running, access the interactive Swagger documentation at:
http://localhost:3001/api-docs
A complete Postman collection is available in the postman/ directory:
- FHL_Order_Management_System.postman_collection.json - Complete API collection
- FHL_Environment.postman_environment.json - Environment variables
- Import both files into Postman
- Select the "FHL Order Management - Local" environment
- Run "Login Admin" or "Login Analyst" to get authentication tokens
- Tokens are automatically saved and used in subsequent requests
See postman/README.md for detailed instructions.
- users - System users (admin, analyst)
- clients - Delivery clients
- warehouses - Storage facilities
- products - Product catalog
- warehouse_products - Stock per warehouse
- orders - Delivery orders
- order_items - Products in each order
- Orders belong to Clients
- Orders contain multiple OrderItems
- OrderItems reference Products and Warehouses
- Products have stock in multiple Warehouses (through warehouse_products)
POST /auth/register- Register new userPOST /auth/login- User login
GET /clients- List all clientsPOST /clients/search- Search client by ID cardPUT /clients/:id- Update client (Admin only)DELETE /clients/:id- Delete client (Admin only)
GET /warehouses/active- List active warehouses with stockPUT /warehouses/:id/activate- Activate warehouse (Admin only)PUT /warehouses/:id/deactivate- Deactivate warehouse (Admin only)
GET /products- List all active products (Public)GET /products/:id- Get product by ID (Public)GET /products/code/:code- Get product by code (Public)POST /products- Create product (Admin only)PUT /products/:id- Update product (Admin only)DELETE /products/:id- Logical delete product (Admin only)
GET /orders- List orders (filtered by role)GET /orders/history- Get complete order historyGET /orders/:id- Get order by IDPOST /orders- Create new orderPUT /orders/:id/status- Update order status (Admin/Analyst)
After running seeders, the database will contain:
- Admin: admin@fhl.com / password: admin123
- Analyst: analyst@fhl.com / password: analyst123
- Multiple test clients with valid ID cards and delivery addresses
- Central Warehouse (Active)
- North Warehouse (Active)
- South Warehouse (Active)
- Various products with stock distributed across warehouses
The system includes the following validations:
- Stock Validation: Orders cannot be created if there's insufficient stock in the selected warehouse
- Unique ID Card: Clients cannot be registered with duplicate ID cards
- Valid Order Status: Only valid status transitions are allowed (pending → in_transit → delivered)
- Role-based Access: Endpoints are protected based on user roles
# Run tests
npm test
# Run tests in watch mode
npm run test:watchOrder Management System/
├── backend/
│ └── src/
│ ├── config/ # Configuration files (Swagger)
│ ├── controllers/ # Request handlers
│ ├── database/ # Seeders
│ ├── interfaces/ # TypeScript interfaces
│ ├── jobs/ # Cron jobs
│ ├── middleware/ # Auth & validation middleware
│ ├── models/ # Sequelize models
│ ├── routes/ # API routes
│ ├── services/ # Business logic
│ ├── tests/ # Unit tests
│ ├── types/ # Type definitions
│ ├── utils/ # Helper functions
│ └── app.ts # Application entry point
├── frontend/ # Frontend (reserved for future implementation)
├── postman/ # Postman collection & environment
│ ├── FHL_Order_Management_System.postman_collection.json
│ ├── FHL_Environment.postman_environment.json
│ └── README.md
├── scripts/ # Utility scripts
│ └── generate-dump.sh # SQL dump generator
├── .env # Environment variables (not in repo)
├── .env.example # Environment template
├── package.json # Dependencies
├── tsconfig.json # TypeScript configuration
├── QUICK_START.md # Quick setup guide
├── IMPLEMENTATION_SUMMARY.md # Implementation checklist
└── README.md # This file
This project follows Git Flow with conventional commits:
main- Production-ready codedevelop- Development branchfeature/*- Feature branches
feat: Add new feature
fix: Fix bug
docs: Update documentation
style: Code style changes
refactor: Code refactoring
test: Add tests
chore: Maintenance tasks
To generate a SQL dump with the current database structure and data:
# Using the provided script
./scripts/generate-dump.sh
# Or manually
npm run db:dumpThis will create a file at database/dump.sql with:
- Complete database structure (tables, constraints, indexes)
- All seeded data
- Clean and recreate commands
To restore the database from the dump file:
psql -U postgres < database/dump.sqlOr if the database already exists:
psql -U postgres -d deliverydb < database/dump.sqlFor issues or questions, please contact the development team.
ISC