Skip to content
This repository was archived by the owner on Mar 13, 2025. It is now read-only.

topcoder-x-ui/issues/459 #97

Merged
merged 1 commit into from
Jun 14, 2022
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
56 changes: 56 additions & 0 deletions services/NotificationService.js
Original file line number Diff line number Diff line change
@@ -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);
7 changes: 7 additions & 0 deletions utils/kafka-consumer.js
Original file line number Diff line number Diff line change
Expand Up @@ -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');

Expand Down Expand Up @@ -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);
}
});
}

Expand Down
17 changes: 15 additions & 2 deletions utils/notification.js
Original file line number Diff line number Diff line change
Expand Up @@ -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',
Expand Down