Ghost/core/server/api/canary/email-preview.js

65 lines
1.8 KiB
JavaScript
Raw Normal View History

2019-11-05 08:14:26 +03:00
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'
2019-11-05 08:14:26 +03:00
],
permissions: true,
query(frame) {
const options = Object.assign(frame.options, {formats: 'html,plaintext'});
return models.Post.findOne(frame.data, options)
2019-11-05 08:14:26 +03:00
.then((model) => {
if (!model) {
throw new common.errors.NotFoundError({
message: common.i18n.t('errors.api.posts.postNotFound')
});
}
const post = model.toJSON(options);
2019-11-05 08:14:26 +03:00
return mega.postEmailSerializer.serialize(post);
});
}
2019-11-05 12:09:07 +03:00
},
sendTestEmail: {
statusCode: 200,
headers: {},
options: [
'id'
],
validation: {
options: {
id: {
required: true
}
}
},
permissions: false,
async query(frame) {
let model = await models.Post.findOne(frame.options);
if (!model) {
throw new common.errors.NotFoundError({
message: common.i18n.t('errors.api.posts.postNotFound')
});
}
const post = model.toJSON();
const {emails = []} = frame.data;
return mega.mega.sendTestEmail(post, emails);
}
2019-11-05 08:14:26 +03:00
}
};