A robust and secure backend API for managing vehicle rentals, built with Node.js, TypeScript, Express, and PostgreSQL.
- Root - https://vehicle-rental-system-backend-six.vercel.app/
- Version - https://vehicle-rental-system-backend-six.vercel.app/api/v1/
- Overview
- Features
- Technology Stack
- Project Structure
- Prerequisites
- Installation
- Environment Configuration
- Database Setup
- Running the Application
- API Documentation
- Authentication
- Business Logic
- Scripts
- Contributing
The Vehicle Rental System is a comprehensive backend solution that enables:
- Vehicle Management - CRUD operations for vehicle inventory with real-time availability tracking
- User Management - Role-based access control for admins and customers
- Booking System - Complete rental lifecycle management with automated pricing and vehicle status updates
- Authentication & Authorization - Secure JWT-based authentication with role-based permissions
- JWT-based authentication
- Password hashing with bcryptjs
- Role-based access control (Admin/Customer)
- Protected routes with middleware validation
- Add, view, update, and delete vehicles
- Real-time availability tracking
- Multiple vehicle types (Car, Bike, Van, SUV)
- Daily rental pricing
- User registration and authentication
- Profile management
- Admin controls for user management
- Deletion protection (users with bookings cannot be deleted)
- Create bookings with automatic price calculation
- Vehicle availability validation
- Booking cancellation (before start date)
- Admin marking bookings as returned
- Automated booking returns via cron job (runs daily)
- Role-based booking views (admin sees all, customers see own)
- Automatic booking status updates when rental period ends
- Vehicle availability auto-updates
- Daily cron job for expired booking cleanup
| Technology | Purpose |
|---|---|
| Node.js | Runtime environment |
| TypeScript | Type-safe development |
| Express.js | Web framework |
| PostgreSQL | Relational database |
| bcryptjs | Password hashing |
| jsonwebtoken | JWT authentication |
| Zod | Schema validation |
| node-cron | Scheduled tasks |
| pg | PostgreSQL client |
| tsx | TypeScript execution |
βββ π src
β βββ π config
β β βββ π db.ts
β β βββ π index.ts
β βββ π middleware
β β βββ π verifyRoles.ts
β βββ π modules
β β βββ π auth
β β β βββ π auth.constant.ts
β β β βββ π auth.controller.ts
β β β βββ π auth.routes.ts
β β β βββ π auth.service.ts
β β β βββ π auth.validation.ts
β β βββ π bookings
β β β βββ π booking.validation.ts
β β β βββ π bookings.controller.ts
β β β βββ π bookings.routes.ts
β β β βββ π bookings.service.ts
β β βββ π jobs
β β β βββ π autoReturnBookings.ts
β β βββ π users
β β β βββ π users.controller.ts
β β β βββ π users.routes.ts
β β β βββ π users.service.ts
β β β βββ π users.validation.ts
β β βββ π vehicles
β β βββ π vehicles.controller.ts
β β βββ π vehicles.routes.ts
β β βββ π vehicles.service.ts
β β βββ π vehicles.validation.ts
β βββ π types
β β βββ π express
β β βββ π index.d.ts
β βββ π app.ts
β βββ π server.ts
βββ βοΈ .gitignore
βββ π README.md
βββ βοΈ package-lock.json
βββ βοΈ package.json
βββ βοΈ tsconfig.json
Before you begin, ensure you have the following installed:
- Node.js (v18 or higher)
- PostgreSQL (v14 or higher)
- npm or yarn
- Clone the repository
git clone <repository-url>
cd vehicle_rental_system_backend- Install dependencies
npm installCreate a .env file in the root directory:
# Server Configuration
PORT=5000
NODE_ENV=development
# Database Configuration
PG_CONNECTION_STR=postgresql://username:password@localhost:5432/vehicle_rental_db
# JWT Configuration
JWT_SECRET=your_super_secret_jwt_key_here_change_in_productionThe application automatically creates all necessary tables and ENUM types on startup. The database schema includes:
- users - User accounts with role-based access
- vehicles - Vehicle inventory with availability tracking
- bookings - Rental records with pricing and status
user_role: 'admin', 'customer'vehicle_type: 'car', 'bike', 'van', 'SUV'booking_status: 'active', 'cancelled', 'returned'status: 'available', 'booked'
Manual Database Creation:
# Create database
psql -U postgres
CREATE DATABASE vehicle_rental_db;
\qThe application will automatically create all tables when it starts.
npm run devThe server will start on http://localhost:5000 with hot-reload enabled.
npm run build
npm startcurl http://localhost:5000Expected response:
{
"message": "Welcome to vehicle rental system by TajUddin",
"status": "running",
"timestamp": "2024-01-15T10:30:00.000Z"
}http://localhost:5000/api/v1
| Method | Endpoint | Access | Description |
|---|---|---|---|
| POST | /auth/signup |
Public | Register new user |
| POST | /auth/signin |
Public | Login and get JWT token |
| Method | Endpoint | Access | Description |
|---|---|---|---|
| POST | /vehicles |
Admin | Create new vehicle |
| GET | /vehicles |
Public | Get all vehicles |
| GET | /vehicles/:vehicleId |
Public | Get vehicle by ID |
| PUT | /vehicles/:vehicleId |
Admin | Update vehicle |
| DELETE | /vehicles/:vehicleId |
Admin | Delete vehicle |
| Method | Endpoint | Access | Description |
|---|---|---|---|
| GET | /users |
Admin | Get all users |
| PUT | /users/:userId |
Admin/Own | Update user |
| DELETE | /users/:userId |
Admin | Delete user |
| Method | Endpoint | Access | Description |
|---|---|---|---|
| POST | /bookings |
Customer/Admin | Create booking |
| GET | /bookings |
Role-based | Get bookings (filtered by role) |
| PUT | /bookings/:bookingId |
Role-based | Update booking status |
curl -X POST http://localhost:5000/api/v1/auth/signup \
-H "Content-Type: application/json" \
-d '{
"name": "John Doe",
"email": "john@example.com",
"password": "password123",
"phone": "01712345678",
"role": "customer"
}'curl -X POST http://localhost:5000/api/v1/auth/signin \
-H "Content-Type: application/json" \
-d '{
"email": "john@example.com",
"password": "password123"
}'curl -X GET http://localhost:5000/api/v1/bookings \
-H "Authorization: Bearer YOUR_JWT_TOKEN_HERE"total_price = daily_rent_price Γ number_of_days
number_of_days = rent_end_date - rent_start_date- Booking Created β Vehicle status:
"booked" - Booking Returned β Vehicle status:
"available" - Booking Cancelled β Vehicle status:
"available"
- Cron job runs daily at 12:01 AM
- Automatically marks bookings as
"returned"whenrent_end_datehas passed - Updates vehicle availability accordingly
- Logs all operations for monitoring
- Users cannot be deleted if they have active bookings
- Vehicles cannot be deleted if they have active bookings
- Active bookings = status is
"active"
- Admin:
- Full system access
- Manage all vehicles, users, and bookings
- Mark bookings as returned
- Customer:
- Create and view own bookings
- Cancel bookings before start date
- Update own profile
| Command | Description |
|---|---|
npm run dev |
Start development server with hot-reload |
npm run build |
Compile TypeScript to JavaScript |
npm start |
Run production server |
npm test |
Run test suite (not implemented yet) |
# 1. Register a user
curl -X POST http://localhost:5000/api/v1/auth/signup \
-H "Content-Type: application/json" \
-d '{"name":"John","email":"john@test.com","password":"123456","phone":"01712345678","role":"customer"}'
# 2. Login
curl -X POST http://localhost:5000/api/v1/auth/signin \
-H "Content-Type: application/json" \
-d '{"email":"john@test.com","password":"123456"}'
# 3. Create booking (use token from step 2)
curl -X POST http://localhost:5000/api/v1/bookings \
-H "Content-Type: application/json" \
-H "Authorization: Bearer YOUR_TOKEN" \
-d '{
"customer_id": 1,
"vehicle_id": 1,
"rent_start_date": "2024-12-15",
"rent_end_date": "2024-12-20"
}'The API uses standard HTTP status codes:
| Code | Meaning | Example |
|---|---|---|
| 200 | Success | Resource retrieved/updated |
| 201 | Created | New resource created |
| 400 | Bad Request | Validation error, business rule violation |
| 401 | Unauthorized | Missing/invalid token |
| 403 | Forbidden | Insufficient permissions |
| 404 | Not Found | Resource doesn't exist |
| 500 | Server Error | Unexpected error |
{
"success": false,
"message": "Error description",
"errors": ["Detailed error messages"]
}The application logs important events:
- β Database initialization
- β Cron job execution
- β Auto-return operations
- β Error logs with timestamps
Example logs:
Database initialized successfully!
β
Cron jobs initialized
[2024-12-07T00:01:00.000Z] Running auto-return job...
β
Auto-returned 3 booking(s)
- Create feature module in
src/modules/ - Follow the pattern:
routes β controller β service - Add validation with Zod
- Update this README
- Use TypeScript strict mode
- Follow modular architecture
- Use async/await for async operations
- Handle errors with try-catch
- Validate all inputs with Zod
This project is licensed under the ISC License.
TajUddin
- Express.js team for the excellent framework
- PostgreSQL community
- TypeScript team
- All open-source contributors
For issues, questions, or contributions, please:
- Check existing documentation
- Review API Reference
- Open an issue on GitHub
- Contact the development team
Happy Coding! π