2016-11-26 02:46:55 +08:00
|
|
|
import { Picker } from 'meteor/meteorhacks:picker';
|
2017-08-19 16:13:59 +09:00
|
|
|
import { runQuery } from 'meteor/vulcan:lib';
|
2017-08-16 15:07:52 +09:00
|
|
|
import VulcanEmail from '../namespace.js';
|
2016-06-23 16:42:06 +09:00
|
|
|
|
2016-06-24 11:45:23 +09:00
|
|
|
Meteor.startup(function () {
|
2016-04-13 12:55:21 +09:00
|
|
|
|
2017-03-24 10:35:19 +09:00
|
|
|
_.forEach(VulcanEmail.emails, (email, key) => {
|
2016-04-13 12:34:41 +09:00
|
|
|
|
2016-06-24 11:45:23 +09:00
|
|
|
// template live preview routes
|
2017-08-19 16:13:59 +09:00
|
|
|
Picker.route(email.path, async (params, req, res) => {
|
2016-04-13 12:34:41 +09:00
|
|
|
|
2016-06-24 11:45:23 +09:00
|
|
|
let html;
|
2016-04-13 12:34:41 +09:00
|
|
|
|
2016-06-24 11:45:23 +09:00
|
|
|
// if email has a custom way of generating test HTML, use it
|
|
|
|
if (typeof email.getTestHTML !== "undefined") {
|
2016-04-13 12:34:41 +09:00
|
|
|
|
2016-06-24 11:45:23 +09:00
|
|
|
html = email.getTestHTML.bind(email)(params);
|
2016-04-13 12:34:41 +09:00
|
|
|
|
2016-06-24 11:45:23 +09:00
|
|
|
} else {
|
2016-04-13 12:34:41 +09:00
|
|
|
|
2016-06-24 11:45:23 +09:00
|
|
|
// else get test object (sample post, comment, user, etc.)
|
2017-08-28 22:27:07 +09:00
|
|
|
const testVariables = (typeof email.testVariables === 'function' ? email.testVariables() : email.testVariables) || {};
|
2017-08-24 13:16:50 +09:00
|
|
|
const result = email.query ? await runQuery(email.query, testVariables) : {data: {}};
|
2017-08-28 22:27:07 +09:00
|
|
|
|
2017-08-20 17:15:40 +09:00
|
|
|
// if email has a data() function, merge it with results of query
|
2017-08-24 13:16:50 +09:00
|
|
|
const emailTestData = email.data ? {...result.data, ...email.data(testVariables)} : result.data;
|
2017-08-19 16:13:59 +09:00
|
|
|
const subject = typeof email.subject === 'function' ? email.subject(emailTestData) : email.subject;
|
2016-04-13 12:34:41 +09:00
|
|
|
|
2016-06-24 11:45:23 +09:00
|
|
|
// then apply email template to properties, and wrap it with buildTemplate
|
2017-08-19 16:13:59 +09:00
|
|
|
html = VulcanEmail.buildTemplate(VulcanEmail.getTemplate(email.template)(emailTestData));
|
2016-04-13 12:34:41 +09:00
|
|
|
|
2017-06-04 13:56:40 +09:00
|
|
|
html += `
|
2017-08-19 16:13:59 +09:00
|
|
|
<h4 style="margin: 20px;"><code>Subject: ${subject}</code></h4>
|
2017-08-20 17:02:30 +09:00
|
|
|
<div style="border: 1px solid #999; padding: 10px 20px; margin: 20px;">
|
|
|
|
<pre><code>${JSON.stringify(emailTestData, null, 2)}</code></pre>
|
2017-06-04 13:56:40 +09:00
|
|
|
</div>
|
|
|
|
`
|
2016-06-24 11:45:23 +09:00
|
|
|
}
|
2016-04-13 12:55:21 +09:00
|
|
|
|
2016-06-24 11:45:23 +09:00
|
|
|
// return html
|
|
|
|
res.end(html);
|
2016-11-26 02:46:55 +08:00
|
|
|
|
2016-06-24 11:45:23 +09:00
|
|
|
});
|
2016-04-13 12:55:21 +09:00
|
|
|
|
2016-06-24 11:45:23 +09:00
|
|
|
// raw template
|
|
|
|
Picker.route("/email/template/:template", (params, req, res) => {
|
2017-03-24 10:35:19 +09:00
|
|
|
res.end(VulcanEmail.templates[params.template]);
|
2016-06-24 11:45:23 +09:00
|
|
|
});
|
|
|
|
|
|
|
|
});
|
2016-11-26 02:46:55 +08:00
|
|
|
|
|
|
|
});
|