Skip to content

Micro-service to handle notifications. Project developed during the Node.js Ignite Lab 4.0

License

Notifications You must be signed in to change notification settings

DiegoVictor/notifications-service

Repository files navigation

Notifications Service

CircleCI NestJS eslint jest coverage MIT License PRs Welcome
Run in Insomnia}

Small microservice to handle notifications, it allows you to send and cancel notifications, mark as read/unread and get the recipient notifications list.

Table of Contents

Installing

Easy peasy lemon squeezy:

$ yarn

Or:

$ npm install

Was installed and configured the eslint and prettier to keep the code clean and patterned.

Configuring

The application is using just one database: SQLite. For the fastest setup is recommended to use docker-compose, you just need to up all services:

$ docker-compose up -d

Or follow the instructions in SQLite and Kafka sections.

SQLite

It stores all the application's data. You just need to remember to run the migrations:

$ npx prisma migrate dev

See more information on Prisma Migrate.

Kafka

Kafka is used to receive notifications through events:

$ docker-compose up -d zookeeper kafka

Remember to run the application manually, see Usage

Script

The repository is shipped with a script to help you to send random notifications to Kafka:

$ node scripts/send-message.js

.env

In this file you may configure the path to the SQLite database file and Kafka settings. Rename the .env.example in the root directory to .env then just update with your settings.

key description default
DATABASE_URL Path to SQLite file. file:./dev.db
KAFKA_TOPIC Name of the Kafka's topic. notifications.send-notification
KAFKA_BROKER Kafka Broker URL. localhost:9092

Usage

To start up the app run:

$ yarn start:dev

Or:

npm run start:dev

Routes

route HTTP Method params description
/notifications POST Body with notification content, category and recipientId. Create a new notification.
/notifications/recipient/:id GET :id of the recipient. Lists recipient notifications.
/notifications/recipient/:id/count GET :id of the recipient. Count recipient notifications.
/notifications/:id/cancel PATCH :id of the notification. Set notification as canceled.
/notifications/:id/read PATCH :id of the notification. Set notification as read.
/notifications/:id/unread PATCH :id of the notification. Set notification as unread.

Requests

  • POST /notifications

Request body:

{
  "content": "Lorem ipsum dolor sit amet",
  "category": "example",
  "recipientId": "69c9a4f1-adbb-44c7-97ca-eac8eee9f029"
}

Running the tests

Jest was the choice to test the app, to run:

$ yarn test

Or:

$ npm run test

Coverage report

You can see the coverage report inside tests/coverage. They are automatically created after the tests run.