mirror of
https://github.com/TryGhost/Ghost.git
synced 2024-12-21 01:41:46 +03:00
a09a6caf5f
no issue - fixed plaintext templates being word wrapped and breaking across replacement strings - updated `postEmailSerializer.serialize` to return the email template plus a replacements array that can be used for creating Mailgun-like recipient variable objects or more straight forward replacement - updated email-preview API to work with the replacements data to show fallback data when previewing
80 lines
2.6 KiB
JavaScript
80 lines
2.6 KiB
JavaScript
const models = require('../../models');
|
|
const common = require('../../lib/common');
|
|
const mega = require('../../services/mega');
|
|
|
|
module.exports = {
|
|
docName: 'email_preview',
|
|
|
|
read: {
|
|
options: [
|
|
'fields'
|
|
],
|
|
validation: {
|
|
options: {
|
|
fields: ['html', 'plaintext', 'subject']
|
|
}
|
|
},
|
|
data: [
|
|
'id',
|
|
'status'
|
|
],
|
|
permissions: true,
|
|
query(frame) {
|
|
const options = Object.assign(frame.options, {formats: 'html,plaintext', withRelated: ['authors', 'posts_meta']});
|
|
const data = Object.assign(frame.data, {status: 'all'});
|
|
return models.Post.findOne(data, options)
|
|
.then((model) => {
|
|
if (!model) {
|
|
throw new common.errors.NotFoundError({
|
|
message: common.i18n.t('errors.api.posts.postNotFound')
|
|
});
|
|
}
|
|
|
|
return mega.postEmailSerializer.serialize(model, {isBrowserPreview: true}).then(({emailTmpl, replacements}) => {
|
|
// perform replacements using no member data
|
|
replacements.forEach((replacement) => {
|
|
emailTmpl[replacement.format] = emailTmpl[replacement.format].replace(
|
|
replacement.match,
|
|
replacement.fallback || ''
|
|
);
|
|
});
|
|
|
|
return emailTmpl;
|
|
});
|
|
});
|
|
}
|
|
},
|
|
sendTestEmail: {
|
|
statusCode: 200,
|
|
headers: {},
|
|
options: [
|
|
'id'
|
|
],
|
|
validation: {
|
|
options: {
|
|
id: {
|
|
required: true
|
|
}
|
|
}
|
|
},
|
|
permissions: true,
|
|
async query(frame) {
|
|
const options = Object.assign(frame.options, {status: 'all'});
|
|
let model = await models.Post.findOne(options, {withRelated: ['authors']});
|
|
if (!model) {
|
|
throw new common.errors.NotFoundError({
|
|
message: common.i18n.t('errors.api.posts.postNotFound')
|
|
});
|
|
}
|
|
const {emails = []} = frame.data;
|
|
const response = await mega.mega.sendTestEmail(model, emails);
|
|
if (response && response[0] && response[0].error) {
|
|
throw new common.errors.EmailError({
|
|
message: response[0].error.message
|
|
});
|
|
}
|
|
return response;
|
|
}
|
|
}
|
|
};
|