1 | 'use strict';
|
2 |
|
3 | Object.defineProperty(exports, "__esModule", {
|
4 | value: true
|
5 | });
|
6 | exports.useMailer = useMailer;
|
7 |
|
8 | var _awsSdk = require('aws-sdk');
|
9 |
|
10 | var _awsSdk2 = _interopRequireDefault(_awsSdk);
|
11 |
|
12 | var _bodyParser = require('body-parser');
|
13 |
|
14 | var _bodyParser2 = _interopRequireDefault(_bodyParser);
|
15 |
|
16 | var _nodemailer = require('nodemailer');
|
17 |
|
18 | var _nodemailer2 = _interopRequireDefault(_nodemailer);
|
19 |
|
20 | var _modes = require('./modes');
|
21 |
|
22 | var _jwt = require('./jwt');
|
23 |
|
24 | function _interopRequireDefault(obj) { return obj && obj.__esModule ? obj : { default: obj }; }
|
25 |
|
26 | function _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 |
|
28 | function useMailer(app, config) {
|
29 | var _this = this;
|
30 |
|
31 |
|
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 |
|
41 |
|
42 | app.use(_bodyParser2.default.json());
|
43 |
|
44 |
|
45 |
|
46 | _awsSdk2.default.config.update(awsConfig);
|
47 | var smtpTransport = _nodemailer2.default.createTransport({ SES: new _awsSdk2.default.SES({
|
48 | apiVersion: '2010-12-01'
|
49 | }) });
|
50 |
|
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 |
|
58 | function send(emails) {
|
59 | return Promise.all(emails.map(function (email) {
|
60 | return new Promise(function (resolve, reject) {
|
61 | |
62 |
|
63 |
|
64 |
|
65 |
|
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 |
|
85 | if (routePath) {
|
86 | app.post(routePath, hasApiAccess, function () {
|
87 | var _ref = _asyncToGenerator( 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 |
|
121 | return Object.assign({ send: send }, config);
|
122 | } |
\ | No newline at end of file |