7.2 KiB
7.2 KiB
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.tsxsrc/app/api/notifications/route.tssrc/app/api/notifications/[id]/read/route.tssrc/app/api/notifications/read-all/route.tssrc/app/api/training-records/[id]/review/route.tssrc/app/api/import-employees/route.tssrc/app/api/master-review/route.tssrc/proxy.tssrc/components/ui/notification-card.tsxsrc/features/audit-logs/server/audit-service.tssrc/features/audit-logs/constants/audit-log-options.tssrc/features/notifications/api/types.tssrc/features/notifications/api/service.tssrc/features/notifications/api/queries.tssrc/features/notifications/api/mutations.tssrc/features/notifications/constants/notification-options.tssrc/features/notifications/server/notification-service.tssrc/features/notifications/server/notification-data.tssrc/features/notifications/components/notifications-listing.tsxsrc/features/notifications/components/notifications-page.tsxsrc/features/notifications/components/notification-center.tsx
Database Changes
- ไม่มีการเปลี่ยน schema
- ไม่มี migration ใหม่
- ใช้งานตารางเดิม
notifications
Routes Added
GET /api/notificationsPATCH /api/notifications/[id]/readPATCH /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_READNOTIFICATION_MARK_ALL_READNOTIFICATION_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 อย่างเป็นทางการ