Skip to content

Commit

Permalink
feat(struc): make sendMail method extendable (makeomatic#61)
Browse files Browse the repository at this point in the history
  • Loading branch information
aleksxor authored Feb 19, 2021
1 parent 26871f4 commit 9cd0d30
Show file tree
Hide file tree
Showing 4 changed files with 33 additions and 30 deletions.
9 changes: 5 additions & 4 deletions src/actions/adhoc.js
Original file line number Diff line number Diff line change
@@ -1,17 +1,18 @@
const Promise = require('bluebird');
const { ActionTransport } = require('@microfleet/core');

const sendMail = require('../utils/send-mail');

/**
* Sends message via passed auth params for the account
* @param {Mixed} params { account: Object, email, [ctx] }
* @param {Object} params
* @param {Object} params.account
* @param {Object} params.email
* @param {Object} [params.ctx]
* @return {Promise}
*/
function adhoc({ params }) {
const disposableConnection = this.initDisposableTransport(params.account, { pool: false });
return Promise.using(disposableConnection, (transport) => (
sendMail(transport, params.email, params.ctx)
this.constructor.sendMail(transport, params.email, params.ctx)
));
}

Expand Down
8 changes: 5 additions & 3 deletions src/actions/predefined.js
Original file line number Diff line number Diff line change
@@ -1,14 +1,16 @@
const { ActionTransport } = require('@microfleet/core');
const sendMail = require('../utils/send-mail');

/**
* Sends message via a predefined account
* @param {Mixed} params { account: String, email, [ctx] }
* @param {Object} params
* @param {String} params.account
* @param {Object} params.email
* @param {Object} [params.ctx]
* @return {Promise}
*/
async function predefined({ params }) {
const transport = await this.getTransport(params.account);
return sendMail(transport, params.email, params.ctx);
return this.constructor.sendMail(transport, params.email, params.ctx);
}

module.exports = predefined;
Expand Down
24 changes: 23 additions & 1 deletion src/mailer.js
Original file line number Diff line number Diff line change
Expand Up @@ -3,12 +3,14 @@
const { Microfleet, ConnectorsTypes } = require('@microfleet/core');
const Promise = require('bluebird');
const Errors = require('common-errors');
const is = require('is');
const nodemailer = require('nodemailer');
const inlineBase64 = require('nodemailer-plugin-inline-base64');
const merge = require('lodash/merge');
const defaults = require('lodash/defaults');
const identity = require('lodash/identity');
const { htmlToText } = require('nodemailer-html-to-text');
const render = require('ms-mailer-templates');
const conf = require('./config');

/**
Expand Down Expand Up @@ -50,6 +52,26 @@ class Mailer extends Microfleet {
));
}

/**
* Promise wrapper over smtp transport
* @param {Object} transport
* @param {String|Object} email
* @param {Object} ctx
* @return {Promise}
*/
static async sendMail(transport, email, ctx) {
const renderedTemplate = is.string(email)
? await Promise.props({
...ctx.nodemailer,
html: render(email, ctx.template),
})
: email;

return Promise.fromNode((next) => {
transport.sendMail(renderedTemplate, next);
});
}

/**
* Returns existing transport for the account
* @param {String} accountName [description]
Expand Down Expand Up @@ -77,7 +99,7 @@ class Mailer extends Microfleet {
/**
* Initializes transport with passed credentials
* @param {Object} credentials
* @param {Object} opts
* @param {Object} _opts
* @return {Promise}
*/
async initTransport(credentials, _opts) {
Expand Down
22 changes: 0 additions & 22 deletions src/utils/send-mail.js

This file was deleted.

0 comments on commit 9cd0d30

Please sign in to comment.