lisa-box
Version:
Home automation system L.I.S.A.
78 lines • 3.22 kB
JavaScript
;
Object.defineProperty(exports, "__esModule", { value: true });
const common_1 = require("@fabrix/fabrix/dist/common");
const _ = require('lodash');
const ejs = require('ejs');
const path = require('path');
const NOTIFICATION_TYPE = require('../utils/enums').NOTIFICATION_TYPE;
class NotificationService extends common_1.FabrixService {
_buildTemplate(type, templateName, data) {
return new Promise((resolve, reject) => {
ejs.renderFile(path.join(this.app.config.main.paths.templates, type, templateName + '.ejs'), data, {}, (err, str) => {
if (err) {
return reject(err);
}
else {
return resolve(str);
}
});
});
}
dispatchNotification(notification) {
return this.app.orm.Notification.create(notification).then(notification => {
this.sendWebNotification(notification);
if (notification.userId) {
notification.getUser().then(user => {
if (user.mobile && (notification.type === NOTIFICATION_TYPE.AUTO || notification.type === NOTIFICATION_TYPE.SMS)) {
return this._buildTemplate(NOTIFICATION_TYPE.SMS, notification.template, {
user: user,
notification: notification
}).then(str => {
return this.app.services.TwilioService.sendSMSTo(user.mobile, str);
}).then(() => notification);
}
else {
return this._buildTemplate(NOTIFICATION_TYPE.EMAIL, notification.template, {
user: user,
notification: notification
}).then(str => {
return this.app.services.EmailService.send({
to: user.email,
subject: notification.subject,
html: str
}).then(() => notification);
});
}
});
}
return notification;
});
}
sendWebNotification(notification) {
if (notification.userId) {
this.app.sockets.room('user_' + notification.userId).send('notification', notification);
}
else {
this.app.sockets.room('admins').send('notification', notification);
}
}
sendNotification(to, pluginName, title, type, description, image, defaultAction, action, lang, templateName) {
if (_.isObject(to)) {
to = to.id;
}
return this.dispatchNotification({
title: title,
description: description,
icon: image,
lang: lang,
type: type,
defaultAction: defaultAction,
addAction: action,
userId: to,
pluginName: pluginName,
template: templateName
});
}
}
exports.NotificationService = NotificationService;
//# sourceMappingURL=NotificationService.js.map