-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Switch pingeon email service to Postmark (#96)
* Switch pingeon email service to Postmark * Add default vars * Add email default vars * Add template maps * Map template names with template ids Refactor code generated vars * Test empty config cases * Fix tests * Fix tests * Add ability set email sender
- Loading branch information
1 parent
3fe4d68
commit 9cd44c9
Showing
21 changed files
with
250 additions
and
171 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -33,6 +33,8 @@ PUBSUB_KEY - pub/sub provider secret key. | |
|
||
EMAIL_KEY - email provider secret key. | ||
EMAIL_FROM - for example [email protected] | ||
EMAIL_DEFAULT_VARS - defaults vars used in email templates | ||
EMAIL_TEMPLATE_MAPS - map template name and template id. | ||
|
||
PUSH_KEY - AWS key. | ||
PUSH_SECRET - AWS secret. | ||
|
@@ -47,6 +49,20 @@ DEFAULT_APP - default app from the list above. | |
SENTRY_DSN - DSN from getsentry.com | ||
``` | ||
Pingeon pass own template variables to help you automate email sending: | ||
- firstName - recipient's first name; | ||
- toEmail - recipient's email; | ||
- currentYear - guess what. | ||
As template Pingeon can use template id or your own name. Just set in env var `EMAIL_TEMPLATE_MAPS`: | ||
```json | ||
{ | ||
"templateName" : "templateId" | ||
} | ||
``` | ||
|
||
## API | ||
|
||
API Docs - [http://docs.pingeon.apiary.io](http://docs.pingeon.apiary.io) | ||
|
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file was deleted.
Oops, something went wrong.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,10 @@ | ||
const Recipient = require('../../services/recipient/model'); | ||
|
||
module.exports = async function (recipientId) { | ||
try { | ||
const { firstName, lastName } = await Recipient.findOne({ _id: recipientId }); | ||
return { firstName, lastName }; | ||
} catch (err) { | ||
return {}; | ||
} | ||
}; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,6 @@ | ||
const config = require('smart-config'); | ||
|
||
module.exports = function (templateName) { | ||
const templatesMap = config.get('email.templatesMap') || {}; | ||
return templatesMap[templateName] || templateName; | ||
}; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,12 @@ | ||
const defaultVars = require('smart-config').get('email.defaultVars') || {}; | ||
const getRecipientNameParts = require('./get-recipient-name-parts'); | ||
|
||
module.exports = async function ({ vars = {}, recipientId, toEmail }) { | ||
const nameParts = await getRecipientNameParts(recipientId); | ||
const generatedVars = { | ||
currentYear: new Date().getFullYear(), | ||
...nameParts, toEmail | ||
}; | ||
return { ...defaultVars, ...generatedVars, ...vars }; | ||
}; | ||
|
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,44 @@ | ||
const debug = require('debug')('app:email'); | ||
|
||
const config = require('smart-config'); | ||
const emailConfig = config.get('email'); | ||
const { promisifyAll } = require('bluebird'); | ||
const postmark = require('postmark'); | ||
const getTemplateId = require('./get-template-id'); | ||
const getTemplateVars = require('./get-template-vars'); | ||
const client = promisifyAll(new postmark.Client(emailConfig.key)); | ||
|
||
const send = async({ email, from, to, cc, bcc, subject, message, template, vars, recipientId }) => { | ||
try { | ||
const toEmail = email || to; | ||
const options = { | ||
From: from || emailConfig.from, | ||
To: toEmail, | ||
Cc: cc, | ||
Bcc: bcc | ||
}; | ||
|
||
if (template) { | ||
const templateId = getTemplateId(template); | ||
const resultVars = await getTemplateVars({ vars, toEmail, recipientId }); | ||
|
||
return await client.sendEmailWithTemplateAsync({ | ||
TemplateId: templateId, | ||
TemplateModel: resultVars, | ||
...options | ||
}); | ||
} | ||
|
||
return await client.sendEmailAsync({ | ||
TextBody: message, | ||
Subject: subject, | ||
...options | ||
}); | ||
|
||
} catch (err) { | ||
debug(err); | ||
return err; | ||
} | ||
}; | ||
|
||
module.exports = { send }; |
This file was deleted.
Oops, something went wrong.
This file was deleted.
Oops, something went wrong.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,20 +1,42 @@ | ||
require('./test-env'); | ||
|
||
const { isMatch } = require('lodash'); | ||
const emailProvider = require('../src/helpers/email'); | ||
const email = '[email protected]'; | ||
const template = 'thank-you-registering'; | ||
const vars = { completeregistration: 'some' }; | ||
const templatesMap = require('smart-config').get('email.templatesMap'); | ||
const email = '[email protected]'; | ||
const cc = email; | ||
const bcc = email; | ||
const template = 'alerts'; | ||
const vars = { | ||
firstName: 'Constantine', | ||
actionUrl: 'http://google.com', | ||
message: 'Alerts test' | ||
}; | ||
const message = 'Hello!'; | ||
const subject = 'Test!'; | ||
|
||
describe('Email send', function () { | ||
this.timeout(100000); | ||
|
||
it('should be sent', done => { | ||
return emailProvider | ||
.send({ email, template, vars }) | ||
.then(res => { | ||
assert.ok(res); | ||
done(); | ||
}); | ||
describe('message', () => { | ||
|
||
it('should be sent', async() => { | ||
const res = await emailProvider.send({ email, message, subject, cc, bcc }); | ||
assert.ok(isMatch(res.TextBody, message)); | ||
assert.ok(isMatch(res.Subject, subject)); | ||
}); | ||
|
||
}); | ||
|
||
describe('template', () => { | ||
|
||
it('should be sent', async() => { | ||
const res = await emailProvider.send({ subject, email, template, vars, cc, bcc }); | ||
|
||
assert.equal(res.TemplateId, templatesMap[template]); | ||
assert.ok(isMatch(res.TemplateModel, vars)); | ||
}); | ||
|
||
}); | ||
|
||
}); |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,55 @@ | ||
require('./test-env'); | ||
const { isMatch } = require('lodash'); | ||
const getTemplateVars = require('../src/helpers/email/get-template-vars'); | ||
|
||
let recipient = { firstName: 'John', lastName: 'Testerson' }; | ||
const defaultVars = require('smart-config').get('email.defaultVars'); | ||
const vars = { | ||
actionUrl: 'http://some.com/any', | ||
message: 'whatever' | ||
}; | ||
const toEmail = '[email protected]'; | ||
|
||
describe('Get template vars', () => { | ||
|
||
describe('User exist', () => { | ||
before(() => request | ||
.post('/recipients') | ||
.send(recipient) | ||
.expect(201) | ||
.expect(({ body }) => { | ||
recipient = body; | ||
})); | ||
|
||
it('should have all expected vars', async() => { | ||
const resultVars = await getTemplateVars({ vars, toEmail, recipientId: recipient.id }); | ||
|
||
assert.equal(resultVars.firstName, recipient.firstName); | ||
assert.equal(resultVars.lastName, recipient.lastName); | ||
assert.equal(resultVars.currentYear, new Date().getFullYear()); | ||
assert.equal(resultVars.toEmail, toEmail); | ||
assert.ok(isMatch(resultVars), defaultVars); | ||
}); | ||
}); | ||
|
||
describe('User not exist', () => { | ||
|
||
let resultVars; | ||
before(async() => { | ||
resultVars = await getTemplateVars({ vars, toEmail, recipientId: helpers.randomId() }); | ||
}); | ||
|
||
it('should have all expected vars', () => { | ||
assert.equal(resultVars.currentYear, new Date().getFullYear()); | ||
assert.equal(resultVars.toEmail, toEmail); | ||
assert.ok(isMatch(resultVars), defaultVars); | ||
}); | ||
|
||
it('should not have user name', () => { | ||
assert(!resultVars.firstName); | ||
assert(!resultVars.lastName); | ||
}); | ||
|
||
}); | ||
|
||
}); |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,12 +1,15 @@ | ||
const awsStub = require('./mocks/aws.stub'); | ||
const postmarkStub = require('./mocks/postmark.stub'); | ||
|
||
awsStub.register(); | ||
postmarkStub.register(); | ||
|
||
const sinon = require('sinon'); | ||
const pubsub = require('../src/helpers/pubsub'); | ||
const pubsubMocks = require('./mocks/pubsub.mock'); | ||
|
||
sinon.stub(pubsub, 'pub', pubsubMocks.pub); | ||
sinon.stub(pubsub, 'sub', pubsubMocks.sub); | ||
sinon.stub(require('../src/helpers/email'), 'send', require('./mocks/email.mock').send); | ||
// sinon.stub(require('../src/helpers/email'), 'send', require('./mocks/email.mock').send); | ||
|
||
module.exports = { awsStub }; |
Oops, something went wrong.