From c488da716fbe78daf58024feb604accee0b2bc84 Mon Sep 17 00:00:00 2001 From: Sachin Maheshwari Date: Thu, 2 Jul 2020 17:46:48 +0530 Subject: [PATCH] sendgrid template id can be overwrite through payload. expecting key 'sendgrid_template_id' in payload --- .circleci/config.yml | 2 +- config/default.js | 2 ++ connect/connectEmailServer.js | 10 ++++------ 3 files changed, 7 insertions(+), 7 deletions(-) diff --git a/.circleci/config.yml b/.circleci/config.yml index 3479b2c..114e500 100644 --- a/.circleci/config.yml +++ b/.circleci/config.yml @@ -82,7 +82,7 @@ workflows: context : org-global filters: branches: - only: ['dev'] + only: ['dev', 'feature/decouple-sendgridtemplateid'] - "build-prod": context : org-global filters: diff --git a/config/default.js b/config/default.js index 7a27aa0..7661e98 100644 --- a/config/default.js +++ b/config/default.js @@ -46,4 +46,6 @@ module.exports = { API_CONTEXT_PATH: process.env.API_CONTEXT_PATH || '/v5/email', + PAYLOAD_SENDGRID_TEMPLATE_KEY: process.env.PAYLOAD_SENDGRID_TEMPLATE_KEY || 'sendgrid_template_id', + }; diff --git a/connect/connectEmailServer.js b/connect/connectEmailServer.js index 777bed0..d41f604 100644 --- a/connect/connectEmailServer.js +++ b/connect/connectEmailServer.js @@ -22,20 +22,18 @@ emailServer.setConfig({ LOG_LEVEL: 'debug' }); // the message is JSON event message, // the callback is function(error, templateId), where templateId is the used SendGrid template id const handler = (topic, message, callback) => { - const templateId = config.TEMPLATE_MAP[topic]; - if (templateId === undefined) { + let templateId = config.TEMPLATE_MAP[topic]; + templateId = _.get(message, config.PAYLOAD_SENDGRID_TEMPLATE_KEY, templateId); + if (!templateId) { return callback(null, { success: false, error: `Template not found for topic ${topic}` }); } - try { service.sendEmail(templateId, message).then(() => { callback(null, { success: true }); }).catch((err) => { + logger.error("Error occurred in sendgrid api calling:", err); callback(null, { success: false, error: err }); }); - } catch (error) { - logger.error('Unknown Error: ', error); - } };