mirror of
https://github.com/TryGhost/Ghost.git
synced 2025-03-11 02:12:21 -05:00
Added support for segmented email content in previews and test emails
refs https://github.com/TryGhost/Team/issues/927 - the `email-cta` card can be segmented so only free or paid members can see the content, it should be possible for authors to preview what that will look like in either case
This commit is contained in:
parent
b5334fe831
commit
dfca0abc93
2 changed files with 18 additions and 6 deletions
|
@ -2,13 +2,15 @@ const models = require('../../models');
|
|||
const i18n = require('../../../shared/i18n');
|
||||
const errors = require('@tryghost/errors');
|
||||
const mega = require('../../services/mega');
|
||||
const labs = require('../../../shared/labs');
|
||||
|
||||
module.exports = {
|
||||
docName: 'email_preview',
|
||||
|
||||
read: {
|
||||
options: [
|
||||
'fields'
|
||||
'fields',
|
||||
'memberSegment'
|
||||
],
|
||||
validation: {
|
||||
options: {
|
||||
|
@ -32,6 +34,10 @@ module.exports = {
|
|||
}
|
||||
|
||||
return mega.postEmailSerializer.serialize(model, {isBrowserPreview: true, apiVersion: 'canary'}).then((emailContent) => {
|
||||
if (labs.isSet('emailCardSegments') && frame.options.memberSegment) {
|
||||
emailContent = mega.postEmailSerializer.renderEmailForSegment(emailContent, frame.options.memberSegment);
|
||||
}
|
||||
|
||||
const replacements = mega.postEmailSerializer.parseReplacements(emailContent);
|
||||
|
||||
replacements.forEach((replacement) => {
|
||||
|
@ -68,8 +74,8 @@ module.exports = {
|
|||
message: i18n.t('errors.api.posts.postNotFound')
|
||||
});
|
||||
}
|
||||
const {emails = []} = frame.data;
|
||||
const response = await mega.mega.sendTestEmail(model, emails, 'canary');
|
||||
const {emails = [], memberSegment} = frame.data;
|
||||
const response = await mega.mega.sendTestEmail(model, emails, 'canary', memberSegment);
|
||||
if (response && response[0] && response[0].error) {
|
||||
throw new errors.EmailError({
|
||||
statusCode: response[0].error.statusCode,
|
||||
|
|
|
@ -72,12 +72,17 @@ const getEmailData = async (postModel, options) => {
|
|||
*
|
||||
* @param {Object} postModel - post model instance
|
||||
* @param {[string]} toEmails - member email addresses to send email to
|
||||
* @param {ValidAPIVersion} options.apiVersion - api version to be used when serializing email data
|
||||
* @param {ValidAPIVersion} apiVersion - api version to be used when serializing email data
|
||||
* @param {ValidMemberSegment} memberSegment
|
||||
*/
|
||||
const sendTestEmail = async (postModel, toEmails, apiVersion) => {
|
||||
const emailData = await getEmailData(postModel, {apiVersion});
|
||||
const sendTestEmail = async (postModel, toEmails, apiVersion, memberSegment) => {
|
||||
let emailData = await getEmailData(postModel, {apiVersion});
|
||||
emailData.subject = `[Test] ${emailData.subject}`;
|
||||
|
||||
if (labs.isSet('emailCardSegments') && memberSegment) {
|
||||
emailData = postEmailSerializer.renderEmailForSegment(emailData, memberSegment);
|
||||
}
|
||||
|
||||
// fetch any matching members so that replacements use expected values
|
||||
const recipients = await Promise.all(toEmails.map(async (email) => {
|
||||
const member = await membersService.api.members.get({email});
|
||||
|
@ -539,4 +544,5 @@ module.exports = {
|
|||
|
||||
/**
|
||||
* @typedef {'v2' | 'v3' | 'v4' | 'canary' } ValidAPIVersion
|
||||
* @typedef {'status:free' | 'status:-free'} ValidMemberSegment
|
||||
*/
|
||||
|
|
Loading…
Add table
Reference in a new issue