# Deploy to cPanel Node.js Hosting

## Files to upload
Upload the entire `collab-shopify-node` folder.

```
collab-shopify-node/
  server.js
  config.js
  collab.js
  shopify.js
  sync.js
  package.json
  .htaccess
  public/
    index.html
```

---

## Step-by-step — cPanel (NameHero / Hostinger)

### 1. Upload files
- Go to **File Manager** in cPanel
- Create a new folder, e.g. `/home/yourusername/collab-shopify`
- Upload all files into it (keeping the `public/` subfolder)

### 2. Set up the Node.js app in cPanel
- Go to **Setup Node.js App** in cPanel
- Click **Create Application**
- Fill in:
  - **Node.js version**: 18 or 20 (latest available)
  - **Application mode**: Production
  - **Application root**: `/home/yourusername/collab-shopify`
  - **Application URL**: choose a domain or subdomain, e.g. `sync.eterni.com.mk`
  - **Application startup file**: `server.js`
- Click **Create**

### 3. Install dependencies
- In the Node.js app panel, click **Run NPM Install**
  OR use the SSH terminal:
  ```bash
  cd ~/collab-shopify
  npm install
  ```

### 4. Edit `.htaccess`
Open `.htaccess` and change `YOUR_CPANEL_USER` to your real cPanel username:
```
PassengerAppRoot /home/YOURUSERNAME/collab-shopify
```

### 5. Edit `config.js` (if you need to change anything)
All credentials are already filled in. Double-check:
- `SHOPIFY_TOKEN` — your Shopify Admin API token
- `SHOPIFY_WEBHOOK_SECRET` — your webhook secret
- `DASHBOARD_KEY` — the password to access the dashboard

### 6. Start the app
In cPanel → Setup Node.js App → click **Start** (or Restart).

### 7. Open the dashboard
```
https://sync.eterni.com.mk/?key=MY_SUPER_SECRET_SYNC_KEY_2025
```
Click **Load Status** to see which SKUs match between Collab and Shopify.
Click **Run Stock Sync Now** to update all inventory.

---

## Set up the Shopify webhook

1. In Shopify admin go to:  
   **Settings → Notifications → Webhooks** (scroll to the bottom)  
   OR  
   In your Shopify Partners app → **Webhooks** tab

2. Click **Create webhook**:
   - **Event**: `Order payment` (orders/paid)
   - **Format**: JSON
   - **URL**: `https://sync.eterni.com.mk/webhook/orders`
   - **Webhook API version**: 2025-01

3. Click **Save**. Shopify will verify the URL.

---

## Daily auto-sync
The app already has a cron job scheduled internally:
```
Every day at 05:00 Europe/Skopje time
```
No extra cPanel cron setup needed — it runs automatically while the Node.js app is running.

If you want to trigger it manually from cPanel Cron Jobs as a backup:
```
0 5 * * *   curl -s -X POST "https://sync.eterni.com.mk/api/sync?key=MY_SUPER_SECRET_SYNC_KEY_2025"
```

---

## API endpoints

| Method | URL | Description |
|--------|-----|-------------|
| GET | `/?key=KEY` | Dashboard UI |
| GET | `/api/status?key=KEY` | JSON: SKU match status (no changes) |
| POST | `/api/sync?key=KEY` | Trigger stock sync (background) |
| GET | `/api/last-sync?key=KEY` | JSON: result of last sync run |
| POST | `/webhook/orders` | Shopify webhook (no key needed — uses HMAC) |

---

## Troubleshooting

**Dashboard shows nothing / "Load status" hangs**
- Check that the Node.js app is running in cPanel → Setup Node.js App
- Check the app's error log (cPanel → Error Logs or the app panel)

**"Cannot connect to Collab API"**
- The server must be able to make outbound HTTP (not HTTPS) requests
- Test from SSH: `curl http://collab.com.mk/eurovizif/CollabApiWeb`

**Shopify token errors (401)**
- The token in `config.js` must have scopes:
  `read_products, write_products, read_inventory, write_inventory, read_orders, read_locations`

**Webhook not creating Collab documents**
- Check the Node.js error log for `[Webhook]` lines
- The Collab `errorCode E2002` means the document type or item IDs are wrong
- SKUs in Shopify must match exactly (case-sensitive) with Collab's `Code` field
