diff --git a/services/NotificationService.js b/services/NotificationService.js new file mode 100644 index 0000000..76d2c9d --- /dev/null +++ b/services/NotificationService.js @@ -0,0 +1,56 @@ +/* + * Copyright (c) 2022 TopCoder, Inc. All rights reserved. + */ +'use strict'; + +/** + * This service processes incoming pure challenge events. + * + * @author TCSCODER + * @version 1.0 + */ +const Joi = require('joi'); +const logger = require('../utils/logger'); +const notification = require('../utils/notification'); + +/** + * Send token expired notification + * @param {Object} event the event + */ +async function handleTokenExpired(event) { + try { + const {copilotHandle, provider} = event.data; + await notification.sendTokenExpiredAlert(copilotHandle, '', provider); + logger.debug('Send token expired notification success'); + } catch (err) { + logger.debug(`Send token expired notification failed. Internal Error: ${err}`); + } +} + +/** + * Process notification event. + * @param {Object} event the event + */ +async function process(event) { + Joi.attempt(event, process.schema); + + if (event.event === 'notification.tokenExpired') { + await handleTokenExpired(event); + } +} + +process.schema = Joi.object().keys({ + event: Joi.string().valid('notification.tokenExpired').required(), + data: Joi.object().keys({ + copilotHandle: Joi.string().required(), + provider: Joi.string().required() + }).required(), + retryCount: Joi.number().integer().default(0).optional() +}); + + +module.exports = { + process +}; + +logger.buildService(module.exports); diff --git a/utils/kafka-consumer.js b/utils/kafka-consumer.js index c0e6fc5..30655a7 100644 --- a/utils/kafka-consumer.js +++ b/utils/kafka-consumer.js @@ -13,6 +13,7 @@ const healthcheck = require('topcoder-healthcheck-dropin'); const IssueService = require('../services/IssueService'); const CopilotPaymentService = require('../services/CopilotPaymentService'); const ChallengeService = require('../services/ChallengeService'); +const NotificationService = require('../services/NotificationService'); const logger = require('./logger'); const kafka = require('./kafka'); @@ -54,6 +55,12 @@ function messageHandler(messageSet) { .process(event) .catch(logger.error); } + if (event && _.includes(['notification.tokenExpired'] + , event.event)) { + NotificationService + .process(event) + .catch(logger.error); + } }); } diff --git a/utils/notification.js b/utils/notification.js index ad9a8fa..f294275 100644 --- a/utils/notification.js +++ b/utils/notification.js @@ -15,12 +15,25 @@ const logger = require('./logger'); const notification = {}; -const content = `Hi {handle}, -You made an update to ticket {link}, but Topcoder-X couldn't process it properly because your {provider} token has expired. To fix this, please login to x.topcoder.com, click your handle in the upper right and then "Settings" to refresh your token. You will need to redo the action that failed in {provider}.`; // eslint-disable-line max-len +/** + * get content template to send + * @param {String} repoPath the repo path + * @returns {String} + */ +function getContent(repoPath) { + return repoPath + ? + `Hi {handle}, + You made an update to ticket {link}, but Topcoder-X couldn't process it properly because your {provider} token has expired. To fix this, please login to x.topcoder.com, click your handle in the upper right and then "Settings" to refresh your token. You will need to redo the action that failed in {provider}.` // eslint-disable-line max-len + : + `Hi {handle}, + You made an operation on {provider}, but Topcoder-X couldn't process it properly because your {provider} token has expired. To fix this, please login to x.topcoder.com, click your handle in the upper right and then "Settings" to refresh your token. You will need to redo the action that failed in {provider}.`; // eslint-disable-line max-len +} notification.sendTokenExpiredAlert = async function sendTokenExpiredAlert(copilotHandle, repoPath, provider) { const copilotId = await topcoderApiHelper.getTopcoderMemberId(copilotHandle); const notificationConfigs = config.MAIL_NOTICIATION; + const content = getContent(repoPath); logger.debug(`Sending mail notification to copilot ${copilotHandle} Repo: ${repoPath} Provider: ${provider}`); await kafkaSender.sendNotification({ serviceId: 'email',