Notification System - Technical Implementation
Note: This document covers the technical implementation of the notification system. For user-facing documentation, see the Notification System User Guide.
Important: The notification system is a feature of toto-bo (backoffice), not a standalone system. It provides real-time notifications for backoffice users.
Update (Aug 2025)
- Simplified notifications modal: compact header, All/Unread/Urgent filters, mark-all-read, click-to-mark-read.
- In-app vs email policy: collaborator invites/resets are sent by email; BO-only events (tickets, updates) use inβapp notifications.
- RBAC: notifications APIs enforce owner or admin for read/mark/delete; mark-all-read defaults to requester.
- Sidebar bell uses real counts; list loads via
/api/notifications?userEmail=. Ordering falls back safely if missing indexes. - Ticket events: creating/updating tickets produce notifications; assignment notifies the assignee.
π― Overviewβ
The notification system is a feature of the toto-bo (backoffice) platform, providing real-time notifications, role-based delivery, and advanced management capabilities for backoffice users.
ποΈ Architectureβ
Core Componentsβ
-
Notification Types System (
src/lib/notificationTypes.ts)- 24 different notification types
- Template-based configuration
- Role-based delivery rules
- Priority and expiration settings
-
Notification Service (
src/lib/notificationService.ts)- Singleton pattern implementation
- Firebase integration
- Audit logging integration
- Auto-expiration handling
-
Real-time Hook (
src/hooks/useRealTimeNotifications.ts)- WebSocket-ready architecture
- Auto-refresh capabilities
- State management
- Error handling
-
Toast System (
src/components/ui/ToastNotifications.tsx)- Immediate feedback
- Multiple notification types
- Action buttons
- Auto-dismiss functionality
π Notification Typesβ
Case Managementβ
case_created- New case createdcase_updated- Case information updatedcase_urgent- Urgent case requiring attentioncase_completed- Case successfully completed
Financialβ
donation_received- New donation receiveddonation_failed- Donation payment failedpayment_processed- Payment successfully processedpayment_failed- Payment processing failed
User Managementβ
guardian_registered- New guardian joinedguardian_updated- Guardian profile updatedpartner_joined- New partner organizationcollaborator_invited- Collaborator invitedcollaborator_joined- Collaborator joined teamcollaborator_left- Collaborator left team
Security & Systemβ
security_alert- Security event detectedsystem_maintenance- System maintenanceaudit_log_created- New audit log entrypermission_granted- New permissions grantedpermission_revoked- Permissions revoked
Support & Analyticsβ
support_ticket_created- New support ticketsupport_ticket_updated- Support ticket updatedsupport_ticket_resolved- Support ticket resolvedreport_generated- New report readyanalytics_ready- Analytics data updated
π Security Featuresβ
Role-Based Access Controlβ
- Owner or admin can read/modify notifications (API-enforced)
- Permission-based visibility in UI
- Admin-only destructive actions (delete)
- Audit logging best-effort in ticket/collaborator flows
Permission Matrixβ
// Who receives which notifications
case_created: ["admin", "staff"]
case_urgent: ["admin", "staff"]
donation_received: ["admin", "staff"]
security_alert: ["admin"]
support_ticket_created: ["admin", "staff"]
π¨ User Interfaceβ
Enhanced Notification Centerβ
- Real-time updates
- Priority-based filtering
- Action buttons with deep linking
- Statistics dashboard
- Bulk operations (mark all read)
Toast Notificationsβ
- Immediate feedback
- Priority-based styling
- Action buttons
- Auto-dismiss with configurable duration
- Urgent notifications (no auto-dismiss)
Notification Settings Pageβ
- Per-type configuration
- Channel preferences (in-app, email, push)
- Frequency settings
- Quiet hours configuration
π§ API Endpointsβ
Core Endpointsβ
GET /api/notificationsβ Fetch notifications. Filters:userEmail(preferred),type,read,priority,limit,offset. Owner/admin only.POST /api/notificationsβ Create notification (server-side use only).POST /api/notifications/[id]/mark-readβ Mark as read (owner/admin only).POST /api/notifications/mark-all-readβ Mark all as read for requester (owner/admin; defaults to requester email).DELETE /api/notifications/[id]β Delete notification (owner/admin only).
Featuresβ
- Role-based access control
- Comprehensive filtering
- Pagination support
- Real-time updates
- Audit logging
π Statistics & Analyticsβ
Notification Statsβ
- Total notifications count
- Unread notifications count
- Priority-based breakdown
- User-specific statistics
- Real-time updates
Management Featuresβ
- Bulk operations
- Filtering by type, priority, status
- Search functionality
- Export capabilities
π Real-time Featuresβ
WebSocket Integrationβ
- Live notification updates
- Automatic reconnection
- Error handling
- Connection status monitoring
Auto-refreshβ
- 5-minute intervals
- User activity detection
- Background sync
- Offline support
π― Key Featuresβ
1. Comprehensive Type Systemβ
- 24 notification types
- Template-based configuration
- Role-based delivery
- Priority management
2. Real-time Updatesβ
- WebSocket-ready architecture
- Live notification delivery
- Auto-refresh capabilities
- Connection management
3. Advanced UI/UXβ
- Enhanced notification center
- Toast notifications
- Settings management
- Statistics dashboard
4. Security & Permissionsβ
- Role-based access control
- Permission guards
- Audit logging
- Secure API endpoints
5. Management Toolsβ
- Notification settings page
- Bulk operations
- Filtering and search
- Export capabilities
π Integration Pointsβ
Authenticationβ
- NextAuth integration
- Session-based notifications
- Role-based filtering
RBAC Systemβ
- Permission guards
- Role-based delivery
- Access control
Audit Systemβ
- Action logging
- Notification tracking
- Security monitoring
Databaseβ
- Firebase Firestore
- Real-time listeners
- Offline support
π Performance Optimizationsβ
Cachingβ
- Notification state caching
- Template caching
- Statistics caching
Paginationβ
- Lazy loading
- Infinite scroll
- Efficient queries
Real-timeβ
- WebSocket optimization
- Connection pooling
- Error recovery
π§ͺ Testingβ
Test Coverageβ
- β Notification types validation
- β Service method testing
- β API endpoint testing
- β Component testing
- β Permission testing
- β RBAC integration
- β File structure validation
Test Scriptβ
node scripts/test-notifications.js
π Deployment Readyβ
Production Featuresβ
- Error handling
- Logging
- Monitoring
- Security
- Performance optimization
Configurationβ
- Environment variables
- Feature flags
- Customization options
π Usage Examplesβ
Creating Notifications (server-side)β
import { createNotification } from '@/lib/notifications';
await createNotification({
userEmail: 'admin@example.com',
type: 'support_ticket_created',
title: 'New ticket created',
message: 'Ticket ABC has been created',
priority: 'medium',
metadata: { ticketId: 'ABC' }
});
Marking as read from the UIβ
The modal marks an item as read on click; use the βMark all readβ button for bulk.
π Summaryβ
The notification system provides:
β Simplified, fast modal with real data β RBAC on endpoints (owner/admin) β In-app notifications for BO events; email for collaborator invites/resets β Safe server-side helpers and fallbacks
The system is now ready for production use and provides a solid foundation for all notification needs in the Toto backoffice platform.