# LeadExpo Backend API - Docs by Jhems

Node.js + Express + MongoDB backend for the LeadExpo SaaS platform.

## Overview

LeadExpo is a mobile-first SaaS platform for exhibitors to collect leads via custom forms. This backend handles:
- **Authentication**: JWT-based auth for Companies and Admins.
- **Form Management**: Creation, editing, and public hosting of forms.
- **Submission Handling**: Collecting leads, sending email notifications, and exporting data.
- **Plan Management**: Subscription tiers (Free, Growth, Scale) with feature locking.
- **Payment Requests**: Manual UPI payment verification workflow.

## Tech Stack

- **Runtime**: Node.js
- **Framework**: Express.js
- **Database**: MongoDB (Mongoose)
- **Email**: Nodemailer (SMTP)
- **File Uploads**: Multer
- **PDF Generation**: PDFKit
- **Excel Generation**: ExcelJS

## Setup

1.  **Install Dependencies**:
    ```bash
    npm install
    ```

2.  **Environment Variables**:
    Create a `.env` file in the root directory:
    ```env
    PORT=5000
    MONGO_URI=mongodb://localhost:27017/leadexpo
    JWT_SECRET=your_super_secret_key_change_this
    
    # SMTP Configuration (Hostinger/Gmail/etc)
    SMTP_HOST=smtp.hostinger.com
    SMTP_PORT=465
    SMTP_USER=no-reply@yourdomain.com
    SMTP_PASS=your_email_password
    
    # Admin Setup (for first run)
    ADMIN_EMAIL=admin@leadexpo.com
    ADMIN_PASSWORD=admin123
    ```

3.  **Run Development Server**:
    ```bash
    npm run dev
    ```

4.  **Run Production Server**:
    ```bash
    npm start
    ```

## Scripts

- `npm run dev`: Starts server with Nodemon for hot-reloading.
- `npm start`: Starts server normally.
- `node src/scripts/updateStarterToFree.js`: Migrates old "Starter" plans to "Free".
- `node src/scripts/cleanupInvalidPlans.js`: Removes corrupted plan data.

## API Structure

### Public
- `GET /api/company/plans`: List available plans.
- `GET /api/payment-settings`: Get payment instructions.
- `GET /api/forms/public/:publicLink`: Get public form details.
- `POST /api/submissions/:formId`: Submit a new lead.

### Company (Protected)
- `POST /api/auth/register`: Create account.
- `POST /api/auth/login`: Login.
- `GET /api/company/profile`: Get profile & plan stats.
- `POST /api/forms`: Create form.
- `GET /api/submissions/:formId`: Get leads for a form.
- `GET /api/submissions/:formId/export`: Export leads (PDF/CSV/Excel).

### Admin (Protected)
- `GET /api/admin/companies`: List all companies.
- `POST /api/admin/companies/:id/plan`: Update company plan manually.
- `GET /api/plan-upgrade-requests/admin/pending`: View pending upgrades.
- `POST /api/plan-upgrade-requests/:id/verify`: Approve/Reject upgrade.

## Folder Structure

```
backend/
├── src/
│   ├── config/         # DB connection
│   ├── middleware/     # Auth & Admin checks
│   ├── models/         # Mongoose schemas (Company, Form, Submission, etc.)
│   ├── routes/         # API endpoints
│   ├── scripts/        # Maintenance scripts
│   ├── services/       # Email, Plan logic
│   └── server.js       # Entry point
├── uploads/            # Payment screenshots storage
└── package.json
```

