2021-01-21 11:57:52 +03:00
|
|
|
const models = require('../../models');
|
2021-05-03 19:29:44 +03:00
|
|
|
const i18n = require('../../../shared/i18n');
|
2021-01-21 11:57:52 +03:00
|
|
|
const errors = require('@tryghost/errors');
|
|
|
|
const mega = require('../../services/mega');
|
|
|
|
|
2021-09-02 11:32:21 +03:00
|
|
|
const emailPreview = new mega.EmailPreview({
|
|
|
|
apiVersion: 'v3'
|
|
|
|
});
|
|
|
|
|
2021-01-21 11:57:52 +03:00
|
|
|
module.exports = {
|
|
|
|
docName: 'email_preview',
|
|
|
|
|
|
|
|
read: {
|
|
|
|
options: [
|
|
|
|
'fields'
|
|
|
|
],
|
|
|
|
validation: {
|
|
|
|
options: {
|
|
|
|
fields: ['html', 'plaintext', 'subject']
|
|
|
|
}
|
|
|
|
},
|
|
|
|
data: [
|
|
|
|
'id',
|
|
|
|
'status'
|
|
|
|
],
|
|
|
|
permissions: true,
|
2021-09-02 09:59:00 +03:00
|
|
|
async query(frame) {
|
2021-01-21 11:57:52 +03:00
|
|
|
const options = Object.assign(frame.options, {formats: 'html,plaintext', withRelated: ['authors', 'posts_meta']});
|
|
|
|
const data = Object.assign(frame.data, {status: 'all'});
|
|
|
|
|
2021-09-02 09:59:00 +03:00
|
|
|
const model = await models.Post.findOne(data, options);
|
2021-01-21 11:57:52 +03:00
|
|
|
|
2021-09-02 09:59:00 +03:00
|
|
|
if (!model) {
|
|
|
|
throw new errors.NotFoundError({
|
|
|
|
message: i18n.t('errors.api.posts.postNotFound')
|
2021-01-21 11:57:52 +03:00
|
|
|
});
|
2021-09-02 09:59:00 +03:00
|
|
|
}
|
|
|
|
|
2021-09-02 11:32:21 +03:00
|
|
|
return emailPreview.generateEmailContent(model, frame.options.memberSegment);
|
2021-01-21 11:57:52 +03:00
|
|
|
}
|
|
|
|
},
|
|
|
|
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']});
|
2021-09-02 12:09:56 +03:00
|
|
|
|
2021-01-21 11:57:52 +03:00
|
|
|
if (!model) {
|
|
|
|
throw new errors.NotFoundError({
|
|
|
|
message: i18n.t('errors.api.posts.postNotFound')
|
|
|
|
});
|
|
|
|
}
|
2021-09-02 12:09:56 +03:00
|
|
|
|
2021-01-21 11:57:52 +03:00
|
|
|
const {emails = []} = frame.data;
|
2021-09-02 12:09:56 +03:00
|
|
|
return await mega.mega.sendTestEmail(model, emails, 'v3');
|
2021-01-21 11:57:52 +03:00
|
|
|
}
|
|
|
|
}
|
|
|
|
};
|