# Chat Page Mapping Fix Report

**Date**: 2026-07-12
**Project**: lnwAdsSJ88
**Site**: https://adsfb.namnan.co.th
**Status**: ✅ PASS

---

## 🐛 Bug

Page mapping ใน `chat_messages` ไม่ตรงกับ Ads API:
- Campaign 120230799572200590 (ไฟตุ้มโซล่าเซลล์ 250W) ควรอยู่บนเพจ SOLAR
- แต่ `chat_messages.page_id = 408865435647368` (เฮียหนวด การเกษตร = FARMING)
- ไม่ตรงกับ Ads API `promoted_object.page_id = 102601625742282` (เฮียหนวด โซล่าเซลล์)

---

## 📊 Evidence

### API — Ads API (Truth)
- **GET /v19.0/120230799572200590/adsets** → `promoted_object.page_id = 102601625742282`
- **GET /v19.0/102601625742282** → `name = "เฮียหนวด โซล่าเซลล์ ไฟตุ้มโซล่าเซลล์ โคมไฟโซล่าเซลล์ สว่างยันเช้า"`

### DB — Before fix
```
408865435647368  เฮียหนวด การเกษตร                120230799572200590  สมชาย ใจดี
408865435647368  เฮียหนวด การเกษตร                120230799572200590  ลุงตุ้ย นาข้าว
```

### DB — After fix
```
102601625742282  เฮียหนวด โซล่าเซลล์ ไฟตุ้มโซล่าเซลล์ โคมไฟโซล่าเซลล์ สว่างยันเช้า  120230799572200590  สมชาย ใจดี
102601625742282  เฮียหนวด โซล่าเซลล์ ไฟตุ้มโซล่าเซลล์ โคมไฟโซล่าเซลล์ สว่างยันเช้า  120230799572200590  ลุงตุ้ย นาข้าว
```

### UI — After fix
- **Inbox (app.html#/chats)**: 5 page chips (All + 4 pages), Solar page now appears with 3 conv / 2 unread
- **Campaign Detail (campaign.html?id=120230799572200590)**: page name = "เฮียหนวด โซล่าเซลล์ ไฟตุ้ม..."

---

## 🔧 Fix Applied

```sql
-- Step 1: Update page_id + page_name
UPDATE chat_messages
SET page_id = '102601625742282',
    page_name = 'เฮียหนวด โซล่าเซลล์ ไฟตุ้มโซล่าเซลล์ โคมไฟโซล่าเซลล์ สว่างยันเช้า'
WHERE campaign_id = '120230799572200590';

-- Step 2: Update conversation_id (page_id is embedded in format: {pageId}-{pageId}-{senderId})
UPDATE chat_messages
SET conversation_id = REPLACE(conversation_id, '408865435647368-408865435647368', '102601625742282-102601625742282')
WHERE campaign_id = '120230799572200590';
```

**Rows affected**: 5 (4 inbound + 1 outbound)

---

## ✅ Verification

| Check | Result |
|-------|--------|
| API `GET /public/chat-inbox` | ✅ 200, pageId=102601625742282 for solar campaign |
| DB `chat_inbox` view | ✅ 4 pages (was 3): All, Solar, Farming, SJ88, คำร่า |
| UI Inbox chips | ✅ 5 chips (was 4) |
| UI Campaign page | ✅ "เฮียหนวด โซล่าเซลล์..." shown correctly |

---

## 📁 Artifacts

- `report.md` (this file)
- `report.html` (visual)
- `summary.json` (machine-readable)
- `test_matrix.json` (test cases)
- `screenshots/01_inbox_after_fix.png`
- `screenshots/02_campaign_chats_after_fix.png`
- `api/api-campaign-page.json` (Ads API response)
- `api/api-real-page.json` (Meta page info)
- `api/api-me-accounts.json` (all admin pages)
- `api/api-inbox-after.json` (chat-inbox after fix)
- `db-before-fix.txt` (DB state before)
- `db-after-fix.txt` (DB state after)
- `pairs/page-mapping-fix__binding.json` (UI ↔ API pairing)
- `logs/` (run logs)
