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 _jwt = require('./jwt');
|
21 |
|
22 | function _interopRequireDefault(obj) { return obj && obj.__esModule ? obj : { default: obj }; }
|
23 |
|
24 | 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"); }); }; }
|
25 |
|
26 | function useMailer(app, config) {
|
27 | var _this = this;
|
28 |
|
29 |
|
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 |
|
39 |
|
40 | app.use(_bodyParser2.default.json());
|
41 |
|
42 |
|
43 |
|
44 | _awsSdk2.default.config.update(awsConfig);
|
45 | var smtpTransport = _nodemailer2.default.createTransport({ SES: new _awsSdk2.default.SES({
|
46 | apiVersion: '2010-12-01'
|
47 | }) });
|
48 |
|
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 |
|
56 | function send(emails) {
|
57 | return Promise.all(emails.map(function (email) {
|
58 | return new Promise(function (resolve, reject) {
|
59 | |
60 |
|
61 |
|
62 |
|
63 |
|
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 |
|
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 |
|
119 | return Object.assign({ send: send }, config);
|
120 | } |
\ | No newline at end of file |