2018-09-12 11:59:00 +09:00
|
|
|
import { Picker } from 'meteor/meteorhacks:picker';
|
|
|
|
import { getSetting } from 'meteor/vulcan:lib';
|
|
|
|
import VulcanEmail from '../namespace.js';
|
2016-04-13 12:55:21 +09:00
|
|
|
|
2018-09-03 21:04:54 +02:00
|
|
|
Meteor.startup(function() {
|
2017-03-24 10:35:19 +09:00
|
|
|
_.forEach(VulcanEmail.emails, (email, key) => {
|
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-06-24 11:45:23 +09:00
|
|
|
let html;
|
|
|
|
// if email has a custom way of generating test HTML, use it
|
2018-09-12 11:59:00 +09:00
|
|
|
if (typeof email.getTestHTML !== 'undefined') {
|
2016-06-24 11:45:23 +09:00
|
|
|
html = email.getTestHTML.bind(email)(params);
|
|
|
|
} else {
|
2018-09-12 11:59:00 +09:00
|
|
|
const locale = params.query.locale || getSetting('locale');
|
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.)
|
2018-09-03 21:04:54 +02:00
|
|
|
const testVariables =
|
2018-10-20 12:42:55 +09:00
|
|
|
(typeof email.testVariables === 'function' ? email.testVariables(params) : email.testVariables) || {};
|
2018-06-06 11:39:04 +09:00
|
|
|
// delete params.query so we don't pass it to GraphQL query
|
|
|
|
delete params.query;
|
2017-11-30 23:08:27 +09:00
|
|
|
// merge test variables with params from URL
|
2018-09-03 21:04:54 +02:00
|
|
|
const variables = { ...testVariables, ...params };
|
2017-08-28 22:27:07 +09:00
|
|
|
|
2018-09-03 21:04:54 +02:00
|
|
|
const { data: emailTestData, subject, html: builtHtml } = await VulcanEmail.build({
|
|
|
|
emailName: key,
|
|
|
|
variables,
|
|
|
|
locale
|
|
|
|
});
|
2016-04-13 12:34:41 +09:00
|
|
|
|
2018-05-10 09:22:24 +09:00
|
|
|
// remove Strings object to avoid echoing it out
|
|
|
|
delete emailTestData.__;
|
|
|
|
|
2018-09-03 21:04:54 +02:00
|
|
|
html = `
|
|
|
|
${builtHtml}
|
2017-08-19 16:13:59 +09:00
|
|
|
<h4 style="margin: 20px;"><code>Subject: ${subject}</code></h4>
|
2018-10-20 12:42:55 +09:00
|
|
|
<h5 style="margin: 20px;">Variables:</h5>
|
|
|
|
<div style="border: 1px solid #999; padding: 10px 20px; margin: 20px;">
|
|
|
|
<pre><code>${JSON.stringify(variables, null, 2)}</code></pre>
|
|
|
|
</div>
|
|
|
|
<h5 style="margin: 20px;">Data:</h5>
|
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>
|
2018-09-03 21:04:54 +02: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
|
|
|
// return html
|
|
|
|
res.end(html);
|
|
|
|
});
|
2016-04-13 12:55:21 +09:00
|
|
|
|
2016-06-24 11:45:23 +09:00
|
|
|
// raw template
|
2018-09-12 11:59:00 +09:00
|
|
|
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
|
|
|
});
|