Ghost/core/server/api/canary/email-preview.js
Hannah Wolfe 470ed05620
Removed unnecessary email_preview.read serializer
refs: https://github.com/TryGhost/Toolbox/issues/245
refs: https://github.com/TryGhost/Ghost/commit/73f91a524

- we don't need this serializer because the default serializer will do the same thing
- commit 73f91a524 fixes the logic so that the default serializer is called as a fallback
  even though the email_preview serialzier exists, as there's no matching method name
- sadly the route name here is wrong, it should be email_previews plural, but the response format is correct
  to make this work we have to fix the docName and rename the serializer
2022-03-29 13:44:51 +01:00

77 lines
2.1 KiB
JavaScript

const models = require('../../models');
const tpl = require('@tryghost/tpl');
const errors = require('@tryghost/errors');
const mega = require('../../services/mega');
const messages = {
postNotFound: 'Post not found.'
};
const emailPreview = new mega.EmailPreview({
apiVersion: 'canary'
});
module.exports = {
// @TODO make the route match the resource name in 5.0
docName: 'email_previews',
read: {
options: [
'fields',
'memberSegment'
],
validation: {
options: {
fields: ['html', 'plaintext', 'subject']
}
},
data: [
'id',
'status'
],
permissions: true,
async query(frame) {
const options = Object.assign(frame.options, {formats: 'html,plaintext', withRelated: ['authors', 'posts_meta']});
const data = Object.assign(frame.data, {status: 'all'});
const model = await models.Post.findOne(data, options);
if (!model) {
throw new errors.NotFoundError({
message: tpl(messages.postNotFound)
});
}
return emailPreview.generateEmailContent(model, frame.options.memberSegment);
}
},
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 errors.NotFoundError({
message: tpl(messages.postNotFound)
});
}
const {emails = [], memberSegment} = frame.data;
return await mega.mega.sendTestEmail(model, emails, 'canary', memberSegment);
}
}
};