0
Fork 0
mirror of https://github.com/TryGhost/Ghost.git synced 2025-02-10 23:36:14 -05:00

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
This commit is contained in:
Daniel Lockyer 2022-08-30 08:55:38 +01:00
parent fcd6360869
commit 8e3b611606
No known key found for this signature in database
GPG key ID: D21186F0B47295AD

View file

@ -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<string[]>}
*/
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;