# Beck Cabinet Company — Portal
## cPanel Setup Guide · beckportal.com

---

### How it works

```
cPanel (beckportal.com)
  │
  ├── Node.js App Manager  → runs server.js (the portal)
  │
  ├── MySQL Database       → stores users, sessions, cached project data
  │
  └── Cron Jobs            → runs sync-monday.js every 5 minutes
                               └── pulls Monday.com data
                               └── writes to monday_cache table
                               └── portal reads cache (fast, no Monday API delay)
```

No Docker. No cloud provider. Everything runs on your existing GoDaddy hosting.

---

## Step 1 — Upload files

Upload the entire project folder to your GoDaddy hosting. The recommended path is:

```
/home/YOUR_CPANEL_USERNAME/beckportal.com/
```

You can use:
- **cPanel File Manager** — upload a zip, then extract
- **FTP/SFTP** — use FileZilla or similar
- **Git** — if your host supports it (cPanel → Git Version Control)

---

## Step 2 — Create .env file

In the app root (`/home/USERNAME/beckportal.com/`), create a file called `.env`.
Copy the contents of `.env.example` and fill in your values:

```
DB_HOST=localhost
DB_PORT=3306
DB_NAME=beckport_portal        ← your database name from cPanel
DB_USER=beckport_admin         ← your database username
DB_PASSWORD=your_password

MONDAY_API_TOKEN=your_token
MONDAY_BOARD_ID=your_board_id

NODE_ENV=production
PORT=3000
BASE_URL=https://beckportal.com

TOKEN_SECRET=   ← generate (see below)
SESSION_SECRET= ← generate (see below)
ADMIN_KEY=      ← generate (see below)

SESSION_DURATION=8h
CACHE_TTL_MINUTES=6
```

**Generate secrets** — run these in cPanel Terminal or SSH:
```bash
node -e "console.log(require('crypto').randomBytes(64).toString('hex'))"
```
Run it three times and paste each output into TOKEN_SECRET, SESSION_SECRET, ADMIN_KEY.

---

## Step 3 — Install dependencies

In cPanel → **Terminal** (or SSH):

```bash
cd ~/beckportal.com
npm install
```

---

## Step 4 — Run the database migration

Creates the four MySQL tables (users, sessions, monday_cache, sync_log):

```bash
cd ~/beckportal.com
node scripts/migrate.js
```

Expected output:
```
✅  Connected
✅  Table: users
✅  Table: sessions
✅  Table: monday_cache
✅  Table: sync_log
✅  Migration complete — 4 tables ready.
```

---

## Step 5 — Test the Monday sync

Run the sync script manually first to confirm it can reach Monday.com:

```bash
cd ~/beckportal.com
node scripts/sync-monday.js
```

Expected output:
```
Fetching projects from Monday.com...
Found 12 project(s)
✅  Hartley Residence
✅  Lake View Custom Home
...
Done — 12 synced, 0 errors (4.2s)
```

If you see errors, check your MONDAY_API_TOKEN and MONDAY_BOARD_ID in `.env`.

---

## Step 6 — Create the first admin user

```bash
cd ~/beckportal.com
npm run create-user
```

Enter name, email, and password when prompted.

---

## Step 7 — Set up the Node.js app in cPanel

1. **cPanel → Software → Setup Node.js App**

2. Click **Create Application**

3. Fill in:
   - **Node.js version**: 18 or 20 (latest available)
   - **Application mode**: Production
   - **Application root**: `beckportal.com` (or the folder you uploaded to)
   - **Application URL**: `beckportal.com`
   - **Application startup file**: `server.js`

4. Click **Create** — cPanel will install and start the app

5. The app is now running. Visit `https://beckportal.com/health` to confirm:
   ```json
   { "status": "ok", "db": "connected", "lastSync": "..." }
   ```

6. **If the port conflicts**: cPanel's Node.js manager assigns a port automatically —
   you may not need to set PORT at all. Remove `PORT=3000` from `.env` if cPanel
   complains about port conflicts.

---

## Step 8 — Set up the cron job

This is what keeps your Monday data fresh.

1. **cPanel → Advanced → Cron Jobs**

2. Set frequency to **Every 5 Minutes**:
   - Minute: `*/5`
   - Hour: `*`
   - Day: `*`
   - Month: `*`
   - Weekday: `*`

3. Command (replace `USERNAME` with your actual cPanel username):
   ```
   /usr/local/bin/node /home/USERNAME/beckportal.com/scripts/sync-monday.js >> /home/USERNAME/logs/monday-sync.log 2>&1
   ```

4. **Create the logs folder first:**
   ```bash
   mkdir -p ~/logs
   ```

5. Click **Add New Cron Job**

**Finding your Node.js path** — if `/usr/local/bin/node` doesn't work, find it with:
```bash
which node
```
Use that full path in the cron command.

**Verify it's working** — after 5 minutes, check the log:
```bash
tail -50 ~/logs/monday-sync.log
```

You should see sync output every 5 minutes.

---

## Step 9 — Monday.com column setup

Run this to see your board's column IDs:
```bash
cd ~/beckportal.com
node scripts/list-columns.js
```

Then edit `monday-config.js` with the correct column IDs for your board.

---

## How the cron sync works

Every 5 minutes:
1. `sync-monday.js` runs
2. It calls the Monday.com API and fetches all your board items
3. Each project is transformed into portal-ready JSON
4. The JSON is saved to the `monday_cache` table in MySQL
5. Old entries (projects deleted from Monday) are removed automatically
6. When a customer opens their portal link, the server reads from the cache — no Monday API call needed, so pages load instantly

The admin dashboard shows the last sync time and how many projects are cached.

---

## Restarting the app

If you change any files (`.env`, `monday-config.js`, etc.), restart the Node.js app:

**cPanel → Setup Node.js App → your app → Restart**

Or in Terminal:
```bash
cd ~/beckportal.com
# cPanel usually manages the process — use the UI restart button
# Or if you have passenger/phusion:
touch ~/beckportal.com/tmp/restart.txt
```

---

## Troubleshooting

| Problem | Fix |
|---|---|
| `health` shows `db: disconnected` | Check `.env` DB credentials; confirm MySQL user has correct permissions |
| Sync log shows Monday API errors | Check MONDAY_API_TOKEN and MONDAY_BOARD_ID in `.env` |
| Cron job not running | Check the log path exists (`mkdir -p ~/logs`); verify node path with `which node` |
| Portal shows "not synced yet" | Run `node scripts/sync-monday.js` manually to force a sync |
| Node.js app won't start | Check cPanel → Setup Node.js App → logs for startup errors |
| Admin login fails | Run `npm run create-user` to create or reset an account |
| Data is out of date | Check `~/logs/monday-sync.log` — look for errors in the last few cron runs |

---

## File structure

```
beckportal.com/
├── server.js              ← Portal web server (started by cPanel Node.js)
├── monday.js              ← Monday.com API client
├── monday-config.js       ← Column mapping — edit this to match your board
├── auth.js                ← Login & user management
├── db.js                  ← MySQL connection pool
├── tokens.js              ← Unique project link generation
├── package.json
├── .env                   ← Your credentials (never commit this)
├── scripts/
│   ├── sync-monday.js     ← Cron script — run every 5 min
│   ├── migrate.js         ← One-time database setup
│   ├── create-user.js     ← Create admin accounts
│   └── list-columns.js    ← Find Monday column IDs
└── public/
    ├── portal.html        ← Customer-facing portal
    ├── admin.html         ← Admin dashboard
    ├── login.html         ← Admin login
    └── logo.png
```
