Skip to content

Commit

Permalink
feat(admin): add email composing test email
Browse files Browse the repository at this point in the history
  • Loading branch information
Hans Kristian Flaatten committed May 2, 2016
1 parent 98b7fbb commit bcb7c14
Show file tree
Hide file tree
Showing 3 changed files with 40 additions and 6 deletions.
27 changes: 23 additions & 4 deletions apps/admin/controller.js
Original file line number Diff line number Diff line change
Expand Up @@ -40,8 +40,10 @@ app.get('/email', (req, res) => {
});

app.post('/email', (req, res, next) => {
const query = { 'owner.userId': req.session.auth.userId };
const from = req.body.from;
const subject = req.body.subject;
const template = req.body.content;

const context = {
site: {
url: `${req.protocol}://${req.get('host')}`,
Expand All @@ -52,13 +54,15 @@ app.post('/email', (req, res, next) => {
// @TODO
res.end();
} else {
const query = { 'owner.userId': req.session.auth.userId };

ApiUser.findOne(query, (findErr, api) => {
if (findErr) { return next(findErr); }

context.api = api;
context.user = api.contact;

return sendgrid.renderTemplate(template, context, (tempErr, data) => {
return sendgrid.renderTemplate(template, context, (tempErr, rendered) => {
if (tempErr && tempErr.name === 'Template render error') {
const error = { message: tempErr.toString() };
return res.render('admin/email.html', { req, error, body: req.body });
Expand All @@ -67,8 +71,23 @@ app.post('/email', (req, res, next) => {
}

if (req.body.test) {
// @TODO
return res.end();
const to = req.session.auth.email;

return sendgrid.sendTemplate(rendered, subject, from, to, (sendErr, data) => {
let error;

if (sendErr) {
error = sendErr;
} else {
error = {
title: 'Test sendt',
message: `Epost ble sendt til ${to}`,
class: 'positive',
};
}

res.render('admin/email.html', { req, error, body: req.body });
});
}

return res.render('admin/email.html', {
Expand Down
15 changes: 15 additions & 0 deletions lib/sendgrid.js
Original file line number Diff line number Diff line change
Expand Up @@ -12,3 +12,18 @@ module.exports.renderTemplate = (template, context, cb) => {
return cb(err);
}
};

module.exports.sendTemplate = (template, subject, from, to, cb) => {
const email = new sendgrid.Email();

email.addTo(to);
email.subject = subject;
email.from = from;
email.html = template;
email.text = email.html.replace(/(<([^>]+)>)/ig, '');

email.addFilter('templates', 'enable', 1);
email.addFilter('templates', 'template_id', process.env.SENDGRID_TEMPLATE_ID);

sendgrid.send(email, cb);
};
4 changes: 2 additions & 2 deletions views/admin/email.html
Original file line number Diff line number Diff line change
Expand Up @@ -30,15 +30,15 @@ <h2 class="ui header">
<p><strong>Hei {% raw %}{{ user.name }}{% endraw %},</strong></p>
<p>Sett inn din melding her.</p>
<hr>
<p>
<p><small>
Du kan også
<a href="{% raw %}{{ site.url }}{% endraw %}/email/unsubscribe/{% raw%}{{ api._id }}{% endraw %}">
melde deg av disse epostene
</a> eller
<a href="{% raw %}{{ site.url }}{% endraw %}/profil">
endre varslingsinnstillingene dine
</a>.
<p>
<small><p>
{% endif %}</textarea>
</div>
<div class="field">
Expand Down

0 comments on commit bcb7c14

Please sign in to comment.