Skip to content

CodeWithIsmail/Ride-Sharing-System

Repository files navigation

Ride Sharing System

A modern, microservice-based ride-sharing application built with React, Node.js, Express, MongoDB, and Docker. This system enables passengers to post ride requests and drivers to apply for rides, with integrated payment processing and admin oversight.

πŸ—οΈ System Architecture

The application follows a microservice architecture with the following services:

Backend Services

  • User Service (Port 3001): User authentication, registration, and profile management
  • Ride Service (Port 3002): Ride request management, driver applications, and ride lifecycle
  • Payment Service (Port 3003): Cash payment processing and receipt generation
  • Admin Service (Port 3004): Administrative functions for user and ride oversight

Frontend

  • React Application (Port 3000): Modern UI built with React and Tailwind CSS

Database

  • MongoDB: Document-based database with separate collections for each service

πŸš€ Quick Start with Docker

Prerequisites

  • Docker and Docker Compose installed
  • Git

Installation & Setup

  1. Clone the repository

    git clone <repository-url>
    cd Ride-Sharing-System
  2. Start all services with Docker Compose

    # For Windows PowerShell
    .\docker-setup.ps1
    
    # For Linux/Mac
    ./docker-setup.sh
    
    # Or manually
    docker-compose up -d
  3. Access the application

πŸ› οΈ Manual Setup (Development)

Prerequisites

  • Node.js (v14 or higher)
  • MongoDB (running locally or MongoDB Atlas)
  • npm or yarn

Installation Steps

  1. Install dependencies for all services

    npm run install-all
  2. Set up environment variables

    Create .env files in each service directory:

    User Service (backend/user-service/.env):

    PORT=3001
    MONGODB_URI=mongodb://localhost:27017/ride-sharing-users
    JWT_SECRET=your-super-secret-jwt-key-change-in-production

    Ride Service (backend/ride-service/.env):

    PORT=3002
    MONGODB_URI=mongodb://localhost:27017/ride-sharing-rides
    USER_SERVICE_URL=http://localhost:3001

    Payment Service (backend/payment-service/.env):

    PORT=3003
    MONGODB_URI=mongodb://localhost:27017/ride-sharing-payments
    RIDE_SERVICE_URL=http://localhost:3002
    USER_SERVICE_URL=http://localhost:3001

    Admin Service (backend/admin-service/.env):

    PORT=3004
    MONGODB_URI=mongodb://localhost:27017/ride-sharing-admin
    USER_SERVICE_URL=http://localhost:3001
    RIDE_SERVICE_URL=http://localhost:3002
  3. Start MongoDB

    # Local MongoDB
    mongod
    
    # Or use MongoDB Atlas (cloud)
  4. Run the application

    # Start all services
    npm run dev

πŸ“± Application Features

User Roles

  • Passenger: Can post ride requests, select drivers, and make payments
  • Driver: Can browse available rides, apply for rides, and complete trips
  • Admin: Can manage users, monitor rides, and oversee system operations

Core Workflows

Passenger Workflow

  1. Registration & Login: Create account and authenticate
  2. Post Ride Request: Specify pickup/dropoff locations, time, and fare
  3. Review Applications: View driver applications for their ride
  4. Select Driver: Choose a driver from applications
  5. Complete Ride: Ride is completed by selected driver
  6. Payment: Record cash payment and generate receipt

Driver Workflow

  1. Registration & Login: Create account with phone number
  2. Browse Rides: View available ride requests
  3. Apply for Rides: Submit applications for desired rides
  4. Complete Rides: Mark selected rides as completed
  5. Receive Payment: Collect cash payment from passenger

Admin Workflow

  1. User Management: View all users and deactivate accounts
  2. Ride Monitoring: Track ride requests and completion rates
  3. System Oversight: Monitor overall system performance

πŸ”Œ Complete API Documentation

User Service APIs (Port 3001)

Authentication Endpoints

POST /api/auth/register

  • Description: Register a new user
  • Request Body:
    {
      "email": "user@example.com",
      "password": "password123",
      "name": "John Doe",
      "role": "passenger|driver|admin",
      "phone": "+1234567890" // Required for drivers
    }
  • Response: 201 - User created successfully

POST /api/auth/login

  • Description: Authenticate user and get JWT token
  • Request Body:
    {
      "email": "user@example.com",
      "password": "password123"
    }
  • Response: 200 - Returns JWT token and user details

POST /api/auth/logout

  • Description: Logout user and invalidate session
  • Headers: Authorization: Bearer <token>
  • Response: 200 - Logout successful

User Management Endpoints

GET /api/users/verify

  • Description: Verify JWT token and get user details
  • Headers: Authorization: Bearer <token>
  • Response: 200 - User details

GET /api/users/:userId

  • Description: Get user details by ID
  • Headers: Authorization: Bearer <token>
  • Response: 200 - User profile (without password)

GET /health

  • Description: Health check endpoint
  • Response: 200 - Service status

Ride Service APIs (Port 3002)

Ride Request Management

POST /api/rides

  • Description: Create a new ride request (Passengers only)
  • Headers: Authorization: Bearer <token>
  • Request Body:
    {
      "pickupLocation": "Shahbag, Dhaka",
      "dropoffLocation": "Dhanmondi, Dhaka",
      "targetTime": "2024-01-15T10:00:00.000Z",
      "desiredFare": 150
    }
  • Response: 201 - Ride request created

GET /api/rides

  • Description: Get available ride requests (Drivers only)
  • Headers: Authorization: Bearer <token>
  • Query Parameters: status=posted|confirmed|completed|cancelled
  • Response: 200 - List of ride requests

Driver Applications

POST /api/rides/:rideRequestId/apply

  • Description: Apply for a ride request (Drivers only)
  • Headers: Authorization: Bearer <token>
  • Response: 201 - Application submitted

GET /api/rides/:rideRequestId/applications

  • Description: Get applications for a ride request (Passenger only)
  • Headers: Authorization: Bearer <token>
  • Response: 200 - List of driver applications

Ride Management

POST /api/rides/:rideRequestId/select

  • Description: Select a driver for ride (Passenger only)
  • Headers: Authorization: Bearer <token>
  • Request Body:
    {
      "driverId": "driver_user_id"
    }
  • Response: 200 - Driver selected

POST /api/rides/:rideRequestId/cancel

  • Description: Cancel a ride request
  • Headers: Authorization: Bearer <token>
  • Response: 200 - Ride cancelled

POST /api/rides/:rideRequestId/complete

  • Description: Mark ride as completed (Driver only)
  • Headers: Authorization: Bearer <token>
  • Response: 200 - Ride completed

Payment Service APIs (Port 3003)

Payment Processing

POST /api/payments/:rideRequestId

  • Description: Record cash payment for completed ride
  • Headers: Authorization: Bearer <token>
  • Request Body:
    {
      "amount": 150
    }
  • Response: 201 - Payment recorded

POST /api/payments/:paymentId/receipt

  • Description: Generate digital receipt for payment
  • Headers: Authorization: Bearer <token>
  • Response: 200 - Receipt generated

Admin Service APIs (Port 3004)

User Management

GET /api/admin/users

  • Description: Get all users (Admin only)
  • Headers: Authorization: Bearer <token>
  • Response: 200 - List of all users

PATCH /api/admin/users/:userId/deactivate

  • Description: Deactivate a user account (Admin only)
  • Headers: Authorization: Bearer <token>
  • Response: 200 - User deactivated

Ride Monitoring

GET /api/admin/rides

  • Description: Get all rides with optional status filter (Admin only)
  • Headers: Authorization: Bearer <token>
  • Query Parameters: status=posted|confirmed|completed|cancelled
  • Response: 200 - List of all rides

πŸ—„οΈ Database Schema

Users Collection

{
  "_id": ObjectId,
  "email": String, // Unique, lowercase
  "password": String, // Hashed with bcrypt
  "name": String,
  "role": String, // "passenger", "driver", "admin"
  "phone": String, // Required for drivers
  "isActive": Boolean, // For admin deactivation
  "createdAt": Date
}

Sessions Collection

{
  "_id": ObjectId,
  "userId": ObjectId, // Reference to Users
  "token": String, // JWT token
  "createdAt": Date,
  "expiresAt": Date // TTL index for automatic cleanup
}

RideRequests Collection

{
  "_id": ObjectId,
  "passengerId": ObjectId, // Reference to Users
  "pickupLocation": String,
  "dropoffLocation": String,
  "targetTime": Date,
  "desiredFare": Number,
  "status": String, // "posted", "confirmed", "completed", "cancelled"
  "driverId": ObjectId, // Reference to Users (null until selected)
  "createdAt": Date
}

RideApplications Collection

{
  "_id": ObjectId,
  "rideRequestId": ObjectId, // Reference to RideRequests
  "driverId": ObjectId, // Reference to Users
  "appliedAt": Date
}

Payments Collection

{
  "_id": ObjectId,
  "rideRequestId": ObjectId, // Reference to RideRequests
  "amount": Number,
  "paymentMethod": String, // "cash" for MVP
  "status": String, // "pending", "completed"
  "receiptSentAt": Date, // When receipt was generated
  "createdAt": Date
}

πŸ”§ Development

Running Individual Services

# User Service
cd backend/user-service
npm run dev

# Ride Service
cd backend/ride-service
npm run dev

# Payment Service
cd backend/payment-service
npm run dev

# Admin Service
cd backend/admin-service
npm run dev

# Frontend
cd frontend
npm start

Docker Commands

# Build all services
docker-compose build

# Start all services
docker-compose up -d

# View logs
docker-compose logs -f

# Stop all services
docker-compose down

# Rebuild and restart specific service
docker-compose up --build user-service

Database Access

# Connect to MongoDB container
docker exec -it ride-sharing-mongodb mongosh -u admin -p password123

# List databases
show dbs

# Use specific database
use ride-sharing-users

πŸ§ͺ Testing

API Testing with Postman

  1. Import the provided Postman collection

  2. Set up environment variables:

    • base_url: http://localhost:3001
    • ride_service_url: http://localhost:3002
    • payment_service_url: http://localhost:3003
    • admin_service_url: http://localhost:3004
  3. Test workflow:

    • Register users (passenger, driver, admin)
    • Login to get JWT tokens
    • Test complete ride flow
    • Test payment processing
    • Test admin functions

Manual Testing

# Test health endpoints
curl http://localhost:3001/health
curl http://localhost:3002/health
curl http://localhost:3003/health
curl http://localhost:3004/health

# Test user registration
curl -X POST http://localhost:3001/api/auth/register \
  -H "Content-Type: application/json" \
  -d '{"email":"test@example.com","password":"password123","name":"Test User","role":"passenger"}'

πŸ”’ Security Features

  • JWT Authentication: Secure token-based authentication
  • Password Hashing: Bcrypt password encryption
  • Role-based Access Control: Different permissions for different user roles
  • Input Validation: Express-validator for request validation
  • CORS Protection: Cross-origin resource sharing configuration
  • Session Management: Automatic session cleanup with TTL indexes

πŸš€ Deployment

Production Environment Variables

# MongoDB
MONGODB_URI=mongodb://username:password@host:port/database

# JWT
JWT_SECRET=your-super-secure-production-secret-key

# Service URLs (for production)
USER_SERVICE_URL=https://user-service.yourdomain.com
RIDE_SERVICE_URL=https://ride-service.yourdomain.com

Docker Production Build

# Build production images
docker-compose -f docker-compose.prod.yml build

# Deploy to production
docker-compose -f docker-compose.prod.yml up -d

πŸ› Troubleshooting

Common Issues

  1. Port already in use

    # Check what's using the port
    lsof -i :3000
    
    # Stop conflicting services
    docker-compose down
  2. MongoDB connection issues

    # Check MongoDB logs
    docker-compose logs mongodb
    
    # Restart MongoDB
    docker-compose restart mongodb
  3. Service not starting

    # Check all logs
    docker-compose logs
    
    # Rebuild specific service
    docker-compose up --build user-service

Health Checks

Each service includes health check endpoints:

  • User Service: GET /health
  • Ride Service: GET /health
  • Payment Service: GET /health
  • Admin Service: GET /health

🀝 Contributing

  1. Fork the repository
  2. Create a feature branch (git checkout -b feature/amazing-feature)
  3. Commit your changes (git commit -m 'Add some amazing feature')
  4. Push to the branch (git push origin feature/amazing-feature)
  5. Open a Pull Request

πŸ“„ License

This project is licensed under the MIT License - see the LICENSE file for details.

πŸ†˜ Support

For support and questions:

  • Open an issue in the repository
  • Check the troubleshooting section
  • Review the API documentation

πŸ“Š System Requirements

  • Node.js: v14 or higher
  • MongoDB: v4.4 or higher
  • Docker: v20.10 or higher (for containerized deployment)
  • RAM: Minimum 4GB (8GB recommended)
  • Storage: 2GB free space

πŸ”„ Version History

  • v1.0.0: Initial release with basic ride-sharing functionality
  • v1.1.0: Added payment processing and receipt generation
  • v1.2.0: Enhanced admin dashboard and user management
  • v1.3.0: Docker containerization and production deployment support