mirror of
https://github.com/TryGhost/Ghost.git
synced 2025-01-06 22:40:14 -05:00
🐛 Fixed email showing as success when an email batch fails to send
no issue - fixed passing of errors up through send/processBatch/processEmail - fixed errant overwrite of email status with a "submitted" status after a failure had occurred
This commit is contained in:
parent
b5ffb38726
commit
8f3ab3c535
1 changed files with 9 additions and 14 deletions
|
@ -16,15 +16,14 @@ const BATCH_SIZE = mailgunProvider.BATCH_SIZE;
|
||||||
* @typedef { Object } BatchResultBase
|
* @typedef { Object } BatchResultBase
|
||||||
* @property { string } data - data that is returned from Mailgun or one which Mailgun was called with
|
* @property { string } data - data that is returned from Mailgun or one which Mailgun was called with
|
||||||
*/
|
*/
|
||||||
class BatchResultBase {}
|
class BatchResultBase {
|
||||||
|
|
||||||
class SuccessfulBatch extends BatchResultBase {
|
|
||||||
constructor(id) {
|
constructor(id) {
|
||||||
super(...arguments);
|
|
||||||
this.id = id;
|
this.id = id;
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
class SuccessfulBatch extends BatchResultBase { }
|
||||||
|
|
||||||
class FailedBatch extends BatchResultBase {
|
class FailedBatch extends BatchResultBase {
|
||||||
constructor(id, error) {
|
constructor(id, error) {
|
||||||
super(...arguments);
|
super(...arguments);
|
||||||
|
@ -93,7 +92,7 @@ module.exports = {
|
||||||
.getFilteredCollectionQuery({filter: `email_id:${emailId}+status:[pending,failed]`}, knexOptions)
|
.getFilteredCollectionQuery({filter: `email_id:${emailId}+status:[pending,failed]`}, knexOptions)
|
||||||
.select('id');
|
.select('id');
|
||||||
|
|
||||||
const batchResults = Promise.map(batchIds, async ({id: emailBatchId}) => {
|
const batchResults = await Promise.map(batchIds, async ({id: emailBatchId}) => {
|
||||||
try {
|
try {
|
||||||
await this.processEmailBatch({emailBatchId, options});
|
await this.processEmailBatch({emailBatchId, options});
|
||||||
return new SuccessfulBatch(emailBatchId);
|
return new SuccessfulBatch(emailBatchId);
|
||||||
|
@ -104,7 +103,7 @@ module.exports = {
|
||||||
|
|
||||||
const successes = batchResults.filter(response => (response instanceof SuccessfulBatch));
|
const successes = batchResults.filter(response => (response instanceof SuccessfulBatch));
|
||||||
const failures = batchResults.filter(response => (response instanceof FailedBatch));
|
const failures = batchResults.filter(response => (response instanceof FailedBatch));
|
||||||
const batchStatus = successes.length ? 'submitted' : 'failed';
|
const emailStatus = failures.length ? 'failed' : 'submitted';
|
||||||
|
|
||||||
let error;
|
let error;
|
||||||
|
|
||||||
|
@ -118,7 +117,7 @@ module.exports = {
|
||||||
|
|
||||||
try {
|
try {
|
||||||
await models.Email.edit({
|
await models.Email.edit({
|
||||||
status: batchStatus,
|
status: emailStatus,
|
||||||
results: JSON.stringify(successes),
|
results: JSON.stringify(successes),
|
||||||
error: error,
|
error: error,
|
||||||
error_data: JSON.stringify(failures) // NOTE: need to discuss how we store this
|
error_data: JSON.stringify(failures) // NOTE: need to discuss how we store this
|
||||||
|
@ -129,8 +128,6 @@ module.exports = {
|
||||||
logging.error(err);
|
logging.error(err);
|
||||||
}
|
}
|
||||||
|
|
||||||
await emailModel.save({status: 'submitted'}, Object.assign({}, knexOptions, {patch: true}));
|
|
||||||
|
|
||||||
return batchResults;
|
return batchResults;
|
||||||
},
|
},
|
||||||
|
|
||||||
|
@ -163,14 +160,12 @@ module.exports = {
|
||||||
|
|
||||||
await emailBatchModel.save({status: 'submitting'}, knexOptions);
|
await emailBatchModel.save({status: 'submitting'}, knexOptions);
|
||||||
|
|
||||||
let result;
|
|
||||||
|
|
||||||
try {
|
try {
|
||||||
// send the email
|
// send the email
|
||||||
const sendResponse = await this.send(emailBatchModel.relations.email.toJSON(), recipientRows);
|
const sendResponse = await this.send(emailBatchModel.relations.email.toJSON(), recipientRows);
|
||||||
|
|
||||||
// update batch success status
|
// update batch success status
|
||||||
result = await emailBatchModel.save({
|
return await emailBatchModel.save({
|
||||||
status: 'submitted',
|
status: 'submitted',
|
||||||
provider_id: sendResponse.id
|
provider_id: sendResponse.id
|
||||||
}, Object.assign({}, knexOptions, {patch: true}));
|
}, Object.assign({}, knexOptions, {patch: true}));
|
||||||
|
@ -187,14 +182,14 @@ module.exports = {
|
||||||
logging.error(ghostError);
|
logging.error(ghostError);
|
||||||
throw ghostError;
|
throw ghostError;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
throw error;
|
||||||
} finally {
|
} finally {
|
||||||
// update all email recipients with a processed_at
|
// update all email recipients with a processed_at
|
||||||
await models.EmailRecipient
|
await models.EmailRecipient
|
||||||
.where({batch_id: emailBatchId})
|
.where({batch_id: emailBatchId})
|
||||||
.save({processed_at: moment()}, Object.assign({}, knexOptions, {patch: true}));
|
.save({processed_at: moment()}, Object.assign({}, knexOptions, {patch: true}));
|
||||||
}
|
}
|
||||||
|
|
||||||
return result;
|
|
||||||
},
|
},
|
||||||
|
|
||||||
/**
|
/**
|
||||||
|
|
Loading…
Reference in a new issue