From 08e1268aed862a062df7dea721e1948634b53cc8 Mon Sep 17 00:00:00 2001 From: Kevin Ansfield Date: Tue, 23 Feb 2021 08:48:21 +0000 Subject: [PATCH] Added migration to remove surrounding <> in email_batches.provider_id (#12673) refs https://github.com/TryGhost/Team/issues/221#issuecomment-759105424 - Mailgun responds to an email send with a provider id in the format `` but everywhere else it's used in their API it uses the format `x@y.com` - updates email batch save to strip the brackets, and migration removes brackets from existing records so we no longer have to add special handling for the stored id any time we use it --- .../versions/4.0/21-sanitize-email-batches-provider-id.js | 8 ++++++++ core/server/services/bulk-email/index.js | 2 +- core/server/services/email-analytics/providers/mailgun.js | 4 ---- test/utils/fixtures/data-generator.js | 3 +-- 4 files changed, 10 insertions(+), 7 deletions(-) create mode 100644 core/server/data/migrations/versions/4.0/21-sanitize-email-batches-provider-id.js diff --git a/core/server/data/migrations/versions/4.0/21-sanitize-email-batches-provider-id.js b/core/server/data/migrations/versions/4.0/21-sanitize-email-batches-provider-id.js new file mode 100644 index 0000000000..660fcdb9e3 --- /dev/null +++ b/core/server/data/migrations/versions/4.0/21-sanitize-email-batches-provider-id.js @@ -0,0 +1,8 @@ +const logging = require('../../../../../shared/logging'); +const {createIrreversibleMigration} = require('../../utils'); + +module.exports = createIrreversibleMigration(async (knex) => { + logging.info('Sanitizing provider_id values in email_batches'); + + await knex.raw('UPDATE email_batches SET provider_id = REPLACE(REPLACE(provider_id, "<", ""), ">", "")'); +}); diff --git a/core/server/services/bulk-email/index.js b/core/server/services/bulk-email/index.js index 466647871f..5b3358c06f 100644 --- a/core/server/services/bulk-email/index.js +++ b/core/server/services/bulk-email/index.js @@ -168,7 +168,7 @@ module.exports = { // update batch success status return await emailBatchModel.save({ status: 'submitted', - provider_id: sendResponse.id + provider_id: sendResponse.id.trim().replace(/^<|>$/g, '') }, Object.assign({}, knexOptions, {patch: true})); } catch (error) { // update batch failed status diff --git a/core/server/services/email-analytics/providers/mailgun.js b/core/server/services/email-analytics/providers/mailgun.js index 23e8fcc186..bbe50f57ba 100644 --- a/core/server/services/email-analytics/providers/mailgun.js +++ b/core/server/services/email-analytics/providers/mailgun.js @@ -114,11 +114,7 @@ class EmailAnalyticsMailgunProvider { } normalizeEvent(event) { - // TODO: clean up the <> surrounding email_batches.provider_id values let providerId = event.message && event.message.headers && event.message.headers['message-id']; - if (providerId) { - providerId = `<${providerId}>`; - } return { type: event.event, diff --git a/test/utils/fixtures/data-generator.js b/test/utils/fixtures/data-generator.js index d43da95400..a02040e821 100644 --- a/test/utils/fixtures/data-generator.js +++ b/test/utils/fixtures/data-generator.js @@ -478,8 +478,7 @@ DataGenerator.Content = { { id: ObjectId.generate(), email_id: null, // emails[0] relation added later - // TODO: cleanup <> in provider_id - provider_id: '', + provider_id: 'email1@testing.mailgun.net', status: 'submitted' } ],