tom-microservice
Version:
Tom creates customers, subscriptions plans & send notifications.
74 lines (59 loc) • 2.15 kB
JavaScript
'use strict'
const { pick, isNil, get } = require('lodash')
const nodemailer = require('nodemailer')
const Mailgen = require('mailgen')
const pupa = require('pupa')
const { wardCredential, ward, is } = require('../../ward')
const compile = require('../../compile')
module.exports = ({ config }) => {
const errFn = wardCredential(config, [
{ key: 'email.transporter.auth.user', env: 'TOM_EMAIL_USER' },
{ key: 'email.transporter.auth.pass', env: 'TOM_EMAIL_PASSWORD' }
])
if (errFn) return errFn
const transporter = nodemailer.createTransport(
get(config, 'email.transporter')
)
const mailGenerator = new Mailgen({
theme: get(config, 'email.theme', 'default'),
product: {
name: get(config, 'company.site'),
link: get(config, 'company.link'),
logo: get(config, 'company.logo'),
copyright: pupa(get(config, 'company.copyright'), {
company: get(config, 'company'),
year: new Date().getFullYear()
})
}
})
const templates = get(config, 'email.template')
const email = async ({ headers, ipAddress, ...opts }) => {
if (opts.templateId) {
ward(opts.templateId, {
label: 'templateId',
test: is.string.is(x => !isNil(get(templates, x))),
message: `Template '${opts.templateId}' not previously declared.`
})
}
ward(opts.to, {
test: is.string.nonEmpty,
label: 'to',
message: "Need to specify at least a destination as 'to'."
})
const template = get(templates, opts.templateId)
const { html, text, ...mailOpts } = compile(template, { config, opts })
ward(mailOpts.from, { label: 'from', test: is.string.nonEmpty })
ward(mailOpts.subject, { label: 'subject', test: is.string.nonEmpty })
const info = await transporter.sendMail({
...mailOpts,
html: html || text || mailGenerator.generate({ body: mailOpts.body }),
text: text || mailGenerator.generatePlaintext({ body: mailOpts.body })
})
return {
...pick(mailOpts, ['subject', 'from', 'bcc', 'cc']),
...opts,
preview: nodemailer.getTestMessageUrl(info)
}
}
return email
}