From 8e3b6116062ff15d2c62a99fea6bac350a5ebcc5 Mon Sep 17 00:00:00 2001 From: Daniel Lockyer Date: Tue, 30 Aug 2022 08:55:38 +0100 Subject: [PATCH] Fixed early return when there are no new email batches - the code in question had the intention of returning early if no new email batches had been created for an Email - there were 2 minor bugs here: - `batchIds` would end up being an array of an array of strings because we just push an array in without the spread operator - we would compare that the returned array equaled zero, which was never the case - this commit fixes these minor issues and adds JSDoc to document the function's return type --- ghost/core/core/server/services/mega/mega.js | 11 +++++++---- 1 file changed, 7 insertions(+), 4 deletions(-) diff --git a/ghost/core/core/server/services/mega/mega.js b/ghost/core/core/server/services/mega/mega.js index deec670ec1..567756853d 100644 --- a/ghost/core/core/server/services/mega/mega.js +++ b/ghost/core/core/server/services/mega/mega.js @@ -306,10 +306,11 @@ async function sendEmailJob({emailModel, options}) { const existingBatchCount = await emailModel.related('emailBatches').count('id'); if (existingBatchCount === 0) { - let newBatchCount; + let newBatchCount = 0; await models.Base.transaction(async (transacting) => { - newBatchCount = await createSegmentedEmailBatches({emailModel, options: {transacting}}); + const emailBatches = await createSegmentedEmailBatches({emailModel, options: {transacting}}); + newBatchCount = emailBatches.length; }); if (newBatchCount === 0) { @@ -423,6 +424,8 @@ function partitionMembersBySegment(memberRows, segments) { * @param {Object} options * @param {Object} options.emailModel - instance of Email model * @param {Object} options.options - knex options + * + * @returns {Promise} */ async function createSegmentedEmailBatches({emailModel, options}) { let memberRows = await getEmailMemberRows({emailModel, options}); @@ -444,11 +447,11 @@ async function createSegmentedEmailBatches({emailModel, options}) { memberSegment: partition === 'unsegmented' ? null : partition, options }); - batchIds.push(emailBatchIds); + batchIds.push(...emailBatchIds); } } else { const emailBatchIds = await createEmailBatches({emailModel, memberRows, options}); - batchIds.push(emailBatchIds); + batchIds.push(...emailBatchIds); } return batchIds;