# AGENTS.md — ระบบจัดการเพจ (Page Manager) ระบบหลังบ้านสำหรับทีมดูแล **หลายเพจ Facebook (+ Instagram ที่ผูกกับเพจ)** ในที่เดียว: เชื่อมเพจ, เขียน/อนุมัติ/ตั้งเวลาโพสต์, ตอบ inbox + คอมเมนต์, auto-reply, insights Setup / env / คำสั่ง: [README.md](README.md) ## Verify ก่อนจบงาน `npm run typecheck` · `npm run lint` · `npm run build` — ทั้งสามต้องผ่าน. ไม่มี test suite; flow ที่แตะ Graph ทดสอบกับ DB local (`npx prisma dev`) + เพจทดสอบจริง ## เอกสาร (อ่านก่อนแก้ส่วนนั้น) - **[docs/ARCHITECTURE.md](docs/ARCHITECTURE.md)** — โมดูล, data model, auth/roles, Graph API, webhook, cron, กับดัก Meta. อ่านก่อนแตะ `src/lib/facebook/`, `prisma/`, `api/webhooks/`, `api/cron/` - **[DESIGN.md](DESIGN.md)** — สี, font, layout, component. อ่านก่อนสร้าง/แก้ UI ทุกหน้า ## Reference projects (อ่านอย่างเดียว ห้ามแก้) | ต้องการ | ดูที่ | | --- | --- | | **ตัวอย่างเชื่อม FB** ที่ใช้งานจริง (Graph client, token exchange, publish/schedule, inbox, webhook HMAC, insights, cron) | `D:\Project 4tech\loma-agent\dashboard\src\lib\facebook\`, `src\app\api\webhooks\messenger\route.ts`, `src\app\api\cron\` | | **โทนสี / visual language** | `D:\Project 4tech\Lomalotto\Loma-Lotto\DESIGN.md` + `frontend\app.css` | dashboard ผูก **เพจเดียว** ผ่าน env `META_PAGE_ID` — ระบบนี้ **หลายเพจ**: ทุกฟังก์ชัน Graph รับ `pageId` เป็น argument และดึง token จาก DB (รายละเอียดใน ARCHITECTURE.md). ยก pattern มาได้ แต่ห้ามลอก global `META_PAGE_ID` ## Stack (ตาม dashboard) Next.js 16 App Router + React 19 + TypeScript · Prisma 7 + PostgreSQL · Tailwind v4 · `jose` session cookie · Vercel (Cron, Blob) **Next.js 16 ต่างจากที่ model รู้จัก** — ก่อนเขียน route/layout/proxy ให้อ่าน guide ใน `node_modules/next/dist/docs/`. auth gate อยู่ที่ `src/proxy.ts` (ไม่ใช่ `middleware.ts`) ## กฎหลัก - **Token อยู่ฝั่ง server เท่านั้น** — page token เก็บใน DB แบบเข้ารหัส (AES-256-GCM, key `TOKEN_ENCRYPTION_KEY`), ห้ามส่งถึง browser, ห้าม log - Error จาก Graph: log เต็มฝั่ง server, คืนข้อความไทยที่ปลอดภัยให้ client (pattern `safeFacebookErrorMessage` ของ dashboard) - ทุก query/route ที่แตะข้อมูลเพจ ต้องเช็กว่า user มีสิทธิ์ใน **เพจนั้น** (`PageMember`) ไม่ใช่แค่ login แล้ว - การกระทำที่เปลี่ยนสถานะ (อนุมัติ, เผยแพร่, ยกเลิก, เชื่อม/ถอดเพจ, เปลี่ยนสิทธิ์) append ลง `AuditLog` — ห้ามแก้/ลบแถว - Webhook route: verify `X-Hub-Signature-256` จาก raw body ก่อน parse, ตอบ 200 เสมอหลังผ่าน signature - เปลี่ยน schema → สร้าง Prisma migration (`npx prisma migrate dev --name <ชื่อ>`) ห้ามแก้ DB ตรง - UI ภาษาไทย, code/comment/commit ภาษาอังกฤษ - เอกสารภาษาไทยปนศัพท์เทคนิคอังกฤษ เหมือน workspace อื่นของ 4tech # This is NOT the Next.js you know This version has breaking changes — APIs, conventions, and file structure may all differ from your training data. Read the relevant guide in `node_modules/next/dist/docs/` (resolved from this file's directory; in monorepos the `next` package may not be visible from the repo root) before writing any code. Heed deprecation notices. This block is written and re-added by `next dev` — verify at `node_modules/next/dist/server/lib/generate-agent-files.js`. Removing it from a diff only re-creates the uncommitted change; committing it with your work keeps the tree clean.