UNPKG

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