🔔 Notifications Center Module
📖 Overview
The Notifications Center module provides unified in-app notification management, combining CRM reminders and Firebase Cloud Messaging (FCM) push notifications into a single interface. Users can view unread counts, mark notifications as read, and filter by module/type.
File Path: internal/api/v1/notifications-center/
Key Features
- Unified Count API: Single endpoint for all unread notifications
- CRM Reminders: Task and event reminders from CRM module
- FCM Notifications: Push notifications from various modules
- Bulk Read Marking: Mark all notifications as read
- Module Filtering: Filter notifications by source module (projects, CRM, etc.)
- User-Specific: Per-user read/unread tracking
� Directory Structure
notifications-center/
├── 📄 index.js - Module router
├── 📂 controllers/
│ ├── common.controller.js - Unified notification endpoints
│ ├── fcm.controller.js - FCM-specific operations
│ └── reminder.controller.js - Reminder-specific operations
├── 📂 services/
│ ├── common.service.js - Cross-notification business logic
│ ├── fcm.service.js - FCM notification handling
│ └── reminder.service.js - Reminder operations
├── 📂 routes/
│ └── index.js - Route definitions
└── 📂 validations/
└── (validation schemas)
�🗄️ Collections Used
crm.reminders
- Purpose: Store CRM task/event reminders
- Model:
shared/models/reminder.js - Key Fields (for notifications):
account(ObjectId) - Account referenceassigned(ObjectId) - Assigned userread_by(Array[ObjectId]) - Users who read this remindertitle(String) - Reminder titledue_date(Date) - When reminder is due
fcm-notifications
- Purpose: Store FCM push notifications
- Model:
shared/models/fcm-notification.js - Key Fields:
account(ObjectId) - Account referenceusers(Array[ObjectId]) - Target usersmodule(String) - Source module ('projects', 'crm', etc.)type(String) - Notification typeread_by(Array[ObjectId]) - Users who read this notificationremoved_by(Array[ObjectId]) - Users who dismissed this notificationtitle(String) - Notification titlebody(String) - Notification messagedata(Object) - Additional metadata
🔧 Core Functions
Get Notification Counts
Endpoint: GET /api/v1/notifications-center/count
Query Parameters:
module(String, optional) - Filter by module ('projects', 'crm')type(String, optional) - Filter by notification type
Response:
{
"success": true,
"data": {
"reminder": {
"total": 5
},
"fcm": {
"total": 12,
"project": 7
},
"total": 17
}
}
Logic (services/common.service.js):
// Count unread reminders assigned to user
const reminderOptions = {
account: accountObjectId,
assigned: uidObjectId,
read_by: { $ne: uidObjectId }, // Not in read_by array
};
// Count unread FCM notifications
const fcmOptions = {
account: accountObjectId,
users: uidObjectId, // User is a recipient
removed_by: { $ne: uidObjectId }, // Not dismissed
read_by: { $ne: uidObjectId }, // Not read
};
// Aggregation provides total + project-specific counts
Mark All as Read
Endpoint: POST /api/v1/notifications-center/read
Description: Marks all unread notifications as read for authenticated user.
Response:
{
"success": true,
"message": "Notifications marked as read successfully",
"data": {
"stats": {
"reminderUpdated": 3,
"fcmUpdated": 8
}
}
}
Logic:
// Add user ID to read_by arrays
const updateOperation = { $addToSet: { read_by: uidObjectId } };
// Update both collections in parallel
await Promise.all([
CRMReminder.updateMany(baseFilter, updateOperation),
FcmNotification.updateMany(fcmFilter, updateOperation),
]);
🔀 Integration Points
CRM Module
Reminders Creation: When CRM tasks/events are created with reminders, entries are added to crm.reminders collection.
Notification Display: Notifications Center aggregates these reminders for unread count display.
Projects Module
FCM Notifications: Project updates (task assignments, comments, status changes) trigger FCM notifications with module: 'projects'.
Filtering: Notifications Center can filter by module: 'projects' to show project-specific counts.
General Socket Service
Real-Time Updates: When new notifications arrive, socket events notify connected clients to refresh notification counts.
⚠️ Important Notes
- 📋 Two Data Sources: Combines CRM reminders + FCM notifications
- 👤 Per-User Tracking: Uses
read_byarrays for multi-user accounts - 🗑️ Soft Delete: FCM notifications use
removed_byinstead of hard delete - 🔢 Count Optimization: Uses aggregation pipelines with
$countfor performance - 🎯 Module Filtering: FCM notifications support module-based filtering
- 📊 Disk Usage: Queries use
allowDiskUse(true)for large datasets
📈 Performance Considerations
Critical Indexes:
// crm.reminders
{ account: 1, assigned: 1, read_by: 1 }
// fcm-notifications
{ account: 1, users: 1, read_by: 1, removed_by: 1 }
{ account: 1, users: 1, module: 1, read_by: 1 } // For filtered queries
Optimization Tips:
- Aggregation pipelines can be slow on large collections
- Index
read_byandremoved_byfor$nequeries - Consider caching notification counts (5-minute TTL)
- Bulk read operations update multiple documents efficiently
Last Updated: 2025-01-08
Module Path:internal/api/v1/notifications-center/
Main Routes:
GET /api/v1/notifications-center/count- Unread countsPOST /api/v1/notifications-center/read- Mark all read