5.5 KiB
5.5 KiB
Policy Management Enhancement Report
1. Existing Structure Review
- พบว่าโมดูล
training-policyเดิมเชื่อมฐานข้อมูลจริงอยู่แล้ว แต่ยังมี scope ค่อนข้างเล็ก ใช้เพียงyear,total_hours,k_hours,s_hours,a_hours,is_active - ส่วนที่ Reuse:
- โครง
src/features/<feature>/api/{types,service,queries,mutations}.ts - Route Handlers ใต้
src/app/api/training-policies/** useAppForm+ Zod schema เดิม- shared DataTable stack (
DataTable,DataTableToolbar,useDataTable,DataTableColumnHeader) - audit log infrastructure เดิม
- โครง
- ส่วนที่ Refactor:
- เปลี่ยน access control จาก
requireHRD()เป็น permission-first ผ่านrequirePermission(...) - ปรับ list API ให้รองรับ pagination / search / status / sort
- ปรับหน้า dashboard ให้ใช้ query-string driven table pattern แบบ feature อื่นใน repo
- เปลี่ยน access control จาก
- ส่วนที่ Extend:
- detail endpoint
- clone / activate / deactivate endpoints
- history endpoint ที่ดึงจาก audit log
- action menu และ detail sheet ฝั่ง UI
- ส่วนที่สร้างใหม่:
- route handlers สำหรับ
clone,activate,deactivate,history - helper permission dashboard guard แบบ generic
- UI table flow ใหม่สำหรับ policy management
- route handlers สำหรับ
2. Files Changed
src/app/api/training-policies/route.tssrc/app/api/training-policies/[id]/route.tssrc/app/api/training-policies/[id]/clone/route.tssrc/app/api/training-policies/[id]/activate/route.tssrc/app/api/training-policies/[id]/deactivate/route.tssrc/app/api/training-policies/[id]/history/route.tssrc/app/dashboard/training-policy/page.tsxsrc/features/audit-logs/server/audit-log-data.tssrc/features/audit-logs/server/audit-service.tssrc/features/training-policy/api/types.tssrc/features/training-policy/api/service.tssrc/features/training-policy/api/queries.tssrc/features/training-policy/api/mutations.tssrc/features/training-policy/schemas/training-policy.tssrc/features/training-policy/server/training-policy-data.tssrc/features/training-policy/components/training-policy-form.tsxsrc/features/training-policy/components/training-policy-page.tsxsrc/lib/auth/page-guards.ts
3. Database Changes
- ไม่มี migration ในรอบนี้
- ไม่มีการเพิ่ม column / index / constraint
- เลือก reuse schema เดิมเพื่อลดผลกระทบกับ runtime data และให้ enhancement รอบแรก deploy ได้เร็ว
4. Permission Matrix
training_policy:read- list policy
- view policy detail
- view policy history
training_policy:write- create
- edit
- clone
- activate
- deactivate
หมายเหตุ: prompt เป้าหมายต้องการ permission แยกละเอียดกว่านี้ แต่ permission catalog ปัจจุบันมีเพียง read และ write สำหรับโมดูลนี้
5. Business Rules
- 1 องค์กรมี policy active ได้เพียง 1 รายการ
- หากเปิดใช้งาน policy ใหม่ ระบบจะปิด policy อื่นในองค์กรก่อน
- clone จะคัดลอกค่า hours ทั้งหมด แต่ตั้ง
is_active = false - clone จะหา
yearถัดไปที่ยังไม่ซ้ำโดยเริ่มจากsource.year + 1 - create / update ยังบังคับ rule เดิม:
K + S + Aต้องเท่ากับtotalHours - duplicate year ในองค์กรเดียวกันจะถูก block ที่ server
6. Versioning
- รอบนี้ยังไม่มี version table หรือ field-level diff versioning ในฐานข้อมูล
- ใช้ audit history เป็น lightweight history layer ชั่วคราวสำหรับการตรวจย้อนหลัง
7. Audit Log
- รองรับ action:
CREATEUPDATECLONEACTIVATEDEACTIVATE
- history ดึงจาก
audit_logsโดย filtermodule_name = TRAINING_POLICY
8. UI Screens
- List:
- shared DataTable
- search / status filter / pagination / sorting
- row action menu
- View:
- detail sheet
- history section
- Create / Edit:
- sheet + existing TanStack Form pattern
9. Testing
- ตรวจ
npm run lintผ่าน - ตรวจ
npm run buildผ่าน - ไม่มี automated CRUD / permission / responsive regression test เพิ่มในรอบนี้
10. Remaining Improvements
- เพิ่ม schema จริงสำหรับ:
- policy name
- description
- effective date
- archived / restored state
- version
- mandatory courses
- certificate policy
- advanced settings
- แยก permission ให้ละเอียดตาม action (
clone,activate,audit,restore) - เพิ่ม soft delete / restore ตาม archived workflow
- เพิ่ม field-level version comparison
- เพิ่ม scheduled activation / policy comparison / rollback