add support for passing arbitrary non-GraphQL data to email template

This commit is contained in:
SachaG 2017-08-20 17:15:40 +09:00
parent 0a6034aed1
commit 8d6855d09c
4 changed files with 12 additions and 13 deletions

View file

@ -10,11 +10,9 @@ VulcanEmail.addEmails({
customEmail: { customEmail: {
template: "customEmail", template: "customEmail",
path: "/email/custom-email", path: "/email/custom-email",
getProperties() {return {};},
subject() { subject() {
return "My awesome new email"; return "My awesome new email";
}, }
getTestObject() {return {};}
} }
}); });

View file

@ -5,15 +5,12 @@ VulcanEmail.addEmails({
test: { test: {
template: "test", template: "test",
path: "/email/test", path: "/email/test",
getProperties() { data() {
return {date: new Date()}; return {date: new Date()};
}, },
subject() { subject() {
return "This is a test"; return "This is a test";
}, },
getTestObject() {
return {date: new Date()};
}
} }
}); });

View file

@ -96,11 +96,13 @@ VulcanEmail.send = (to, subject, html, text) => {
}; };
VulcanEmail.buildAndSend = async ({ to, email, variables, data }) => { VulcanEmail.buildAndSend = async ({ to, email, variables }) => {
// either use data passed as argument, or execute email's GraphQL query // execute email's GraphQL query
const result = await runQuery(email.query, variables); const result = email.query ? await runQuery(email.query, variables) : {data: {}};
const emailData = data || result.data;
// if email has a data() function, merge its return value with results from the query
const emailData = email.data ? {...result.data, ...email.data()} : result.data;
const subject = typeof email.subject === 'function' ? email.subject(emailData) : email.subject; const subject = typeof email.subject === 'function' ? email.subject(emailData) : email.subject;
const html = VulcanEmail.buildTemplate(VulcanEmail.getTemplate(email.template)(emailData)); const html = VulcanEmail.buildTemplate(VulcanEmail.getTemplate(email.template)(emailData));

View file

@ -19,8 +19,10 @@ Meteor.startup(function () {
} else { } else {
// else get test object (sample post, comment, user, etc.) // else get test object (sample post, comment, user, etc.)
const result = await runQuery(email.query, email.testVariables || {}) const result = email.query ? await runQuery(email.query, email.testVariables || {}) : {data: {}};
const emailTestData = result.data;
// if email has a data() function, merge it with results of query
const emailTestData = email.data ? {...result.data, ...email.data()} : result.data;
const subject = typeof email.subject === 'function' ? email.subject(emailTestData) : email.subject; const subject = typeof email.subject === 'function' ? email.subject(emailTestData) : email.subject;
// then apply email template to properties, and wrap it with buildTemplate // then apply email template to properties, and wrap it with buildTemplate