# VATA / FieldForce

Field service management demo — React (Vite) + Express + MySQL.

## Project structure

```
backend/    Express API (JWT auth, MySQL)
frontend/   React app (Vite)
```

## Setup

```bash
npm run install:all
```

### Environment variables

Copy the examples and fill in real values. `.env` files are gitignored and must never be committed.

```bash
cp backend/.env.example backend/.env
cp frontend/.env.example frontend/.env
```

**Backend (`backend/.env`)**

| Key | Description |
| --- | --- |
| `PORT` | API port (default `5000`, or `5010` when `NODE_ENV=production`) |
| `JWT_SECRET` | Long random string — generate one: `node -e "console.log(require('crypto').randomBytes(48).toString('hex'))"` |
| `JWT_EXPIRES_IN` | Token lifetime, e.g. `7d` |
| `CORS_ORIGIN` | Comma-separated allowed origins |
| `DB_HOST` / `DB_PORT` / `DB_USER` / `DB_PASSWORD` / `DB_NAME` | MySQL connection |

**Frontend (`frontend/.env`)**

| Key | Description |
| --- | --- |
| `VITE_API_URL` | API base URL — `/api` when backend serves the built frontend |
| `VITE_USE_MOCK` | `false` in production; `true` forces demo data |

### Production values

```env
# backend/.env
PORT=5010
JWT_SECRET=<generate a long random value>
JWT_EXPIRES_IN=7d
CORS_ORIGIN=https://yourdomain.com
DB_HOST=<prod-mysql-host>
DB_PORT=3306
DB_USER=<prod-user>
DB_PASSWORD=<prod-password>
DB_NAME=fieldforce
```

```env
# frontend/.env
VITE_API_URL=/api
VITE_USE_MOCK=false
```

## Development

```bash
# terminal 1
npm run dev:backend

# terminal 2
npm run dev:frontend
```

Frontend runs at `http://localhost:5173` and proxies `/api` to the backend on port `5000`.

### Database

```bash
# one-time bootstrap (creates DB, users table, demo users)
npm run db:init --prefix backend

# or run migrations manually
npm run db:migrate --prefix backend
```

Demo users (change passwords in production): `admin/admin123`, `coordinator/coordinator123`, `reviewer/reviewer123`.

## Production build

```bash
npm run build          # builds frontend to frontend/dist
npm run start:backend  # Express serves /api and the built frontend
```

The backend serves `frontend/dist` automatically when it exists, so a single Node process handles both the API and the UI.

Set `JWT_SECRET`, database credentials and `CORS_ORIGIN` in the server's `backend/.env` before deploying.
