UNPKG

@tendencys/queues

Version:

Librería para manejar notificaciones con Bull y Redis

34 lines (30 loc) 851 B
const Bull = require('bull'); const NotificationSchema = require('../schemas/notifications.schema'); class Notifications { constructor(config) { let name = 'notifications'; if (/localhost/.test(config.env)) { name = `localhost:${name}`; } this.config = { ...config, name, }; this.queue = new Bull(name, config.redis); } async createNotification(payload) { try { const { error } = NotificationSchema.validate(payload); if (error) { throw new Error(error); } return await this.queue.add(`${this.config.name}:send`, payload); } catch (error) { throw new Error(error); } } async close() { await this.queue.close(); } } module.exports = Notifications;