mirror of
https://github.com/TryGhost/Ghost.git
synced 2024-12-19 16:42:17 +03:00
Refactored email-preview ctrl to use async/await
refs https://github.com/TryGhost/Team/issues/694 - async/await has been a standard way to handle async code throughout the codebase. Refactoring it before moving code makes it way easier to reason about similarities between multiple controllers
This commit is contained in:
parent
a39dd7255d
commit
9a9866cf59
@ -22,34 +22,35 @@ module.exports = {
|
||||
'status'
|
||||
],
|
||||
permissions: true,
|
||||
query(frame) {
|
||||
async 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 errors.NotFoundError({
|
||||
message: i18n.t('errors.api.posts.postNotFound')
|
||||
});
|
||||
}
|
||||
|
||||
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 model = await models.Post.findOne(data, options);
|
||||
|
||||
const replacements = mega.postEmailSerializer.parseReplacements(emailContent);
|
||||
|
||||
replacements.forEach((replacement) => {
|
||||
emailContent[replacement.format] = emailContent[replacement.format].replace(
|
||||
replacement.match,
|
||||
replacement.fallback || ''
|
||||
);
|
||||
});
|
||||
|
||||
return emailContent;
|
||||
});
|
||||
if (!model) {
|
||||
throw new errors.NotFoundError({
|
||||
message: i18n.t('errors.api.posts.postNotFound')
|
||||
});
|
||||
}
|
||||
|
||||
let emailContent = await mega.postEmailSerializer.serialize(model, {
|
||||
isBrowserPreview: true,
|
||||
apiVersion: 'canary'
|
||||
});
|
||||
if (labs.isSet('emailCardSegments') && frame.options.memberSegment) {
|
||||
emailContent = mega.postEmailSerializer.renderEmailForSegment(emailContent, frame.options.memberSegment);
|
||||
}
|
||||
const replacements = mega.postEmailSerializer.parseReplacements(emailContent);
|
||||
|
||||
replacements.forEach((replacement) => {
|
||||
emailContent[replacement.format] = emailContent[replacement.format].replace(
|
||||
replacement.match,
|
||||
replacement.fallback || ''
|
||||
);
|
||||
});
|
||||
|
||||
return emailContent;
|
||||
}
|
||||
},
|
||||
sendTestEmail: {
|
||||
|
@ -20,30 +20,32 @@ module.exports = {
|
||||
'status'
|
||||
],
|
||||
permissions: true,
|
||||
query(frame) {
|
||||
async 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 errors.NotFoundError({
|
||||
message: i18n.t('errors.api.posts.postNotFound')
|
||||
});
|
||||
}
|
||||
|
||||
return mega.postEmailSerializer.serialize(model, {isBrowserPreview: true, apiVersion: 'v3'}).then((emailContent) => {
|
||||
const replacements = mega.postEmailSerializer.parseReplacements(emailContent);
|
||||
const model = await models.Post.findOne(data, options);
|
||||
|
||||
replacements.forEach((replacement) => {
|
||||
emailContent[replacement.format] = emailContent[replacement.format].replace(
|
||||
replacement.match,
|
||||
replacement.fallback || ''
|
||||
);
|
||||
});
|
||||
|
||||
return emailContent;
|
||||
});
|
||||
if (!model) {
|
||||
throw new errors.NotFoundError({
|
||||
message: i18n.t('errors.api.posts.postNotFound')
|
||||
});
|
||||
}
|
||||
|
||||
let emailContent = await mega.postEmailSerializer.serialize(model, {
|
||||
isBrowserPreview: true,
|
||||
apiVersion: 'v3'
|
||||
});
|
||||
const replacements = mega.postEmailSerializer.parseReplacements(emailContent);
|
||||
|
||||
replacements.forEach((replacement) => {
|
||||
emailContent[replacement.format] = emailContent[replacement.format].replace(
|
||||
replacement.match,
|
||||
replacement.fallback || ''
|
||||
);
|
||||
});
|
||||
|
||||
return emailContent;
|
||||
}
|
||||
},
|
||||
sendTestEmail: {
|
||||
|
Loading…
Reference in New Issue
Block a user