Skip to content

mono-js/mono-notifications

Repository files navigation

Mono

Notifications module for Mono

npm version Travis Coverage license

Installation

npm install --save mono-notifications

Usage

Mono notifications library manage your users feed notifications

//Control the notifications workflow (create, read, count, list)
const monoNotification = require('mono-notifications')

Mono notifications also expose the notifications as REST routes

All rest calls need a session that specify the user notifications

Routes

Method URI Query params Body Action
GET /notifications markRead, limit, offset, read Return the notifications
GET /notifications/count Return the number of unread notifications
PUT /notifications/read [notificationId1, ...] Set the notifications as read
PUT /notifications/:id/read Set the specified notification as read

Query params:

  • markRead: Boolean (true, false) Set the notifications as read
  • limit: Number. Limit the returned notifications
  • offset: String (ASC or DESC). Sort the returned notifications
  • read: Boolean (true, false) Get notifications as read or unread

Methods

add

add(userId = string || ObjectID, payload = object): Promise<void>

Insert a new notification for a specific userId with a specific payload

// Add a new notification of the userId '59c0de2dfe8fa448605b1d89' with a specific payload
const notification = monoNotification.add('59c0de2dfe8fa448605b1d89', {
  title: 'mono-notification',
  description: 'mono notification description'
})

count

count(userId = string || ObjectID, read = boolean): Promise<Number>

Return the number of notifications (all, read or unread) for a specific userId

// Return all notifications of the userId '59c0de2dfe8fa448605b1d89'
const notifications = monoNotification.count('59c0de2dfe8fa448605b1d89')
// Return all unread notifications of the userId '59c0de2dfe8fa448605b1d89'
const notifications = monoNotification.count('59c0de2dfe8fa448605b1d89', false)
// Return all read notifications of the userId '59c0de2dfe8fa448605b1d89'
const notifications = monoNotification.count('59c0de2dfe8fa448605b1d89', true)

read

read(userId = string || ObjectID, notificationsId = string || Array<string>): Promise<void>

Set a notification or a list of notifications as read

// Set the notification as read that match '59c0de2dfe8fa448605b1d89' of the userId '59c0de2dfe8fa448605b1d90'
const result = await monoNotification.read('59c0de2dfe8fa448605b1d90', '59c0de2dfe8fa448605b1d89')
// Set the notifications as read that match ['59c0de2dfe8fa448605b1d89','59c0de2dfe8fa448605b1d87'] of the userId '59c0de2dfe8fa448605b1d90'
const result = await monoNotification.read('59c0de2dfe8fa448605b1d90', ['59c0de2dfe8fa448605b1d89', '59c0de2dfe8fa448605b1d87'])

list

list(userId = string || ObjectID, query = { limit, offset, sort, fields, read }): Promise<Array<notification>>

Return all notifications (all, read or unread) for a specific userId

// Get all notifications of the userId '59c0de2dfe8fa448605b1d89'
const notifications = await monoNotification.list('59c0de2dfe8fa448605b1d89')
// Get all unread notifications of the userId '59c0de2dfe8fa448605b1d89'
const notifications = await monoNotification.list('59c0de2dfe8fa448605b1d89', { read: false })
// Get all read notifications of the userId '59c0de2dfe8fa448605b1d89'
const notifications = await monoNotification.list('59c0de2dfe8fa448605b1d89', { read: true })