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

Commit 95dbb8e

Browse files
authored
Merge pull request #97 from 52cs/issue-459
topcoder-x-ui/issues/459
2 parents 4198263 + a902dd7 commit 95dbb8e

File tree

3 files changed

+78
-2
lines changed

3 files changed

+78
-2
lines changed

services/NotificationService.js

+56
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,56 @@
1+
/*
2+
* Copyright (c) 2022 TopCoder, Inc. All rights reserved.
3+
*/
4+
'use strict';
5+
6+
/**
7+
* This service processes incoming pure challenge events.
8+
*
9+
* @author TCSCODER
10+
* @version 1.0
11+
*/
12+
const Joi = require('joi');
13+
const logger = require('../utils/logger');
14+
const notification = require('../utils/notification');
15+
16+
/**
17+
* Send token expired notification
18+
* @param {Object} event the event
19+
*/
20+
async function handleTokenExpired(event) {
21+
try {
22+
const {copilotHandle, provider} = event.data;
23+
await notification.sendTokenExpiredAlert(copilotHandle, '', provider);
24+
logger.debug('Send token expired notification success');
25+
} catch (err) {
26+
logger.debug(`Send token expired notification failed. Internal Error: ${err}`);
27+
}
28+
}
29+
30+
/**
31+
* Process notification event.
32+
* @param {Object} event the event
33+
*/
34+
async function process(event) {
35+
Joi.attempt(event, process.schema);
36+
37+
if (event.event === 'notification.tokenExpired') {
38+
await handleTokenExpired(event);
39+
}
40+
}
41+
42+
process.schema = Joi.object().keys({
43+
event: Joi.string().valid('notification.tokenExpired').required(),
44+
data: Joi.object().keys({
45+
copilotHandle: Joi.string().required(),
46+
provider: Joi.string().required()
47+
}).required(),
48+
retryCount: Joi.number().integer().default(0).optional()
49+
});
50+
51+
52+
module.exports = {
53+
process
54+
};
55+
56+
logger.buildService(module.exports);

utils/kafka-consumer.js

+7
Original file line numberDiff line numberDiff line change
@@ -13,6 +13,7 @@ const healthcheck = require('topcoder-healthcheck-dropin');
1313
const IssueService = require('../services/IssueService');
1414
const CopilotPaymentService = require('../services/CopilotPaymentService');
1515
const ChallengeService = require('../services/ChallengeService');
16+
const NotificationService = require('../services/NotificationService');
1617
const logger = require('./logger');
1718
const kafka = require('./kafka');
1819

@@ -54,6 +55,12 @@ function messageHandler(messageSet) {
5455
.process(event)
5556
.catch(logger.error);
5657
}
58+
if (event && _.includes(['notification.tokenExpired']
59+
, event.event)) {
60+
NotificationService
61+
.process(event)
62+
.catch(logger.error);
63+
}
5764
});
5865
}
5966

utils/notification.js

+15-2
Original file line numberDiff line numberDiff line change
@@ -15,12 +15,25 @@ const logger = require('./logger');
1515

1616
const notification = {};
1717

18-
const content = `Hi {handle},
19-
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
18+
/**
19+
* get content template to send
20+
* @param {String} repoPath the repo path
21+
* @returns {String}
22+
*/
23+
function getContent(repoPath) {
24+
return repoPath
25+
?
26+
`Hi {handle},
27+
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
28+
:
29+
`Hi {handle},
30+
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
31+
}
2032

2133
notification.sendTokenExpiredAlert = async function sendTokenExpiredAlert(copilotHandle, repoPath, provider) {
2234
const copilotId = await topcoderApiHelper.getTopcoderMemberId(copilotHandle);
2335
const notificationConfigs = config.MAIL_NOTICIATION;
36+
const content = getContent(repoPath);
2437
logger.debug(`Sending mail notification to copilot ${copilotHandle} Repo: ${repoPath} Provider: ${provider}`);
2538
await kafkaSender.sendNotification({
2639
serviceId: 'email',

0 commit comments

Comments
 (0)