This commit is contained in:
2026-07-16 09:53:14 +07:00
parent 0fc112a2e8
commit 29cec708a3
1236 changed files with 212848 additions and 0 deletions

View File

@@ -0,0 +1,134 @@
# Sprint 6 Phase 2 Notification Review
## Summary
ดำเนินการสร้าง Notification Center แบบใช้งานจริงบนฐานข้อมูลเดิมเรียบร้อยแล้ว โดยใช้ตาราง `notifications` ที่มีอยู่แล้วในระบบ และเชื่อมเข้ากับ Auth.js, Drizzle, React Query, header notification popover และหน้า `/dashboard/notifications`
งานในรอบนี้ครอบคลุม:
- สร้าง notification data layer และ reusable notification service
- เพิ่ม API สำหรับ list, mark read, mark all read
- เชื่อม notification event เข้ากับ training review, employee import และ master review
- เปลี่ยน notification center/header จาก mock store เป็นข้อมูลจริงจากฐานข้อมูล
- สร้าง notification center page แบบค้นหา กรอง อ่านทีละรายการ และอ่านทั้งหมด
- เพิ่ม audit log สำหรับ mark read และ mark all read
## Files Changed
- `src/app/dashboard/notifications/page.tsx`
- `src/app/api/notifications/route.ts`
- `src/app/api/notifications/[id]/read/route.ts`
- `src/app/api/notifications/read-all/route.ts`
- `src/app/api/training-records/[id]/review/route.ts`
- `src/app/api/import-employees/route.ts`
- `src/app/api/master-review/route.ts`
- `src/proxy.ts`
- `src/components/ui/notification-card.tsx`
- `src/features/audit-logs/server/audit-service.ts`
- `src/features/audit-logs/constants/audit-log-options.ts`
- `src/features/notifications/api/types.ts`
- `src/features/notifications/api/service.ts`
- `src/features/notifications/api/queries.ts`
- `src/features/notifications/api/mutations.ts`
- `src/features/notifications/constants/notification-options.ts`
- `src/features/notifications/server/notification-service.ts`
- `src/features/notifications/server/notification-data.ts`
- `src/features/notifications/components/notifications-listing.tsx`
- `src/features/notifications/components/notifications-page.tsx`
- `src/features/notifications/components/notification-center.tsx`
## Database Changes
- ไม่มีการเปลี่ยน schema
- ไม่มี migration ใหม่
- ใช้งานตารางเดิม `notifications`
## Routes Added
- `GET /api/notifications`
- `PATCH /api/notifications/[id]/read`
- `PATCH /api/notifications/read-all`
- ใช้งานหน้าเดิม `/dashboard/notifications` แต่เปลี่ยนเป็น data-driven implementation
## Notification Events Implemented
- `createNotification()`
- `createTrainingApprovedNotification()`
- `createTrainingRejectedNotification()`
- `createAnnouncementNotification()`
- `createImportCompletedNotification()`
- `createMasterReviewNotification()`
- `markNotificationAsRead()`
- `markAllNotificationsAsRead()`
Workflow ที่เชื่อมแล้ว:
- HRD อนุมัติ training record -> แจ้ง employee
- HRD ปฏิเสธ training record -> แจ้ง employee
- Employee import completed -> แจ้ง HRD ผู้ที่ทำรายการ
- Master review approve/reject -> แจ้ง HRD ผู้ที่ทำรายการ
## Permission Rules
- ผู้ใช้เห็นเฉพาะ notification ของตัวเอง
- HRD เห็นเฉพาะ notification ของตัวเอง
- API บังคับ ownership ที่ server-side ด้วย `organizationId + userId`
- การอ่าน notification ทีละรายการและอ่านทั้งหมดทำได้เฉพาะของตัวเอง
- หน้า `/dashboard/notifications` ใช้ `requireEmployeeDashboardAccess()`
- API `/api/notifications/**` ใช้ `requireOrganizationAccess()`
## Audit Events Added
- `NOTIFICATION_MARK_READ`
- `NOTIFICATION_MARK_ALL_READ`
- `NOTIFICATION_CREATE` สำหรับ system/manual notification flow ที่เปิด audit
หมายเหตุ:
- ไม่ได้ audit notification อัตโนมัติทุกตัว เพื่อลด log noise
- mark read actions ถูก audit ตาม requirement
## Thai Localization Changes
- เปลี่ยน notification center และ notification page เป็นข้อความไทย
- ปรับ label ของ filter, action, state, empty/error/loading ให้เป็นไทย
- ปรับ relative time บน notification card เป็นไทย
## Responsive/Mobile Notes
- Notification popover ยังคงรองรับ mobile width แบบเดิม
- หน้า `/dashboard/notifications` ใช้ stacked filters บนจอเล็ก
- ปุ่ม action และ mark-all ยังใช้งานได้บน mobile
- card layout ไม่ซ้อนทับข้อความและยังรองรับหลายบรรทัด
## Manual Test Checklist
- Employee can see only own notifications.
- HRD can see only own notifications.
- Training approved creates notification for employee.
- Training rejected creates notification for employee.
- Import completed creates notification for HRD.
- Master review action creates notification for HRD.
- Unread count displays correctly.
- Mark as read works.
- Mark all as read works.
- Search works.
- Filter unread/read works.
- Notification center works on mobile.
- Direct API access cannot read other users' notifications.
- Audit log records mark read actions.
## Known Limitations
- ตาราง `notifications.type` เดิมเป็น enum ชุดเก่า (`approved`, `rejected`, `announcement`, `reminder`) จึงต้อง map logical notification type ของ phase นี้เข้ากับ enum เดิมแทนการเพิ่ม enum ใหม่
- filter ประเภท notification ฝั่ง server ใช้การ resolve จาก `type + referenceType` หลัง query ไม่ได้ทำ SQL-native classification
- `createAnnouncementNotification()` ถูกเตรียมไว้แล้ว แต่ announcements module ยังเป็น placeholder จึงยังไม่ได้เชื่อม workflow publish จริงใน phase นี้
- notification center popover แสดงล่าสุด 5 รายการ ส่วนหน้ารวมใช้ pagination
## Next Phase Recommendation
- เชื่อม announcements publish workflow ให้สร้าง notification จริง
- เพิ่ม background delivery strategy หากภายหลังต้องรองรับ email/push
- เพิ่ม bulk cleanup หรือ archive strategy สำหรับ notification จำนวนมาก
- เพิ่ม richer notification linking เช่น deep link ไปหน้ารายละเอียดแบบ anchor/state-aware
- พิจารณาปรับ enum notification type ใน DB เมื่อพร้อมทำ migration อย่างเป็นทางการ