Notifications Module Documentation - EML Backend
1. Introduction
The Notifications module in EML Backend enables sending alerts, reminders, and automated notifications to users through various channels, such as email, SMS, and WebSockets.
2. Code Structure
modules/notifications/
├── notifications.module.ts # Module definition
├── notifications.controller.ts # HTTP Controller
├── notifications.service.ts # Notification sending logic
├── notifications.gateway.ts # WebSockets for real-time notifications
├── notifications.repository.ts # Database access
├── dto/ # Data Transfer Objects (DTOs)
│ ├── create-notification.dto.ts
│ ├── update-notification.dto.ts
├── entities/ # Database models
│ ├── notification.entity.ts
3. Available Endpoints
3.1 Create a Notification
POST /notifications/create - Schedules a new notification.
Request Example:
curl -X POST http://localhost:3000/notifications/create -H "Content-Type: application/json" -H "Authorization: Bearer eyJhbGciOiJIUzI1NiIsInR5c..." -d '{"userId": "12345", "type": "email", "message": "Your appointment is tomorrow at 10 AM"}'
Response Example:
{
"notificationId": "notif_abc123",
"userId": "12345",
"type": "email",
"message": "Your appointment is tomorrow at 10 AM",
"status": "scheduled"
}
3.2 Get Pending Notifications
GET /notifications/pending - Lists scheduled but not yet sent notifications.
Request Example:
curl -X GET http://localhost:3000/notifications/pending -H "Authorization: Bearer eyJhbGciOiJIUzI1NiIsInR5c..."
Response Example:
[
{
"notificationId": "notif_abc123",
"userId": "12345",
"type": "email",
"message": "Your appointment is tomorrow at 10 AM",
"status": "scheduled"
}
]
3.3 WebSockets: Real-Time Notifications
The module allows sending real-time notifications using WebSockets.
Example WebSockets Connection:
const socket = io('http://localhost:3000');
socket.on('connect', () => {
console.log('Connected to the notifications server');
});
socket.on('newNotification', (data) => {
console.log('New notification received:', data);
});
4. Security and Middleware
- JWT Authentication: All requests require a valid JWT token.
- Notification Filtering: Ensures users receive only relevant notifications.
- Priority Rules: Manages priority for urgent alerts.
Example of middleware for protection:
@UseGuards(AuthGuard('jwt'))
@Post('create')
async createNotification(@Body() notificationDto: CreateNotificationDto) {
return this.notificationService.create(notificationDto);
}
5. Dependencies with Other Modules
- Users: To identify recipients.
- Messages: To manage notifications derived from sent messages.
- Reports: To generate metrics on notification delivery and reception.
6. Additional Notes
- Plans to implement integration with WhatsApp and Push Notifications.
- Considering integration with email providers such as SendGrid or Mailgun.
7. Conclusion
The Notifications module in EML Backend enables the automation of alerts across multiple channels, ensuring effective communication with users.
🚀 This document will be updated as the system evolves.