# EasyPOS — hosting on joywines.bibetech.co.ke

This folder is the complete production application. Everything below assumes
cPanel with **Setup Node.js App** and **PostgreSQL Databases** icons, SSL issued
for `joywines.bibetech.co.ke`.

`server.js` serves both the web app (`app/`) and the API (`/api?q=…`), so there
is no PHP, no `.htaccess` rewrite and no separate API folder.

---

## 1. Subdomain

cPanel → **Domains → Create A New Domain**

| Field | Value |
| --- | --- |
| Domain | `joywines.bibetech.co.ke` |
| Document Root | `joywines.bibetech.co.ke` (or `public_html/joywines`) |

The Node app does **not** need the document root — Passenger proxies the
subdomain to `server.js`. Leaving the folder empty is correct.

Then cPanel → **SSL/TLS Status** → **Run AutoSSL** for
`joywines.bibetech.co.ke` and wait for it to show *Valid*. M-Pesa and the login
cookie both require HTTPS.

---

## 2. Database

cPanel → **PostgreSQL Databases**

1. **Create New Database**: `posdb` → cPanel prefixes it, e.g. `bibetech_posdb`.
2. **Add New User**: e.g. `joywines` → `bibetech_joywines`, strong password.
3. **Add User To Database** → tick **ALL PRIVILEGES**.

Write down the full database name and full user name — the prefix is part of both.

You do **not** need to import `schema.sql`: the first start creates the tables,
the indexes, the default settings and the first administrator.

To start from a completely blank database later (admin user only, no products or
sales), run `blank-database.sql` once with psql or phpPgAdmin → SQL.

---

## 3. Upload and install

1. cPanel → **File Manager** → home folder → create `easypos`
   (outside `public_html` if the host allows it).
2. Upload this zip there, right-click → **Extract**, delete the zip.
3. Confirm the structure:

```
easypos/
├── server.js
├── package.json
├── package-lock.json
├── schema.sql
├── blank-database.sql
├── backup.sh
├── .env.example
├── env.production.sample
├── HOSTING.md              (this file)
├── DEPLOYMENT_GUIDE.md
└── app/
    ├── index.html
    ├── assets/             (css, js, vendor libraries)
    └── uploads/products/   (writable, 755 — product images go here)
```

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

| Field | Value |
| --- | --- |
| Node.js version | 18, 20 or 22 (not older — the server needs global `fetch`) |
| Application mode | **Production** |
| Application root | `easypos` |
| Application URL | `joywines.bibetech.co.ke` |
| Application startup file | `server.js` |

5. Click **Run NPM Install** (installs `express`, `pg`, `bcryptjs`, `multer` from
   the committed `package-lock.json`). Do not upload `node_modules`.

---

## 4. The `.env` file

File Manager → **Settings → Show Hidden Files**, then copy
`env.production.sample` to `.env` and fill in the real values:

```ini
NODE_ENV=production
PORT=
DB_HOST=localhost
DB_PORT=5432
DB_NAME=bibetech_posdb
DB_USER=bibetech_joywines
DB_PASSWORD=your-database-password
SECRET_KEY=<openssl rand -hex 32>
ADMIN_USERNAME=admin
ADMIN_PASSWORD=<10+ characters — the first admin password>
TRUST_PROXY=1
```

- `SECRET_KEY` must be 32+ random characters; in production the server **refuses
  to start** with a missing or placeholder key. Generate it with
  `openssl rand -hex 32` or any password generator (single line, no spaces).
- `PORT` stays empty on cPanel — Passenger sets it.
- `ADMIN_PASSWORD` is only read the first time, when the database has no users.
  Change it in *Settings → Users* right after the first sign-in.
- `.env` holds the database password: never put it in `public_html`, never
  commit it, never email it.
- Optional: the same keys can be entered under **Environment variables** in
  *Setup Node.js App* instead; real environment variables win over `.env`.
- Optional: `SYSTEM_CONTROL_PASSWORD=` sets your own password for
  *Settings → System control* (otherwise the built-in one is used).

---

## 5. First start

1. *Setup Node.js App* → **Restart**.
2. Open `https://joywines.bibetech.co.ke/api/health`. Expected:

```json
{"status":"ok","message":"EasyPOS Node.js API is running","db":"ok","port":...}
```

`503 {"status":"error","message":"Database unavailable"}` means Node is running
but PostgreSQL is not reachable — recheck `DB_*` (including the cPanel prefix).

3. Open `https://joywines.bibetech.co.ke/`, sign in as `admin`, and change the
   password and add your staff under *Settings → Users*.

On that first start the server applies `schema.sql`, runs the column migrations,
seeds the currency/payment settings and creates the administrator.

---

## 6. Business details and M-Pesa

1. *Settings → Business* — name (`Joyland Wines and Spirits`), phone, receipt
   footer.
2. *Settings → M-Pesa* — environment, shortcode, consumer key, secret, passkey
   from the Daraja portal, then the callback URL:

   ```
   https://joywines.bibetech.co.ke/api?q=mpesa/callback
   ```

   (press **Use this site** in the app to fill it automatically). Register the
   same URL at Safaricom. M-Pesa only reaches public HTTPS URLs.
3. *Settings → System control* — optional scheduled system lock.

---

## 7. Backups (do this before going live)

```bash
cd ~/easypos
./backup.sh                      # backups/easypos-backup-YYYYMMDD-HHMMSS.tar.gz
./backup.sh restore backups/<archive>.tar.gz   # stop the app first
```

The archive contains `db.dump` (`pg_dump` custom format) and `uploads.tar.gz`.
It reads `.env`, keeps the last 14 archives, needs `pg_dump`/`pg_restore` and
`tar` on the host. Download archives off the server regularly.

If the extracted script is not executable, run `chmod +x backup.sh` once.

Nightly cron (cPanel → **Cron Jobs**):

```bash
30 2 * * * cd ~/easypos && mkdir -p backups && ./backup.sh >> backups/backup.log 2>&1
```

---

## 8. Go-live checklist

- [ ] `NODE_ENV=production`, 32+ char `SECRET_KEY`, `ADMIN_PASSWORD` 10+ chars.
- [ ] `npm install` finished and the app shows **Running** in *Setup Node.js App*.
- [ ] `https://joywines.bibetech.co.ke/api/health` → `db: "ok"`.
- [ ] `https://joywines.bibetech.co.ke/.env` returns **404** (not the file).
- [ ] Sign-in works over HTTPS and the session survives page reloads.
- [ ] A test sale, its receipt and a PDF report work.
- [ ] M-Pesa callback saved in Settings **and** registered at Safaricom.
- [ ] `./backup.sh` produced an archive and one restore was tested.
- [ ] Admin password changed from the first-run value.

## Troubleshooting

| Symptom | Fix |
| --- | --- |
| 503 / "Application error" | Read the app's stderr log; usually wrong `DB_*` or missing `node_modules` → *Run NPM Install* |
| Page blank, `/api/health` ok | Remove any old `.htaccess` in the app root except cPanel's Passenger one |
| Logged out on every click | Not on HTTPS yet — run AutoSSL |
| Images upload but do not show | `app/uploads/products/` missing or not writable (755) |
| "M-Pesa not configured yet" | Fill key/secret/passkey/shortcode in Settings → M-Pesa |
| Startup error about `SECRET_KEY` | Production requires a 32+ char key in `.env` |

Full details: `DEPLOYMENT_GUIDE.md`.
