5.1 KiB
You are a Senior Full Stack Engineer.
Project: Training Management System (TMS)
Current status:
- Sprint 6 Phase 1 Audit Log Foundation is completed and approved.
- Audit service logAuditEvent(...) is available.
- Auth.js, RBAC, Thai localization, responsive layout, and mobile UX are already completed.
Current task: Implement Sprint 6 Phase 2: Notification Center
Important constraints:
- Do NOT change folder structure.
- Do NOT migrate ORM.
- Do NOT rename tables.
- Do NOT rename columns.
- Do NOT break existing routes.
- Reuse existing Auth.js.
- Reuse existing Drizzle ORM.
- Reuse existing UI components.
- Keep Thai UI.
- Keep responsive/mobile behavior.
- Minimal-impact changes only.
- Reuse existing Audit Log service for important notification actions.
Part 1: Notification Data Layer
Requirements:
- Use existing notifications table if available.
- If notifications table already exists, reuse it.
- Do not create a new schema unless absolutely necessary.
- Notification fields should support:
- id
- userId
- title
- message
- type
- referenceType
- referenceId
- isRead
- readAt
- createdAt
Part 2: Notification Service
Create reusable service functions:
- createNotification()
- createTrainingApprovedNotification()
- createTrainingRejectedNotification()
- createAnnouncementNotification()
- createImportCompletedNotification()
- createMasterReviewNotification()
- markNotificationAsRead()
- markAllNotificationsAsRead()
Notification types:
- TRAINING_APPROVED
- TRAINING_REJECTED
- ANNOUNCEMENT_PUBLISHED
- IMPORT_COMPLETED
- MASTER_REVIEW_APPROVED
- MASTER_REVIEW_REJECTED
- SYSTEM
Part 3: Integrate Notification Events
Add notification creation to existing workflows:
-
HRD approves training record
- Notify employee
- Title: รายการอบรมได้รับการอนุมัติ
- Message should include course name, category, and approved hours
-
HRD rejects training record
- Notify employee
- Title: รายการอบรมไม่ผ่านการอนุมัติ
- Message should include course name and reject reason
-
Employee import completed
- Notify HRD user who performed import
- Include total, success, failed count
-
Master review approved/rejected
- Notify HRD user who performed action
- Include master type and action result
Important:
- Do not implement Email notification in this phase.
- In-app notification only.
Part 4: Notification Center UI
Route:
- /dashboard/notifications
Requirements:
- User can view own notifications.
- HRD and Employee both can access notifications.
- Show unread count.
- Show list ordered by latest first.
- Support filter:
- All
- Unread
- Read
- Type
- Support search by title/message.
- Support mark one as read.
- Support mark all as read.
- Support click notification to open related reference if reference route is known.
- Use Thai labels.
- Add loading state.
- Add empty state.
- Add error state.
- Responsive/mobile friendly.
Part 5: Header / Sidebar Badge
Requirements:
- Show unread notification badge in header or sidebar if existing notification UI exists.
- Badge should show unread count.
- Do not break existing header responsive behavior.
Part 6: Permissions
Rules:
- User can only see their own notifications.
- User can only mark their own notifications as read.
- HRD cannot read another user's notifications from the notification center unless explicitly implemented later.
- APIs must enforce user ownership server-side.
Part 7: Audit Log
Use existing logAuditEvent(...) for:
- NOTIFICATION_MARK_READ
- NOTIFICATION_MARK_ALL_READ
- NOTIFICATION_CREATE if created manually or system-level
Do not audit every automatic notification if it creates too much noise, but audit mark-read actions and system notifications.
Part 8: API Routes / Actions
Implement or reuse routes/actions:
- GET /api/notifications
- PATCH /api/notifications/[id]/read
- PATCH /api/notifications/read-all
If existing API path differs, reuse the existing pattern.
Part 9: Output Review File
Create:
docs/sprint-6-phase-2-notification-review.md
The review file must include:
- Summary
- Files Changed
- Database Changes
- Routes Added
- Notification Events Implemented
- Permission Rules
- Audit Events Added
- Thai Localization Changes
- Responsive/Mobile Notes
- Manual Test Checklist
- Known Limitations
- Next Phase Recommendation
Manual Test Checklist must include:
- 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.
Return:
- Complete code changes with file paths.
- Content of docs/sprint-6-phase-2-notification-review.md.
- Migration commands if required.