UNPKG

notificator

Version:

Library for handling custom notifications, emails, APS, GCM...

101 lines (78 loc) 3.21 kB
// Generated by CoffeeScript 1.10.0 (function() { var EmailChannel, EmailTemplate, NotificatorChannel, nodemailer, util, validator, extend = function(child, parent) { for (var key in parent) { if (hasProp.call(parent, key)) child[key] = parent[key]; } function ctor() { this.constructor = child; } ctor.prototype = parent.prototype; child.prototype = new ctor(); child.__super__ = parent.prototype; return child; }, hasProp = {}.hasOwnProperty; nodemailer = require('nodemailer'); validator = require('validator'); util = require('util'); NotificatorChannel = require('../NotificatorChannel'); EmailTemplate = (function(superClass) { extend(EmailTemplate, superClass); function EmailTemplate(subject, text, html) { this.subject = subject; this.text = text; this.html = html; } return EmailTemplate; })(NotificatorChannel.ChannelTemplate); EmailChannel = (function(superClass) { extend(EmailChannel, superClass); function EmailChannel(options) { EmailChannel.__super__.constructor.call(this, options); } EmailChannel.prototype.getTransport = function() { if (!this.transport) { this.transport = nodemailer.createTransport(this.options); } return this.transport; }; EmailChannel.prototype.sendMessage = function(message, destination, callback) { var messageOptions; messageOptions = { from: this.options.sender, to: destination.destination, subject: message.subject, text: message.text, html: message.html }; this.debug(messageOptions); return this.getTransport().sendMail(messageOptions, callback); }; EmailChannel.prototype.validateTemplate = function(template) { EmailChannel.__super__.validateTemplate.call(this, template); if (!(template instanceof EmailTemplate)) { throw new Error('template must be type of EmailTemplate'); } if (!template.subject) { throw new Error('email template must have subject'); } if (!template.text && !template.html) { throw new Error('email template must have text or html'); } }; EmailChannel.prototype.validateDestination = function(destination) { EmailChannel.__super__.validateDestination.call(this, destination); if (!validator.isEmail(destination.destination)) { throw new Error(util.format(destination.destination) + ' is not valid email'); } return true; }; EmailChannel.prototype.wrappedDestination = function(destination) { if (destination != null ? destination.email : void 0) { destination.destination = destination.email; delete destination.email; } return EmailChannel.__super__.wrappedDestination.call(this, destination); }; EmailChannel.prototype.transformTemplate = function(template) { return new EmailTemplate(template.subject, template.text, template.html); }; EmailChannel.prototype.name = function() { return 'Email'; }; return EmailChannel; })(NotificatorChannel); EmailChannel.Template = EmailTemplate; module.exports = EmailChannel; }).call(this);