Files
alla-tms/docs/sprint-6-phase-1-audit-log-review.md
2026-07-16 09:53:14 +07:00

135 lines
6.1 KiB
Markdown

# Sprint 6 Phase 1 Audit Log Review
## Summary
ดำเนินการสร้าง Audit Log Foundation ตามขอบเขต Phase 1 แล้ว โดยใช้ตาราง `audit_logs` เดิมใน schema ปัจจุบัน ไม่ได้เปลี่ยนโครงสร้างฐานข้อมูล และต่อเข้ากับ Auth.js, Route Handlers, React Query, UI table และ RBAC เดิมของระบบ
สิ่งที่ทำหลัก ๆ:
- สร้างโมดูล `audit-logs` สำหรับ query, service, server formatter, และ UI table
- สร้างหน้า `/dashboard/audit-logs`
- สร้าง API:
- `GET /api/audit-logs`
- `GET /api/audit-logs/export`
- เพิ่ม reusable audit service: `logAuditEvent(...)`
- ฝัง audit event ใน authentication และ route หลักตามที่ sprint ระบุ
- จำกัดสิทธิ์หน้าและ API ให้ HRD เท่านั้น
- เพิ่ม loading, empty, error state และ CSV export
## Files Changed
- `src/app/dashboard/audit-logs/page.tsx`
- `src/app/api/audit-logs/route.ts`
- `src/app/api/audit-logs/export/route.ts`
- `src/app/api/training-records/route.ts`
- `src/app/api/training-records/[id]/route.ts`
- `src/app/api/training-records/[id]/review/route.ts`
- `src/app/api/courses/route.ts`
- `src/app/api/courses/[id]/route.ts`
- `src/app/api/training-policies/route.ts`
- `src/app/api/training-policies/[id]/route.ts`
- `src/app/api/import-employees/route.ts`
- `src/app/api/master-review/route.ts`
- `src/auth.ts`
- `src/proxy.ts`
- `src/lib/searchparams.ts`
- `src/features/audit-logs/api/types.ts`
- `src/features/audit-logs/api/service.ts`
- `src/features/audit-logs/api/queries.ts`
- `src/features/audit-logs/constants/audit-log-options.ts`
- `src/features/audit-logs/schemas/audit-log.ts`
- `src/features/audit-logs/server/audit-service.ts`
- `src/features/audit-logs/server/audit-log-data.ts`
- `src/features/audit-logs/components/audit-log-listing.tsx`
- `src/features/audit-logs/components/audit-log-table.tsx`
- `src/features/audit-logs/components/audit-log-toolbar.tsx`
- `src/features/audit-logs/components/audit-log-columns.tsx`
## Database Changes
- ไม่มีการเปลี่ยน schema
- ไม่มี migration ใหม่
- ใช้งานตารางเดิม `audit_logs` จาก `src/db/schema.ts`
## Events Implemented
### Authentication
- `LOGIN_SUCCESS`
- `LOGIN_FAILED`
### Training Records
- `CREATE`
- `UPDATE`
- `DELETE`
### HRD Review
- `APPROVE`
- `REJECT`
### Course Master
- `CREATE`
- `UPDATE`
### Training Policy
- `CREATE`
- `UPDATE`
### Employee Import
- `IMPORT`
### Master Review
- `APPROVE`
- `REJECT`
## Permission Rules
- หน้า `/dashboard/audit-logs` ใช้ `requireHRDDashboardAccess()`
- API `/api/audit-logs` และ `/api/audit-logs/export` ใช้ `requireHRD()`
- เพิ่ม `/api/audit-logs` ใน `src/proxy.ts` เพื่อบังคับ auth ระดับ proxy
- ผู้ใช้ทั่วไปไม่สามารถเข้าดู audit log ได้
## Manual Test Checklist
- เข้าระบบด้วยบัญชีถูกต้อง แล้วตรวจสอบว่ามี `LOGIN_SUCCESS`
- เข้าระบบด้วยรหัสผ่านผิด แล้วตรวจสอบว่ามี `LOGIN_FAILED`
- สร้าง training record แล้วตรวจสอบ event `TRAINING_RECORDS / CREATE`
- แก้ไข training record สถานะ pending แล้วตรวจสอบ event `TRAINING_RECORDS / UPDATE`
- ลบ training record สถานะ pending แล้วตรวจสอบ event `TRAINING_RECORDS / DELETE`
- HRD อนุมัติ record แล้วตรวจสอบ event `HRD_REVIEW / APPROVE`
- HRD ปฏิเสธ record แล้วตรวจสอบ event `HRD_REVIEW / REJECT`
- สร้าง course แล้วตรวจสอบ event `COURSE_MASTER / CREATE`
- แก้ไข course แล้วตรวจสอบ event `COURSE_MASTER / UPDATE`
- สร้าง training policy แล้วตรวจสอบ event `TRAINING_POLICY / CREATE`
- แก้ไข training policy แล้วตรวจสอบ event `TRAINING_POLICY / UPDATE`
- import employee file แล้วตรวจสอบ event `EMPLOYEE_IMPORT / IMPORT`
- approve/reject master review แล้วตรวจสอบ event `MASTER_REVIEW / APPROVE` หรือ `REJECT`
- เปิดหน้า `/dashboard/audit-logs` แล้วทดสอบ:
- ค้นหา
- filter ผู้ใช้งาน
- filter โมดูล
- filter รายการ
- filter วันที่
- export CSV
## Known Limitations
- Login failed ที่ยังผูก organization ไม่ได้ จะไม่แสดงในมุมมององค์กรนั้น เพราะหน้า audit log เป็น organization-scoped
- CSV export จำกัดที่ 1000 แถวต่อครั้งใน implementation ปัจจุบัน
- Search ปัจจุบันเน้นค้นจาก user/module/action/entity ไม่ได้ full-text ภายใน JSON snapshot ทั้งหมด
- ยังไม่ได้เพิ่ม audit event ให้ module อื่นนอกขอบเขต sprint นี้
## Next Phase Recommendation
- เพิ่ม date range picker แบบใช้งานสะดวกขึ้นแทน input date ธรรมดา
- เพิ่ม advanced search บน `old_value` และ `new_value`
- เพิ่ม pagination/export แบบ background job หาก log โตเกินระดับ 1000+ แถว
- เพิ่ม audit coverage ให้ notifications, announcements, users, training matrix และ reports export
- เพิ่ม retention policy หรือ archive strategy หากต้องเก็บ log ระยะยาว