# Pre-Deployment Checklist – WhatsApp Messaging & Full App

Use this **before** deploying to **node.ekpk.pk** (backend) and **whatsapp.ekpk.pk** (frontend). Complete each section and tick the boxes so WhatsApp and the app are verified first.

---

## 1. WhatsApp / Meta Setup

### 1.1 Meta App & Webhook URL

- [ ] **Callback URL in Meta**  
  In Meta App Dashboard → WhatsApp → Configuration → Webhook, set:
  - **Callback URL:** `https://node.ekpk.pk/api/v1/webhooks/whatsapp`
  - (Use this only after the backend is live on node.ekpk.pk and responding.)

- [ ] **Verify token**  
  In Meta, set **Verify token** to the same value as in your backend `.env` (e.g. `AfsaraliKhan`). Backend supports:
  - `WEBHOOK_VERIFY_TOKEN` or `META_WEBHOOK_VERIFY_TOKEN`

- [ ] **Webhook verification**  
  After saving in Meta, click **Verify and save**. Meta sends a GET request; backend must return the challenge. If it fails, check:
  - Backend is running on node.ekpk.pk and reachable via HTTPS.
  - Reverse proxy (e.g. Nginx) forwards to the Node app (e.g. port 5000).
  - `.env` has the correct verify token.

- [ ] **Subscribe to events**  
  In the same Webhook section, subscribe to **messages** (and optionally **message status**).

### 1.2 Backend environment variables (WhatsApp)

On the server (node.ekpk.pk), in `backend/unified_server/.env` or your process-manager env:

- [ ] **Verify token** (one of):
  - `WEBHOOK_VERIFY_TOKEN=AfsaraliKhan`  
  - or `META_WEBHOOK_VERIFY_TOKEN=AfsaraliKhan`

- [ ] **Graph API token** (one of):
  - `GRAPH_API_TOKEN=<your_token>`  
  - or `META_ACCESS_TOKEN=<your_token>`

- [ ] **Phone Number ID** (required for sending):
  - `META_PHONE_NUMBER_ID=<from Meta Dashboard → WhatsApp → API Setup>`

- [ ] **API version** (optional):
  - `META_API_VERSION=v21.0` (or v25.0)

### 1.3 Test WhatsApp receive (incoming)

- [ ] Backend is running and webhook URL is set in Meta (step 1.1).
- [ ] From your phone, send a **WhatsApp message** to the business number linked to your Meta app.
- [ ] Check backend logs for webhook POST (e.g. “Received webhook payload”).
- [ ] Check DB: new row in `messages` (or your messages table) and optionally `webhook_logs`.
- [ ] If using the app inbox: refresh or use real-time; the message should appear in the conversation.

### 1.4 Test WhatsApp send (outgoing)

- [ ] `META_PHONE_NUMBER_ID` and token are set (step 1.2).
- [ ] From the app (or Postman), call the send-message API (e.g. POST to your outbound endpoint with `to`, `messageText`, `businessId`, etc.).
- [ ] Check backend logs for success or Meta API error.
- [ ] On WhatsApp, confirm the message is received on the target phone.
- [ ] Optional: send from the **inbox UI** (select conversation, type, send) and confirm delivery.

If any step fails, fix before deployment (see **documentation/WHATSAPP_INTEGRATION_GUIDE.md**).

---

## 2. Backend (node.ekpk.pk)

### 2.1 Environment & database

- [ ] **`.env`** on server (or in process manager) with:
  - Database: `DATABASE_URL` or `DB_HOST`, `DB_PORT`, `DB_NAME`, `DB_USER`, `DB_PASSWORD` (remote DB).
  - Server: `NODE_ENV`, `PORT` (e.g. 5000).
  - WhatsApp: as in section 1.2.
  - No secrets committed to git; `.env` in `.gitignore`.

- [ ] **Database reachable**  
  From the server, test DB connection (e.g. run a small script or start the app and check for DB errors). Migrations applied if needed.

### 2.2 CORS & reverse proxy

- [ ] **CORS** allows the frontend origin:  
  `https://whatsapp.ekpk.pk`  
  In backend (e.g. `server.js`), something like:
  - `origin: process.env.CORS_ORIGIN || 'https://whatsapp.ekpk.pk'`  
  or an array including that origin.

- [ ] **HTTPS**  
  node.ekpk.pk is served over HTTPS (e.g. Nginx + SSL).

- [ ] **Reverse proxy**  
  Requests to `https://node.ekpk.pk/` (and `/api/...`) are proxied to the Node app (e.g. port 5000). Health check works: `https://node.ekpk.pk/` or `https://node.ekpk.pk/health`.

### 2.3 Health & webhook

- [ ] **GET** `https://node.ekpk.pk/` or `https://node.ekpk.pk/health` returns 200 and a success message.
- [ ] **GET** `https://node.ekpk.pk/api/v1/webhooks/whatsapp?hub.mode=subscribe&hub.verify_token=AfsaraliKhan&hub.challenge=test123`  
  returns body `test123` and 200 (so Meta verification will succeed when you point the webhook to this URL).

---

## 3. Frontend (whatsapp.ekpk.pk)

### 3.1 API base URL

- [ ] Frontend is built and deployed with **API base URL** pointing to backend:
  - **Production:** `https://node.ekpk.pk`
  - All API calls (auth, conversations, contacts, messages, etc.) use this base (no `localhost:5000` in production build).

- [ ] If using env: e.g. `VITE_API_URL=https://node.ekpk.pk` and every API client uses `import.meta.env.VITE_API_URL` (or a single config that reads it).

### 3.2 Socket (real-time)

- [ ] Socket.io client points to the backend URL in production (e.g. `https://node.ekpk.pk`), not localhost.
- [ ] Backend allows this origin in Socket.io CORS.

### 3.3 Deploy

- [ ] Build: `npm run build` (e.g. in `frontend/`).
- [ ] Document root for whatsapp.ekpk.pk is set (e.g. `/home/scnbuneredu/whatsapp.ekpk.pk`).
- [ ] Built files (e.g. `dist/`) are deployed to that document root; Nginx (or server) serves the SPA (fallback to index.html for client routes).
- [ ] HTTPS is enabled for whatsapp.ekpk.pk.

---

## 4. End-to-end checks (before calling it “deployed”)

- [ ] **Login**  
  Open https://whatsapp.ekpk.pk, log in with a test user; no CORS or network errors.

- [ ] **Dashboard**  
  Dashboard loads; widgets and data come from backend (node.ekpk.pk).

- [ ] **Inbox / conversations**  
  Conversations list loads; selecting a conversation loads messages.

- [ ] **Send message (app → WhatsApp)**  
  From the inbox, send a message to a WhatsApp number; message is delivered on WhatsApp.

- [ ] **Receive message (WhatsApp → app)**  
  Send a message from WhatsApp to your business number; it appears in the app (inbox and/or DB).

- [ ] **Contacts**  
  Contacts list and add-contact work with the remote DB.

---

## 5. Quick reference – URLs

| What        | URL |
|------------|-----|
| Backend    | https://node.ekpk.pk |
| Frontend   | https://whatsapp.ekpk.pk |
| Webhook    | https://node.ekpk.pk/api/v1/webhooks/whatsapp |
| Health     | https://node.ekpk.pk/ or https://node.ekpk.pk/health |

---

## 6. If something fails

- **Webhook verification fails:** Backend not reachable, wrong verify token, or proxy not forwarding.
- **No incoming messages:** Webhook URL wrong in Meta, or backend not running / not receiving POST.
- **No outgoing messages:** Missing or wrong `META_PHONE_NUMBER_ID` or token; check backend logs and Meta API errors.
- **CORS errors:** Backend CORS must include `https://whatsapp.ekpk.pk`.
- **Frontend calls localhost:** Rebuild with correct API URL and redeploy.

Complete this checklist and fix any failures **before** final deployment. After everything is green, you can treat the app as ready for production use.
