From 9a9866cf596b25f9f9a5c4addd7040ea2e10bc7b Mon Sep 17 00:00:00 2001 From: Naz Date: Thu, 2 Sep 2021 10:59:00 +0400 Subject: [PATCH] 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 --- core/server/api/canary/email-preview.js | 47 +++++++++++++------------ core/server/api/v3/email-preview.js | 40 +++++++++++---------- 2 files changed, 45 insertions(+), 42 deletions(-) diff --git a/core/server/api/canary/email-preview.js b/core/server/api/canary/email-preview.js index c971bdd945..a8945d2444 100644 --- a/core/server/api/canary/email-preview.js +++ b/core/server/api/canary/email-preview.js @@ -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: { diff --git a/core/server/api/v3/email-preview.js b/core/server/api/v3/email-preview.js index 154fda5610..64b5a7e61e 100644 --- a/core/server/api/v3/email-preview.js +++ b/core/server/api/v3/email-preview.js @@ -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: {