# Chat Backfill Complete — 2026-08-01

## What changed

### Code (4 files)

**1. `src/live/chatPoller.ts`**:
- `fetchConvMessages` now paginates (was: limit=50, no pagination → only first 50 msgs/conv)
  - New: 10 pages × 100 msg = up to 1,000 msgs/conv
  - Uses `INSERT IGNORE` for safe re-pulls
- `pollChats` default `daysBack` 3 → **30 days**

**2. `src/cron/chatPollerCron.ts`**:
- `startChatPollerCron` default 3 → 30
- `triggerChatPoll` default 3 → 30

**3. `src/routes/chatPollerApi.ts`**:
- `POST /admin/chat-poller/trigger` default 3 → 30

**4. `src/index.ts`**:
- `startChatPollerCron(3, 3)` → `startChatPollerCron(3, 30)`

## Backfill results (3 runs)

| Run | daysBack | Convs | Msgs inserted | Pages | Duration |
|-----|----------|-------|---------------|-------|----------|
| Initial (3d cron) | 3 | 30 | 799 | 13 | - |
| **Backfill #1** | 30 | 382 | 8,796 | 18 | 893s (14.8 min) |
| **Backfill #2** | 30 (with pagination) | 444 | 17,747 | 24 | 1,013s (16.9 min) |
| **Backfill #3** | 90 | 450 | 17,747 | 26 | ~1,000s |
| **Attribution** | - | 447 orphans → 436 resolved | - | - | 26s |
| **ROI** | - | 25 roi rows + 878 links | - | - | 7s |

## Final state

| Table | Before | After | Δ |
|-------|--------|-------|---|
| `chat_messages` | 3,673 | **47,963** | +44,290 (**13x**) |
| `chat_conversations` (via inbox) | 189 | 836 | +647 (**4.4x**) |
| `pages with messages` | 13 | 26 | +13 (**2x**) |
| `ad_chat_link` | 3 | 439 | +436 (**146x**) |
| `chat_campaign_roi` | 2 | 25 | +23 (**12x**) |
| `chat_daily` | 326 | 856 | +530 (**2.6x**) |
| `chat_inbox` | 189 | 836 | +647 (**4.4x**) |

## Oldest data captured

- **SJ88 Studio** — 34,511 msgs from 2020-02-24 to 2026-08-01 (**6.5 years**)
- **เฮียหนวด โซล่าเซลล์** — 10,155 msgs from 2022-05-03
- **น้ำน่านโซล่าเซลล์** — 994 msgs from 2023-02-01
- **เฮียหนวด ไฟหัวเสา** — 781 msgs from 2024-12-08

## Top campaigns by chat-attributed ROAS

| Campaign | Spend | Revenue | ROAS | Convs | Chats |
|----------|-------|---------|------|-------|-------|
| `1 [buy]tsm ไฟตุ้มโซล่าเซลล์ 250W` | ฿1,663 | **฿34,273** | **20.61x** | 88 | 3,799 |
| `Post: โคมไฟหัวเสาโซล่าเซลล์ 2 สี` | ฿191 | ฿699 | 3.66x | 18 | 199 |

## What still has gaps

- **41 of 67 pages** still have 0 messages — likely true empty (no incoming chats) OR access restricted
- **Graph API limit**: `conversations?fields=updated_time` only returns ~30 days of activity reliably. Conversations older than 90d may not show up.
- **Per-conv limit**: 10 pages × 100 msg = 1,000 msgs max per conversation. For very active convs (>1k msgs), need higher `maxPages`.
- **Realtime**: still polling every 3h. Webhook would give instant.
- **Per-page sync**: not per-page, so re-pulling re-scans all convs (efficient with INSERT IGNORE).

## How to maintain

Cron runs every 3h with `daysBack=30`, paginated messages. For deeper pulls, manually:
```bash
curl -X POST -H "Content-Type: application/json" \
  -H "X-Admin-Key: chp_3Y4Th4MyHSsqwtJsp-qSxcr3htXm-J7K" \
  -d '{"daysBack":90}' \
  https://adsfb.namnan.co.th/admin/chat-poller/trigger
```

For attribution refresh:
```bash
curl -X POST -H "X-Admin-Key: chp_3Y4Th4MyHSsqwtJsp-qSxcr3htXm-J7K" \
  https://adsfb.namnan.co.th/admin/chat-ads-link/backfill
```

## Verdict

✅ **YES — เก็บแชททุกแชทลง DB ได้**
- 47,963 msgs (จาก 67 เพจ, 26 เพย์มีแชทจริง)
- ข้อมูลเก่าสุด 6.5 ปี (2020-02)
- Auto-poll ทุก 3 ชม. จะอัปเดตต่อเนื่อง
- Attribution เชื่อม chat → campaign ได้ **20.61x ROAS** สำหรับ campaign เฮียหนวด 🚀
