# Prompt: Enhance Training Policy Management Module (Production Ready) ## Objective ปรับปรุง (Enhance) โมดูล **Training Policy Management** ของระบบ Training Management System (TMS) **งานนี้ไม่ใช่การสร้างระบบใหม่ (Rewrite)** ให้ตรวจสอบโครงสร้างเดิมของระบบก่อน และขยาย (Extend) หรือปรับปรุง (Refactor) ตามความเหมาะสม โดยคง Architecture, Coding Standard และ Business Logic ของระบบเดิมไว้ให้มากที่สุด เป้าหมายคือยกระดับ Policy Management ให้เป็นศูนย์กลางในการกำหนดนโยบายการอบรมขององค์กร (Policy Engine) ที่สามารถรองรับการเติบโตของระบบในอนาคต --- # IMPORTANT ก่อนเริ่มพัฒนา ต้องตรวจสอบโครงสร้างเดิมทั้งหมดของระบบ เช่น - Database - Prisma Schema - Models - Services - Repository - API - Server Actions - Validation - Shared Components - UI Components - Permissions - Audit Log - Status - Existing Policy Module หากพบว่าโมดูลหรือโครงสร้างเดิมสามารถรองรับ Requirement ได้ ให้ - Reuse - Extend - Refactor แทนการสร้างใหม่ ห้าม Rewrite ทั้ง Module --- # Existing Implementation Compatibility ให้ใช้หลัก > Reuse > Extend > Refactor > Create New ห้าม - Duplicate Component - Duplicate API - Duplicate Business Logic - Duplicate Validation ห้ามสร้าง - Table ใหม่ - API ใหม่ - Component ใหม่ หากของเดิมสามารถใช้งานต่อได้ หากจำเป็นต้องสร้าง ต้องอธิบายเหตุผล และกระทบระบบเดิมให้น้อยที่สุด (Minimal Impact) --- # Scope พัฒนาและปรับปรุง - Policy List - View Policy - Create Policy - Edit Policy - Clone Policy - Archive - Restore - Activate - Deactivate - Version History - Audit Log --- # Policy Concept Policy คือชุดกฎที่ใช้กำหนดการทำงานของระบบ เช่น - ชั่วโมงอบรมขั้นต่ำ - ชั่วโมง K/S/A - Mandatory Course - Certificate Policy - Approval Rule - Effective Date - Expiration Date ทุก Feature ในระบบควรอ้างอิง Policy ไม่ Hardcode ค่า --- # Policy Fields ## General - Policy Name \* - Description - Company - Organization (ถ้ามี) - Status - Version - Created By - Updated By - Created At - Updated At --- ## Status รองรับ - Draft - Active - Inactive - Archived --- ## Effective Date - Effective From \* - Effective To รองรับ "No Expiration" --- ## Training Hours - Required Total Hours \* - Required K Hours \* - Required S Hours \* - Required A Hours \* Validation ``` K + S + A ต้องเท่ากับ Required Total ``` --- ## Mandatory Courses Multi Select ค้นหาได้ แสดง - Course Code - Course Name - Category --- ## Certificate Policy - Certificate Required - Validity - Renewal Required --- ## Advanced Settings รองรับในอนาคต - Auto Approve - Allow External Training - Allow Self Submission - Notification Rule --- # Policy List Columns - Name - Version - Status - Effective Date - Updated By - Updated At - Action Search - Name - Description Filter - Status - Company - Year Sorting - Name - Updated - Effective Date - Status Pagination ใช้มาตรฐานเดียวกับระบบ --- # View Policy Read Only แบ่ง Section - General - Hours - Mandatory Courses - Certificate - Advanced - History --- # Create Policy สร้างเป็น Draft Validation ครบ ใช้ Required Style เดิมของระบบ --- # Edit Policy แก้ไขได้ Draft Active Inactive Archived ห้ามแก้ไข - Version - Created By - Created At ทุกครั้งที่ Save - Updated At - Updated By ต้องอัปเดต และสร้าง Audit Log --- # Activate Policy Active ได้เพียง 1 Policy ต่อ Scope หากมี Active อยู่แล้ว แสดง Confirm Dialog ให้ Deactivate ของเดิม ก่อน Activate ตัวใหม่ --- # Deactivate รองรับ Deactivate หากไม่มี Active Policy เหลือ แสดง Warning --- # Clone Clone Policy Copy - Hours - Mandatory - Certificate - Settings ไม่ Copy - Audit - Version - History Default Status Draft Version 1 --- # Archive ใช้ Soft Delete เท่านั้น ห้าม Hard Delete Database ใช้ deleted_at deleted_by หรือ Status = Archived ตามมาตรฐานของระบบ --- # Restore Archived สามารถ Restore กลับมาเป็น Inactive ได้ --- # Version History ทุกครั้งที่ Save สร้าง Version ใหม่ สามารถดูได้ว่า Field ไหน เปลี่ยนอะไร เช่น 30 → 40 --- # Audit Log รองรับ - Create - Edit - Clone - Activate - Deactivate - Archive - Restore Audit Log ต้องเก็บ - User - Role - Action - Date - Old Value - New Value - IP - Browser --- # Permission ใช้ Permission-first เท่านั้น ห้ามใช้ Business Role Permission - policy:view - policy:create - policy:update - policy:delete - policy:restore - policy:clone - policy:activate - policy:audit ทุก API ต้องตรวจสอบ Permission Server Side --- # UI ใช้ - shadcn/ui - TailwindCSS Design System เดิม ห้ามสร้าง Design ใหม่ Layout ``` Policy Management [+ New Policy] Search Filter Table ``` Action Menu - View - Edit - Clone - Activate - Deactivate - Archive - Restore - Version History - Audit Log แสดงตาม Permission --- # Validation ตรวจสอบ - Required Field - Duplicate Name - Hours Validation - Effective Date - Mandatory Course --- # Responsive Desktop Tablet Mobile ต้องใช้งานได้ --- # Performance - Memoization - Server Pagination - Lazy Rendering - ห้าม Fetch ซ้ำ --- # Security ทุก API ต้องตรวจสอบ Permission Server Side ห้ามใช้ Client Permission เพียงอย่างเดียว --- # Existing Structure Review ก่อนเริ่ม Coding ให้ตรวจสอบว่า - Database เดิมมีอะไร - API เดิมมีอะไร - Component เดิมมีอะไร - Validation เดิมมีอะไร จากนั้น Reuse Refactor Extend ตามความเหมาะสม หากจำเป็นต้องสร้างใหม่ ให้ระบุเหตุผล --- # Deliverables เมื่อดำเนินการเสร็จ สร้างเอกสาร docs/policy-management-enhancement-report.md ประกอบด้วย ## 1. Existing Structure Review - สิ่งที่พบ - ส่วนที่ Reuse - ส่วนที่ Refactor - ส่วนที่ Extend - ส่วนที่สร้างใหม่ พร้อมเหตุผล --- ## 2. Files Changed รายการไฟล์ทั้งหมด --- ## 3. Database Changes Migration Column Index Constraint --- ## 4. Permission Matrix Permission ที่ใช้ --- ## 5. Business Rules อธิบายทุก Rule --- ## 6. Versioning Workflow --- ## 7. Audit Log Action ที่รองรับ --- ## 8. UI Screens - List - View - Create - Edit - Version - Audit --- ## 9. Testing - CRUD - Permission - Validation - Responsive - Regression --- ## 10. Remaining Improvements เสนอแนวทางพัฒนาเพิ่มเติม เช่น - Scheduled Activation - Policy Comparison - Rollback Version - Policy Approval Workflow - Notification Before Expiry - Company / Department / Position Specific Policy --- # Success Criteria งานถือว่าเสร็จเมื่อ - ปรับปรุงจากโครงสร้างเดิม ไม่ Rewrite - Reuse Component/API ได้มากที่สุด - CRUD สมบูรณ์ - Clone - Soft Delete - Restore - Activate / Deactivate - Version History - Audit Log - Permission-first ครบทุก Action - Validation ครบ - Responsive สมบูรณ์ - TypeScript ไม่มี Error - ESLint ผ่าน - Build ผ่าน - ไม่มี Regression - รายงาน `docs/policy-management-enhancement-report.md` ถูกสร้างเรียบร้อย