# Prompt: Permission Template Type Validation Audit & Fix ## Objective ดำเนินการตรวจสอบ วิเคราะห์ และแก้ไขปัญหาในฟอร์มที่เกี่ยวข้องกับ **Permission Template** ซึ่งปัจจุบันพบ Validation Error ดังนี้ ```text Invalid input: expected number, received string ``` เป้าหมายคือให้ระบบสามารถเลือก Permission Template ได้อย่างถูกต้อง โดยไม่มี Type Mismatch ระหว่าง Frontend, Validation Schema และ Backend พร้อมทั้งสร้างเอกสารสรุปผลการดำเนินงานไว้ในโฟลเดอร์ `docs/` --- # Background ระบบกำลังพัฒนาโดยใช้แนวคิด **Permission Template First Architecture** Permission Template เป็นตัวกำหนดสิทธิ์หลักของผู้ใช้งาน เมื่อผู้ใช้เลือก Permission Template จาก Dropdown ไม่ควรเกิด Validation Error และข้อมูลต้องสามารถบันทึกได้ทั้ง Create และ Edit --- # Scope ตรวจสอบทุกส่วนที่เกี่ยวข้องกับ Permission Template ได้แก่ - Form Component - Select Component - TanStack Form - React Hook Form (ถ้ามี) - Zod Schema - DTO - API Request - API Route - Service Layer - Repository - Prisma - Database Mapping - TypeScript Types - Default Values - Edit Form - Create Form รวมถึงไฟล์ Shared ที่เกี่ยวข้อง --- # Phase 1 — Architecture Audit ก่อนแก้ไข ต้องตรวจสอบระบบทั้งหมดก่อน ## ตรวจสอบ ### Frontend - Permission Template Select - Form State - Default Values - Initial Values - Edit Values - Submit Handler ### Validation ตรวจสอบ - zod schema - validator - preprocess - transform ### Backend ตรวจสอบ - Route Handler - Server Action - Service - Repository - Prisma ### Database ตรวจสอบว่า Permission Template ใช้ ``` INT ``` หรือ ``` UUID ``` หรือ ``` String ``` ห้ามเดา ต้องอ้างอิงจากโค้ดจริง --- # Phase 2 — Root Cause Analysis ค้นหาสาเหตุที่แท้จริง เช่น ## Case A Select ส่ง ```ts "1"; ``` แต่ schema ```ts z.number(); ``` --- ## Case B Select ส่ง ```ts "employee"; ``` แต่ database ต้องการ ```ts 1; ``` --- ## Case C Default Value ```ts permissionTemplateId: ""; ``` แต่ schema ```ts z.number(); ``` --- ## Case D Edit Form โหลดข้อมูลมาเป็น ```ts number; ``` แต่ Select ต้องการ ```ts string; ``` --- ## Case E API รับ ```ts string; ``` แต่ backend schema ```ts number; ``` --- ต้องระบุให้ชัดเจนว่า Root Cause ที่แท้จริงคืออะไร พร้อมอธิบายเหตุผล --- # Phase 3 — Fix Frontend ## ตรวจสอบ Select ตัวอย่าง ```tsx