# Car Rental Admin System — Server Deployment Guide

This repository contains a full-stack Car Rental application built with **React / Vite** (Frontend) and **Node.js / Express** (Backend API), utilizing a **PostgreSQL** database.

---

## 📁 Deployment Package Structure

```text
car/
├── client/
│   ├── src/
│   ├── public/
│   ├── package.json
│   ├── package-lock.json
│   ├── vite.config.js
│   ├── .env.development
│   └── .env.production
├── server/
│   ├── controllers/
│   ├── middleware/
│   ├── routes/
│   ├── uploads/
│   ├── index.js
│   ├── db.js
│   ├── package.json
│   ├── package-lock.json
│   └── .env.example
├── car-1.sql
└── README.md
```

---

## 🗄️ PostgreSQL Database Setup (`car-1.sql`)

The repository includes the complete database dump in `car-1.sql`.

### Step 1: Log in to PostgreSQL
```bash
sudo -u postgres psql
```

### Step 2: Create the Database & User
```sql
CREATE DATABASE car_rental;
CREATE USER car_user WITH PASSWORD 'your_secure_password';
GRANT ALL PRIVILEGES ON DATABASE car_rental TO car_user;
ALTER DATABASE car_rental OWNER TO car_user;
\q
```

### Step 3: Grant Schema Permissions on PostgreSQL 15+
```bash
sudo -u postgres psql -d car_rental -c "GRANT ALL ON SCHEMA public TO car_user;"
```

### Step 4: Import Database Dump (`car-1.sql`)
Import the `car-1.sql` schema into your database:
```bash
psql -U car_user -d car_rental -h 127.0.0.1 -f car-1.sql
```

### Step 5: Verify Tables Created
```bash
psql -U car_user -d car_rental -h 127.0.0.1 -c "\dt"
```
Expected tables: `booking_audits`, `booking_payments`, `bookings`, `cars`, `customers`, `users`, etc.

---

## ⚙️ Backend Setup & Configuration

### 1. Install Backend Dependencies
```bash
cd server
npm install
```

### 2. Configure Backend `.env`
Create `server/.env` based on `server/.env.example`:
```bash
cp .env.example .env
```
Edit `server/.env`:
```env
PORT=5004
DB_HOST=127.0.0.1
DB_PORT=5432
DB_NAME=car_rental
DB_USER=car_user
DB_PASSWORD=your_secure_password
JWT_SECRET=your_super_secret_jwt_key
EMAIL_USER=your_email@gmail.com
EMAIL_PASSWORD=your_email_app_password
FRONTEND_URL=https://invictustec.co.in/car
```

### 3. Test Database Connection
```bash
node test_conn.js
```
*Expected Output:* `Successfully connected to PostgreSQL`

### 4. Start Backend with PM2
```bash
pm2 start index.js --name car-backend
pm2 save
pm2 startup
```

#### Useful PM2 Commands:
```bash
pm2 status
pm2 logs car-backend
pm2 restart car-backend
pm2 stop car-backend
```

---

## 💻 Frontend Build & Setup

### 1. Install Frontend Dependencies
```bash
cd client
npm install
```

### 2. Frontend Environment Configuration
- `.env.development` handles local development (`VITE_API_BASE_URL=http://localhost:5004/api`, `VITE_SERVER_URL=http://localhost:5004`).
- `.env.production` handles production deployment (`VITE_API_BASE_URL=/car/api`, `VITE_SERVER_URL=/car`).

### 3. Build Production Bundle
```bash
npm run build
```
This generates compiled production assets under `client/dist/`.

---

## 🌐 Apache Web Server Configuration

To host the application under `https://invictustec.co.in/car/`:

### Apache VirtualHost Snippet (`/etc/apache2/sites-available/invictustec.conf`):

```apache
<VirtualHost *:443>
    ServerName invictustec.co.in
    DocumentRoot /var/www/html

    SSLEngine on
    SSLCertificateFile /etc/letsencrypt/live/invictustec.co.in/fullchain.pem
    SSLCertificateKeyFile /etc/letsencrypt/live/invictustec.co.in/privkey.pem

    # 1. Frontend Client Application SPA Routing
    Alias /car /var/www/html/car/client/dist
    <Directory /var/www/html/car/client/dist>
        Options Indexes FollowSymLinks
        AllowOverride All
        Require all granted

        # SPA Fallback for React Router deep links (/car/login, /car/dashboard, etc.)
        FallbackResource /car/index.html
    </Directory>

    # 2. Uploaded Media Directory Serving
    Alias /car/uploads /var/www/html/car/server/uploads
    <Directory /var/www/html/car/server/uploads>
        Options -Indexes +FollowSymLinks
        AllowOverride None
        Require all granted
    </Directory>

    # 3. Backend Express API Reverse Proxy
    ProxyPreserveHost On
    ProxyPass /car/api http://127.0.0.1:5004/api
    ProxyPassReverse /car/api http://127.0.0.1:5004/api

</VirtualHost>
```

### Enable Modules & Reload Apache:
```bash
sudo a2enmod proxy proxy_http rewrite
sudo systemctl restart apache2
```

---

## 🚀 Complete Step-by-Step Deployment Procedure

- **Step 1:** Upload and unzip project folder to `/var/www/html/car`.
- **Step 2:** Navigate to `client/` and run `npm install`.
- **Step 3:** Verify `client/.env.production` contains `VITE_API_BASE_URL=/car/api` and `VITE_SERVER_URL=/car`.
- **Step 4:** Run `npm run build` in `client/` to generate `client/dist/`.
- **Step 5:** Navigate to `server/` and run `npm install`.
- **Step 6:** Create and configure `server/.env` with database and port configuration.
- **Step 7:** Create PostgreSQL database `car_rental` and database user.
- **Step 8:** Import schema dump using `psql -U car_user -d car_rental -f car-1.sql`.
- **Step 9:** Verify table creation using `psql -U car_user -d car_rental -c "\dt"`.
- **Step 10:** Start backend service using `pm2 start index.js --name car-backend`.
- **Step 11:** Add Apache VirtualHost configuration for `/car`, `/car/api`, and `/car/uploads`.
- **Step 12:** Test and reload Apache (`sudo apache2ctl configtest && sudo systemctl reload apache2`).
- **Step 13:** Test frontend in browser: `https://invictustec.co.in/car/`.
- **Step 14:** Test API endpoint: `https://invictustec.co.in/car/api/`.
- **Step 15:** Test admin login on `/car/login`.
- **Step 16:** Test uploading a car image and verifying public URL `/car/uploads/filename`.
- **Step 17:** Test creating a car booking and viewing dashboard statistics.
