This project provides a notification service built with Node.js, TypeScript, TypeORM, and Express. It allows you to send notifications and log them into a database. Additionally, it provides endpoints to retrieve the logs and check the service health.
- Send Notifications: Send notifications with different categories and types.
- Log Notifications: Log all notifications into the database.
- Retrieve Logs: Retrieve logs of all notifications, sorted from newest to oldest.
- Health Check: Check the health status of the service.
The database consists of the following tables:
Column | Type | Description |
---|---|---|
id | INT | Primary Key |
name | VARCHAR | User's name |
VARCHAR | User's email | |
phoneNumber | VARCHAR | User's phone number |
Column | Type | Description |
---|---|---|
id | INT | Primary Key |
name | VARCHAR | Category name |
Column | Type | Description |
---|---|---|
id | INT | Primary Key |
name | VARCHAR | Notification type name |
Column | Type | Description |
---|---|---|
id | INT | Primary Key |
userId | INT | Foreign Key to Users |
categoryId | INT | Foreign Key to Categories |
Column | Type | Description |
---|---|---|
id | INT | Primary Key |
userId | INT | Foreign Key to Users |
notificationTypeId | INT | Foreign Key to NotificationTypes |
Column | Type | Description |
---|---|---|
id | INT | Primary Key |
userId | INT | Foreign Key to Users |
categoryId | INT | Foreign Key to Categories |
notificationTypeId | INT | Foreign Key to NotificationTypes |
message | TEXT | Notification message |
timestamp | TIMESTAMP | Time of notification |
Send a notification and log it into the database.
Payload:
{
"message": "Test Message",
"category_id": "1",
"notification_type_id": "1"
}
Retrieve the logs of all notifications, sorted from newest to oldest.
Response:
[
{
"id": 2,
"message": "Test Message",
"timestamp": "2024-06-06T18:22:11.000Z",
"user": null,
"category": {
"id": 1,
"name": "Sports"
},
"notificationType": {
"id": 1,
"name": "SMS"
}
},
{
"id": 1,
"message": "Test Message",
"timestamp": "2024-06-06T18:17:26.000Z",
"user": null,
"category": {
"id": 1,
"name": "Sports"
},
"notificationType": {
"id": 1,
"name": "SMS"
}
}
]
Check the health status of the service.
Response:
OK
Follow these steps to get the project up and running:
-
Clone the repository:
git clone <repository_url> cd <repository_directory>
-
Install dependencies:
npm install
-
Create environment variables file: Create a
.env
file in the root directory with the following content:DB_HOST=localhost DB_PORT=5432 DB_USERNAME=user DB_PASSWORD=password DB_DATABASE=notifications REDIS_URL=redis://localhost:6379
-
Start Docker containers: Make sure Docker is installed and running on your machine, then start the services with Docker Compose.
docker compose up
-
Run the application:
npm start
The application should now be running and accessible at http://localhost:3000
.
Sending a Notification:
curl -X POST http://localhost:3000/api/send-notifications \
-H "Content-Type: application/json" \
-d '{
"message": "Test Message",
"category_id": "1",
"notification_type_id": "1"
}'
Retrieving Notification Logs:
curl http://localhost:3000/api/notification-logs
Health Check:
curl http://localhost:3000/api/health-check