diff --git a/core/server/api/canary/utils/serializers/output/utils/clean.js b/core/server/api/canary/utils/serializers/output/utils/clean.js index 7d72bb1674..7a87293af2 100644 --- a/core/server/api/canary/utils/serializers/output/utils/clean.js +++ b/core/server/api/canary/utils/serializers/output/utils/clean.js @@ -143,7 +143,7 @@ const settings = (attrs) => { if (_.isArray(attrs)) { attrs.forEach((attr) => { if (attr.key === 'bulk_email_settings') { - let {provider, apiKey, domain, baseUrl} = attr.value ? JSON.parse(attr.value) : {}; + const {provider, apiKey, domain, baseUrl} = attr.value ? JSON.parse(attr.value) : {}; const bulkEmailConfig = config.get('bulkEmail'); const hasMailgunConfig = !!(bulkEmailConfig && bulkEmailConfig.mailgun); diff --git a/core/server/services/bulk-email/index.js b/core/server/services/bulk-email/index.js index 380422844b..8cf75173de 100644 --- a/core/server/services/bulk-email/index.js +++ b/core/server/services/bulk-email/index.js @@ -1,34 +1,8 @@ -const {URL} = require('url'); const _ = require('lodash'); -const mailgun = require('mailgun-js'); -const configService = require('../../config'); const common = require('../../lib/common'); -let mailgunInstance; - -function createMailgun(config) { - const baseUrl = new URL(config.baseUrl); - - return mailgun({ - apiKey: config.apiKey, - domain: config.domain, - protocol: baseUrl.protocol, - host: baseUrl.host, - port: baseUrl.port, - endpoint: baseUrl.pathname - }); -} - -const config = configService.get('bulkEmail'); - -if (!config || !config.mailgun) { - common.logging.warn(`Bulk email service is not configured`); -} else { - try { - mailgunInstance = createMailgun(config.mailgun); - } catch (err) { - common.logging.warn(`Bulk email service is not configured`); - } -} +const mailgunProvider = require('./mailgun'); +const configService = require('../../config'); +const mailgunInstance = mailgunProvider.getInstance(); /** * An email address @@ -76,10 +50,11 @@ module.exports = { from: fromAddress, 'recipient-variables': recipientVariables }); + const bulkEmailConfig = configService.get('bulkEmail'); - if (config.mailgun.tag) { + if (bulkEmailConfig && bulkEmailConfig.mailgun && bulkEmailConfig.mailgun.tag) { Object.assign(messageData, { - 'o:tag': config.mailgun.tag + 'o:tag': bulkEmailConfig.mailgun.tag }); } @@ -96,6 +71,10 @@ module.exports = { 'message-id': messageId }; + if (!mailgunInstance) { + return; + } + return await mailgunInstance.events().get(filter); } catch (err) { common.logging.error({err}); diff --git a/core/server/services/bulk-email/mailgun.js b/core/server/services/bulk-email/mailgun.js new file mode 100644 index 0000000000..ebb707c477 --- /dev/null +++ b/core/server/services/bulk-email/mailgun.js @@ -0,0 +1,40 @@ +const {URL} = require('url'); +const mailgun = require('mailgun-js'); +const common = require('../../lib/common'); +const configService = require('../../config'); +const settingsCache = require('../settings/cache'); + +function createMailgun(config) { + const baseUrl = new URL(config.baseUrl); + + return mailgun({ + apiKey: config.apiKey, + domain: config.domain, + protocol: baseUrl.protocol, + host: baseUrl.host, + port: baseUrl.port, + endpoint: baseUrl.pathname + }); +} + +function getInstance() { + const bulkEmailConfig = configService.get('bulkEmail'); + const bulkEmailSetting = settingsCache.get('bulk_email_settings'); + const hasMailgunConfig = !!(bulkEmailConfig && bulkEmailConfig.mailgun); + const hasMailgunSetting = !!(bulkEmailSetting && bulkEmailSetting.apiKey && bulkEmailSetting.baseUrl && bulkEmailSetting.domain); + if (!hasMailgunConfig && !hasMailgunSetting) { + common.logging.warn(`Bulk email service is not configured`); + } else { + try { + let mailgunConfig = hasMailgunConfig ? bulkEmailConfig.mailgun : bulkEmailSetting; + return createMailgun(mailgunConfig); + } catch (err) { + common.logging.warn(`Bulk email service is not configured`); + } + } + return null; +} + +module.exports = { + getInstance: getInstance +};