From 05e9f01f30c59917f8e619b3cc7dda1dd46bb713 Mon Sep 17 00:00:00 2001 From: Rish Date: Wed, 13 Nov 2019 09:21:33 +0530 Subject: [PATCH] Added dynamic fields isEnabled/isConfig on bulk email settings no issue Adds 2 new dynamic calculated fields on bulk email settings - `isEnabled` - If mailgun is configured either with config or admin settings `isConfig` - If mail is configured via config directly --- .../utils/serializers/input/settings.js | 6 ++++ .../utils/serializers/output/utils/clean.js | 30 +++++++++---------- 2 files changed, 21 insertions(+), 15 deletions(-) diff --git a/core/server/api/canary/utils/serializers/input/settings.js b/core/server/api/canary/utils/serializers/input/settings.js index 4827cb4652..1e1bff818e 100644 --- a/core/server/api/canary/utils/serializers/input/settings.js +++ b/core/server/api/canary/utils/serializers/input/settings.js @@ -49,6 +49,12 @@ module.exports = { if (['cover_image', 'icon', 'logo'].includes(setting.key)) { setting = url.forSetting(setting); } + + //CASE: Ensure we don't store calculated fields `isEnabled/Config` in bulk email settings + if (setting.key === 'bulk_email_settings') { + const {apiKey, domain, baseUrl, provider} = setting.value ? JSON.parse(setting.value) : ''; + setting.value = JSON.stringify({apiKey, domain, baseUrl, provider}); + } }); // CASE: deprecated, won't accept 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 9ccc09222e..7d72bb1674 100644 --- a/core/server/api/canary/utils/serializers/output/utils/clean.js +++ b/core/server/api/canary/utils/serializers/output/utils/clean.js @@ -140,22 +140,22 @@ const action = (attrs) => { }; const settings = (attrs) => { - // We don't send mail provider API key in settings API if set in config if (_.isArray(attrs)) { - const membersSubscriptionSettingsObj = attrs.find(({key}) => { - return key === 'members_subscription_settings'; - }) || {}; - const membersSubscriptionSettings = membersSubscriptionSettingsObj.value; - if (membersSubscriptionSettings && config.get('mailgunApiKey')) { - let membersSubscriptionSettingsJson = JSON.parse(membersSubscriptionSettings); - delete membersSubscriptionSettingsJson.mailgunApiKey; - delete membersSubscriptionSettingsJson.mailgunDomain; - attrs.forEach((attr) => { - if (attr.key === 'members_subscription_settings') { - attr.value = JSON.stringify(membersSubscriptionSettingsJson); - } - }); - } + attrs.forEach((attr) => { + if (attr.key === 'bulk_email_settings') { + let {provider, apiKey, domain, baseUrl} = attr.value ? JSON.parse(attr.value) : {}; + + const bulkEmailConfig = config.get('bulkEmail'); + const hasMailgunConfig = !!(bulkEmailConfig && bulkEmailConfig.mailgun); + const hasMailgunSetting = !!(apiKey && baseUrl && domain); + + attr.value = JSON.stringify({ + provider, apiKey, domain, baseUrl, + isEnabled: (hasMailgunConfig || hasMailgunSetting), + isConfig: hasMailgunConfig + }); + } + }); } };