# Deploying Ariana on cPanel (Node.js)

The system is one Node.js app: it serves the web pages and a small API, and keeps all data in one file
(`data/db.json`) with a daily backup. It runs on cPanel's **Setup Node.js App** (Phusion Passenger). No MySQL needed.

## 1. Make the upload package (on your Mac)

In the `ariana-parts` folder:

```bash
npm install
npm run package:cpanel
```

This builds the app and creates **`deploy/ariana-parts-cpanel.zip`** (only what the server needs).

## 2. Upload it

1. cPanel → **File Manager**.
2. In your home folder (next to `public_html`, **not inside it**), create a folder, e.g. `ariana-app`.
3. Open it, **Upload** `ariana-parts-cpanel.zip`, then right-click it → **Extract**. You should see
   `dist/`, `server/`, `db.seed.json`, `package.json`, `DEPLOY.md`. You can delete the zip afterwards.

## 3. Create the Node.js app

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

| Field | Value |
|---|---|
| Node.js version | 20 or 22 (18 is the minimum) |
| Application mode | Production |
| Application root | `ariana-app` |
| Application URL | your domain, or a sub-path such as `ariana` |
| Application startup file | `server/index.cjs` |

Under **Environment variables**, add:

| Name | Value |
|---|---|
| `JWT_SECRET` | a long random text (40+ characters). Keep it private. |
| `SEED` | `empty` for the real business (starts clean), or `demo` to show the sample data |
| `SHOW_DEMO_LOGINS` | `0` on a live site (`1` shows the demo accounts on the sign-in page) |

Click **Create**, then **Run NPM Install**, then **Restart**.

## 4. First sign-in

Open your Application URL. Sign in as **owner / owner123**.

**Change the default passwords straight away:**
- Your own: profile menu (top right) → **Change password**.
- Everyone else: **Settings → Users → Password** next to each person. The default accounts are
  `manager / manager123` and `staff / staff123`.
- Add real people in Settings with their own username and password; turn off accounts nobody uses.

## Roles

| Role | Can do |
|---|---|
| Owner | Everything, including users, settings and the audit log |
| Manager | Purchases (record and mark received), vehicles, containers, inventory, POS, expenses, reports |
| Staff | Point of sale, sales and returns, inventory |

These limits are checked by the server too, not only hidden in the menu.

## Updating to a new version

1. On your Mac: `npm run package:cpanel`.
2. In File Manager, upload the new zip into `ariana-app` and **Extract**, letting it replace files.
   **Never delete the `data` folder**: it holds all your records.
3. Setup Node.js App → **Run NPM Install** (only needed if packages changed) → **Restart**.

## Backups

- Every day the first change makes a copy in `ariana-app/data/backups/` (the last 14 days are kept).
- Download the whole `data` folder from File Manager now and then, and keep it somewhere safe.
- To restore: stop the app, replace `data/db.json` with a backup copy, start the app.

## Starting over

To wipe the data and start again from `SEED` (the old file is kept as `db.json.before-reset-…`):
Setup Node.js App → **Run JS script** → `reset`, or in cPanel Terminal:

```bash
source ~/nodevenv/ariana-app/20/bin/activate && cd ~/ariana-app && npm run reset
```

(Change `20` to your Node.js version.) Then **Restart** the app.

## If something goes wrong

| What you see | What to do |
|---|---|
| “The web app is not built yet” | The `dist` folder is missing. Re-upload the zip and extract it. |
| “Cannot find module 'express'” in the log | Click **Run NPM Install**, then **Restart**. |
| Blank page under a sub-path | Add the environment variable `BASE_PATH` with the sub-path, e.g. `/ariana`, and restart. |
| Everyone was signed out | `JWT_SECRET` was changed. Sign in again. |
| Error details | `ariana-app/stderr.log` (or the log shown in Setup Node.js App). |

Turn on HTTPS for the domain (cPanel → **SSL/TLS Status** → Run AutoSSL) so passwords travel encrypted.

## Running it on your Mac

```bash
npm run dev       # browser-only demo (no server), data kept in the browser
npm run dev:api   # Node.js server on :3001 + app on :5173, the same way it runs on cPanel
```
