# Deploying the backend (MilesWeb cPanel — Node.js App / Passenger)

The API is a plain Express app started by `node server.js`. On MilesWeb shared/
business hosting it runs under **cPanel → Setup Node.js App**, which uses Phusion
Passenger behind LiteSpeed. `app.set('trust proxy', 1)` is already configured for
that.

## 0. Before you upload — create the migration history

`prisma migrate deploy` (run on the server) only **applies** migrations that
already exist in `prisma/migrations/`. That folder currently has just the initial
auth migration. Generate the rest locally first, against a throwaway/dev database
(a Neon branch works well):

```bash
# locally, with DATABASE_URL/DIRECT_URL pointing at a dev DB
npx prisma migrate dev --name plans_sales_pdf_templates
```

Commit / upload the resulting `prisma/migrations/**`. (Quick alternative for a
first cut with no history: skip migrations and run `npx prisma db push` on the
server instead of step 5.)

## 1. Neon database

- Create the database in the Neon console; copy the **pooled** connection string
  into `DATABASE_URL` and the **direct** one into `DIRECT_URL`.
- Both must keep `?sslmode=require`.
- Neon speaks Postgres on **port 5432**. Confirm MilesWeb allows outbound 5432 to
  `*.neon.tech` (open a support ticket if a test connection hangs).

## 2. Upload the code

Upload the `backend/` folder to something like `~/apps/pet-insurance-admin-api`
(App Manager, Git, or SFTP). Do **not** upload `node_modules` or any `.env`.

## 3. Create the Node.js app in cPanel

**Setup Node.js App → Create Application**

| Field | Value |
|---|---|
| Node.js version | 18 or 20 LTS |
| Application mode | Production |
| Application root | `apps/pet-insurance-admin-api` |
| Application URL | a subdomain, e.g. `api.yourdomain.com` |
| Application startup file | `server.js` |

Add every variable from `.env.production.example` under **Environment variables**
(or place a filled-in `.env` in the application root — `dotenv` picks it up).

## 4. Install dependencies

Click **Run NPM Install**. This runs `npm install`, which triggers the
`postinstall` script (`prisma generate`). `prisma` is a normal dependency so it
is available even with dev deps pruned.

## 5. Run migrations + seed (once)

From the app's shell (cPanel **Terminal**, `cd` into the app root, then
`source ~/nodevenv/apps/pet-insurance-admin-api/<ver>/bin/activate`), or via the
app's "Run JS script" using the npm scripts:

```bash
npm run migrate:deploy   # apply prisma/migrations to Neon
npm run seed             # first super admin + default insurers/plan/PDF template
```

`npm run release` does both in one go for later deploys (seed is idempotent).

## 6. Enable HTTPS

In cPanel **SSL/TLS Status**, issue the free Let's Encrypt cert for
`api.yourdomain.com`. This is required — the session uses a
`SameSite=None; Secure` refresh cookie, which browsers only accept over HTTPS.

## 7. Restart

Use the **Restart** button in Setup Node.js App, or `touch tmp/restart.txt` in
the app root. Passenger sends `SIGTERM`; `server.js` drains connections and
disconnects Prisma before exit.

## 8. Verify

```bash
curl https://api.yourdomain.com/api/health
# {"success":true,"message":"Pet Insurance Admin API is up", ...}
```

## Frontend

Build the SPA with the API URL baked in and host the static `dist/` (a second
cPanel site / subdomain, or any static host):

```bash
cd ../frontend
echo "VITE_API_URL=https://api.yourdomain.com/api" > .env.production
npm ci && npm run build      # outputs dist/
```

Set the backend's `CLIENT_URL` to that frontend origin (comma-separate if there
is more than one). SPA routing needs a catch-all rewrite to `index.html`
(`.htaccess`: `FallbackResource /index.html`).

## Redeploys

1. Upload changed files.
2. **Run NPM Install** if `package.json` changed.
3. `npm run migrate:deploy` if there are new migrations.
4. Restart the app.

## Notes

- `PORT` is provided by Passenger — leave it unset in production.
- cPanel generates the app's `.htaccess`; don't hand-edit it unless you know why.
- Logs: app stdout/stderr in the Setup Node.js App log; Passenger errors in the
  domain's Apache/LiteSpeed error log under `~/logs`.
