All acceptance criteria have been successfully implemented for the centralized notification service.
File: database/migrations/014_create_notifications.sql
- Created
notification_typeENUM with all required types - Created
notificationstable with proper schema - Added indexes for performance optimization
- Includes CASCADE delete on user deletion
File: src/services/notification.service.ts (Enhanced existing service)
Added:
create(userId, type, payload)method for simplified notification creation- WebSocket integration via
SocketService - Real-time
notification:newevent emission
File: src/services/notification-cleanup.service.ts (New)
- Daily cron job at 2:00 AM
- Auto-deletes notifications older than 90 days
- Manual cleanup trigger available
- Graceful shutdown support
File: src/controllers/notifications.controller.ts (New)
Implements all required endpoints:
getNotifications- Paginated list with unread firstgetUnreadCount- Returns{ count: number }markAsRead- Mark single notification as readmarkAllAsRead- Mark all as readdeleteNotification- Delete single notification
File: src/routes/notifications.routes.ts (New)
- All endpoints properly secured with
authenticatemiddleware - Swagger documentation included
- RESTful route structure
File: src/routes/index.ts (Updated)
- Mounted notifications routes at
/api/v1/notifications - Initialized notification cleanup service
- Added notifications endpoint to API info
Files:
docs/notifications-api.md- Complete API documentationNOTIFICATIONS_QUICK_REFERENCE.md- Quick reference guideNOTIFICATIONS_IMPLEMENTATION_SUMMARY.md- This file
| Criteria | Status | Implementation |
|---|---|---|
| Create notifications table | ✅ | 014_create_notifications.sql |
create(userId, type, payload) method |
✅ | NotificationService.create() |
| All notification types supported | ✅ | ENUM with 12 types |
GET /api/v1/notifications (paginated, unread first) |
✅ | NotificationsController.getNotifications |
PUT /api/v1/notifications/:id/read |
✅ | NotificationsController.markAsRead |
PUT /api/v1/notifications/read-all |
✅ | NotificationsController.markAllAsRead |
DELETE /api/v1/notifications/:id |
✅ | NotificationsController.deleteNotification |
GET /api/v1/notifications/unread-count |
✅ | NotificationsController.getUnreadCount |
Emit notification:new via WebSocket |
✅ | SocketService.emitToUser() |
| Auto-delete notifications > 90 days | ✅ | NotificationCleanupService cron job |
'session_booked' // New session booking created
'session_confirmed' // Session confirmed by mentor
'session_cancelled' // Session cancelled
'session_reminder' // Upcoming session reminder
'payment_received' // Payment successfully received
'payment_failed' // Payment failed
'review_received' // New review received
'escrow_released' // Escrow funds released
'dispute_opened' // New dispute opened
'meeting_confirmed' // Meeting URL generated
'message_received' // New message received
'system_alert' // System notificationWhen a notification is created, it automatically emits a real-time event:
SocketService.emitToUser(userId, 'notification:new', {
notificationId: notification.id,
type: notification.type,
title: notification.title,
message: notification.message,
data: notification.data,
createdAt: notification.created_at
});GET /api/v1/notifications # Get paginated notifications
GET /api/v1/notifications/unread-count # Get unread count
PUT /api/v1/notifications/:id/read # Mark as read
PUT /api/v1/notifications/read-all # Mark all as read
DELETE /api/v1/notifications/:id # Delete notification
- Schedule: Daily at 2:00 AM (cron:
0 2 * * *) - Action: Deletes notifications older than 90 days
- Service:
NotificationCleanupService - Initialization: Automatic on app startup
- Manual Trigger:
notificationCleanupService.triggerCleanup()
import { NotificationService } from '../services/notification.service';
// Create a notification
await NotificationService.create(userId, 'session_booked', {
title: 'New Session Booked',
message: 'You have a new session with John Doe on March 30',
data: {
booking_id: bookingId,
mentor_name: 'John Doe',
scheduled_at: '2026-03-30T14:00:00Z'
}
});
// This will:
// 1. Store notification in database
// 2. Emit 'notification:new' via WebSocket to the user
// 3. Return the created notification recordCREATE TABLE notifications (
id UUID PRIMARY KEY DEFAULT gen_random_uuid(),
user_id UUID NOT NULL REFERENCES users(id) ON DELETE CASCADE,
type notification_type NOT NULL,
title VARCHAR(255) NOT NULL,
message TEXT NOT NULL,
data JSONB DEFAULT '{}'::jsonb,
is_read BOOLEAN DEFAULT FALSE,
created_at TIMESTAMP WITH TIME ZONE DEFAULT NOW(),
updated_at TIMESTAMP WITH TIME ZONE DEFAULT NOW()
);
-- Indexes for performance
CREATE INDEX idx_notifications_user_id ON notifications(user_id);
CREATE INDEX idx_notifications_type ON notifications(type);
CREATE INDEX idx_notifications_is_read ON notifications(is_read);
CREATE INDEX idx_notifications_created_at ON notifications(created_at);
CREATE INDEX idx_notifications_user_unread ON notifications(user_id, is_read) WHERE is_read = FALSE;
CREATE INDEX idx_notifications_user_created ON notifications(user_id, created_at DESC);-
Run Migration
./database/migrate.sh # Unix/Linux/Mac database\migrate.bat # Windows
-
Restart Application
- The notification cleanup service will initialize automatically
- Routes will be mounted at
/api/v1/notifications
-
Verify
# Check API info curl http://localhost:3000/api/v1 # Test notifications endpoint curl -H "Authorization: Bearer TOKEN" \ http://localhost:3000/api/v1/notifications
-
Create Test Notification
await NotificationService.create('test-user-id', 'system_alert', { title: 'Test Notification', message: 'This is a test notification', data: { test: true } });
-
Test WebSocket
socket.on('notification:new', (data) => { console.log('Received notification:', data); });
-
Test Pagination
curl -H "Authorization: Bearer TOKEN" \ "http://localhost:3000/api/v1/notifications?page=1&limit=10"
-
Test Unread Count
curl -H "Authorization: Bearer TOKEN" \ http://localhost:3000/api/v1/notifications/unread-count
- Indexes: Optimized for common queries (user_id, is_read, created_at)
- Pagination: Prevents loading too many notifications at once
- WebSocket: Asynchronous emission doesn't block notification creation
- Cleanup: Runs during low-traffic hours (2:00 AM)
- Cascade Delete: Notifications automatically deleted when user is deleted
- All endpoints require authentication via
authenticatemiddleware - Users can only access their own notifications
- Notification ownership verified before read/delete operations
- SQL injection prevented via parameterized queries
The notification system can be integrated at various points:
-
Booking Events
- Session booked → Notify mentor
- Session confirmed → Notify both parties
- Session cancelled → Notify both parties
-
Payment Events
- Payment received → Notify recipient
- Payment failed → Notify sender
- Escrow released → Notify both parties
-
Review Events
- Review received → Notify reviewed user
-
Dispute Events
- Dispute opened → Notify both parties
- Dispute resolved → Notify both parties
-
System Events
- Maintenance scheduled → Notify all users
- Feature announcements → Notify all users
- Full API Documentation:
docs/notifications-api.md - Quick Reference:
NOTIFICATIONS_QUICK_REFERENCE.md - Migration File:
database/migrations/014_create_notifications.sql
- Push notifications (mobile)
- Email notifications
- SMS notifications
- User notification preferences
- Notification templates
- Notification grouping
- Read receipts
- Notification scheduling
The notification system is fully implemented and ready for use. All acceptance criteria have been met:
- ✅ Database table with proper schema
- ✅ Service method for creating notifications
- ✅ All required API endpoints
- ✅ WebSocket real-time delivery
- ✅ Auto-cleanup cron job
- ✅ Complete documentation
The system is production-ready and can be deployed immediately after running the migration.