# Rider Survey Platform

A complete multi-role platform for managing a rider survey workflow: Admin → Vendor → Project Manager → Client → Payment.

## Features

- **4 role-based dashboards** with distinct workflows
- **Email/password auth** (JWT)
- **Vendor form-link generation** — vendor creates a link, riders fill it on mobile
- **Slot enforcement** — max N entries per city × platform (default 50)
- **Validation** — date range must be exactly 7 days, file uploads, city/platform whitelist
- **Approval workflow** — task moves through pending → in_progress → submitted → pm_approved → client_approved → paid (with rejection branch)
- **Notifications** — in-app bell for status changes
- **Vendor performance** dashboard for PM/Admin
- **Public rider form** at `/f/:token` — no login required
- **File uploads** — videos + screenshots stored in `/uploads`
- Self-contained SQLite DB (no external DB needed)

## Workflow

```
Admin creates task ──► Vendor generates form link(s) ──► Riders fill public form
                                                                    │
                                                                    ▼
                  ◄── PM rejects ◄── PM reviews ◄── Vendor submits
                                              │
                                              ▼
                                  Client reviews & approves
                                              │
                                              ▼
                                  Client marks as paid → Done
```

## Stack

- **Backend**: Node.js + Express + better-sqlite3 + JWT + Multer
- **Frontend**: React 18 + Vite + TypeScript + TailwindCSS + React Router
- **Auth**: JWT in `Authorization: Bearer ...`, stored in localStorage

## Quick start

```bash
# 1. Install backend deps
cd server
npm install
npm run seed          # creates data.db with demo users + tasks
npm start             # http://localhost:4000

# 2. In a new terminal, install + build frontend
cd client
npm install
npm run build         # builds to client/dist (served by backend)
# or for development:
npm run dev           # http://localhost:5173 (proxies /api to :4000)
```

The backend serves the built frontend at `http://localhost:4000`.

## Demo accounts (after `npm run seed`)

| Role | Email | Password |
|------|-------|----------|
| Admin | `admin@demo.com` | `admin123` |
| Project Manager | `pm@demo.com` | `pm12345` |
| Vendor | `vendor@demo.com` | `vendor123` |
| Vendor | `vendor2@demo.com` | `vendor123` |
| Client | `client@demo.com` | `client123` |

## URLs

| Route | Audience |
|-------|----------|
| `/login` | Anyone |
| `/admin/*` | Admin |
| `/pm/*` | Project Manager |
| `/vendor/*` | Vendor |
| `/client/*` | Client |
| `/f/:token` | Public rider form |

## Task status lifecycle

- `pending` → admin just created
- `in_progress` → vendor started collecting
- `submitted` → vendor submitted for PM review
- `pm_approved` → PM approved, sent to client
- `client_approved` → client approved
- `paid` → client marked as paid
- `rejected` → could happen at PM or client step (back to vendor)

## Form fields (rider side)

- Rider name
- Rider location / area
- City (Delhi, Gurugram, Mumbai, Bangalore, Chennai)
- Platform (Swiggy, Zomato, Blinkit, Zepto, Shadowfax)
- Video sample upload
- Earnings screenshots (one or more)
- Week start date
- Week end date (must be exactly 7 days after start)
- Weekly earnings amount

## API (selected)

```
POST   /api/auth/login           { email, password }              → { token, user }
GET    /api/auth/me              (auth)                            → { user }
POST   /api/users                (admin)                           create user
GET    /api/users?role=vendor    (auth)                            list users
GET    /api/tasks                (auth, role-filtered)             list tasks
POST   /api/tasks                (admin)                           create task
GET    /api/tasks/:id            (auth)                            task detail
POST   /api/tasks/:id/submit     (vendor)                          submit for PM
POST   /api/tasks/:id/pm-approve (pm)                              approve
POST   /api/tasks/:id/pm-reject  (pm)                              reject
POST   /api/tasks/:id/client-approve (client)                      approve
POST   /api/tasks/:id/pay        (client)                          mark paid
GET    /api/tasks/performance/all (admin, pm)                      vendor perf
POST   /api/links                (vendor)                          create form link
GET    /api/links/mine           (vendor)                          my links
GET    /api/links/public/:token  (public)                          link info + slots
POST   /api/links/:id/close      (vendor)                          close link
POST   /api/submissions          (public)                          submit form entry
POST   /api/upload               (auth)                            single file
POST   /api/upload/public        (public)                          multiple files
GET    /api/notifications        (auth)                            my notifications
```

## Deployment

The whole app is a single Node.js process. Build the frontend, then run the server:

```bash
cd client && npm install && npm run build
cd ../server && npm install --production
npm start
```

Put behind nginx/Caddy for HTTPS. SQLite lives in `server/data.db` — back up that file.

For multi-instance deployments, swap SQLite for Postgres (the schema is portable).

## Environment

| Variable | Default | Purpose |
|----------|---------|---------|
| `PORT` | `4000` | Server port |
| `JWT_SECRET` | dev placeholder | Sign JWTs (set in production!) |
| `DB_PATH` | `./data.db` | SQLite file path |
| `VITE_API_URL` | empty (relative) | Frontend: API base URL |

## File structure

```
rider-platform/
├── server/
│   ├── src/
│   │   ├── index.js              Express entrypoint
│   │   ├── db/
│   │   │   ├── index.js          SQLite connection
│   │   │   ├── schema.js         Tables + indexes
│   │   │   └── seed.js           Demo data
│   │   ├── middleware/auth.js    JWT + role guards
│   │   └── routes/
│   │       ├── auth.js
│   │       ├── users.js
│   │       ├── tasks.js
│   │       ├── links.js
│   │       ├── submissions.js
│   │       ├── upload.js
│   │       └── notifications.js
│   ├── uploads/                  File storage (gitignore)
│   └── package.json
└── client/
    ├── src/
    │   ├── App.tsx               Routes
    │   ├── main.tsx
    │   ├── index.css
    │   ├── types.ts
    │   ├── lib/
    │   │   ├── api.ts
    │   │   └── auth.tsx
    │   ├── components/
    │   │   ├── DashboardLayout.tsx
    │   │   └── Guards.tsx
    │   └── pages/
    │       ├── Login.tsx
    │       ├── PublicForm.tsx
    │       ├── admin/
    │       ├── pm/
    │       ├── vendor/
    │       └── client/
    ├── index.html
    └── package.json
```
