UNPKG

3.86 kBJavaScriptView Raw
1'use strict';
2
3Object.defineProperty(exports, "__esModule", {
4 value: true
5});
6exports.useMailer = useMailer;
7
8var _awsSdk = require('aws-sdk');
9
10var _awsSdk2 = _interopRequireDefault(_awsSdk);
11
12var _bodyParser = require('body-parser');
13
14var _bodyParser2 = _interopRequireDefault(_bodyParser);
15
16var _nodemailer = require('nodemailer');
17
18var _nodemailer2 = _interopRequireDefault(_nodemailer);
19
20var _jwt = require('./jwt');
21
22function _interopRequireDefault(obj) { return obj && obj.__esModule ? obj : { default: obj }; }
23
24function _asyncToGenerator(fn) { return function () { var gen = fn.apply(this, arguments); return new Promise(function (resolve, reject) { function step(key, arg) { try { var info = gen[key](arg); var value = info.value; } catch (error) { reject(error); return; } if (info.done) { resolve(value); } else { return Promise.resolve(value).then(function (value) { step("next", value); }, function (err) { step("throw", err); }); } } return step("next"); }); }; }
25
26function useMailer(app, config) {
27 var _this = this;
28
29 // unpack
30 var accountName = config.accountName,
31 activationRoute = config.activationRoute,
32 awsConfig = config.awsConfig,
33 logger = config.logger,
34 senderName = config.senderName,
35 projectName = config.projectName,
36 routePath = config.routePath,
37 senderMail = config.senderMail;
38 // use
39
40 app.use(_bodyParser2.default.json());
41 // nodemailer
42 // use the ses smtp aws service via nodemailer transport
43 // maybe use also just email-smtp.us-east-1.amazonaws.com
44 _awsSdk2.default.config.update(awsConfig);
45 var smtpTransport = _nodemailer2.default.createTransport({ SES: new _awsSdk2.default.SES({
46 apiVersion: '2010-12-01'
47 }) });
48 // security, by default all the active user can do it
49 var hasApiAccess = config.hasApiAccess;
50 if (!hasApiAccess) {
51 hasApiAccess = function hasApiAccess(req, res, next) {
52 return (0, _jwt.hasSubscribedApiAccess)('active', req, res, next, logger);
53 };
54 }
55 // function
56 function send(emails) {
57 return Promise.all(emails.map(function (email) {
58 return new Promise(function (resolve, reject) {
59 /*
60 if (!IS_PROD) {
61 logger.debug('DEV: would have sent email to', email.to, '(not sending because: !IS_PROD)', email.subject, '\n', email.html)
62 resolve()
63 return
64 }
65 */
66 if (!smtpTransport) {
67 reject('Email smtpTransport not initiated, cannot send email');
68 return;
69 }
70 smtpTransport.sendMail(email, function (error, info) {
71 if (error) {
72 logger.warn('sendEmail error', email, error);
73 reject(error);
74 return;
75 }
76 logger.debug('Email message sent: ' + info);
77 resolve();
78 });
79 });
80 }));
81 }
82 // api
83 if (routePath) {
84 app.post(routePath, hasApiAccess, function () {
85 var _ref = _asyncToGenerator(regeneratorRuntime.mark(function _callee(req, res) {
86 var _req$body, email, emails, info;
87
88 return regeneratorRuntime.wrap(function _callee$(_context) {
89 while (1) {
90 switch (_context.prev = _context.next) {
91 case 0:
92 _req$body = req.body, email = _req$body.email, emails = _req$body.emails;
93
94 if (!emails) {
95 emails = [email];
96 }
97 _context.next = 4;
98 return send(emails);
99
100 case 4:
101 info = _context.sent;
102
103 res.send({ info: info });
104
105 case 6:
106 case 'end':
107 return _context.stop();
108 }
109 }
110 }, _callee, _this);
111 }));
112
113 return function (_x, _x2) {
114 return _ref.apply(this, arguments);
115 };
116 }());
117 }
118 // return
119 return Object.assign({ send: send }, config);
120}
\No newline at end of file