Skip to content

Commit cdd1297

Browse files
author
James Cori
committed
Merge branch 'develop'
2 parents d1d43ac + 2e5133e commit cdd1297

File tree

3 files changed

+43
-61
lines changed

3 files changed

+43
-61
lines changed

config/default.js

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -22,16 +22,20 @@ module.exports = {
2222
KAFKA_CLIENT_CERT_KEY: process.env.KAFKA_CLIENT_CERT_KEY,
2323

2424
SUBMITTER_ROLE_ID: process.env.SUBMITTER_ROLE_ID || '732339e7-8e30-49d7-9198-cccf9451e221',
25+
MANAGER_RESOURCE_ROLE_ID: process.env.MANAGER_RESOURCE_ROLE_ID || '0e9c6879-39e4-4eb6-b8df-92407890faf1',
2526

2627
RESOURCE_ROLES_WITHOUT_TIMELINE_NOTIFICATIONS: process.env.RESOURCE_ROLES_WITHOUT_TIMELINE_NOTIFICATIONS ? process.env.RESOURCE_ROLES_WITHOUT_TIMELINE_NOTIFICATIONS.split(',') : ['2a4dc376-a31c-4d00-b173-13934d89e286'],
2728

29+
PROJECT_ROLES_WITHOUT_TIMELINE_NOTIFICATIONS: process.env.PROJECT_ROLES_WITHOUT_TIMELINE_NOTIFICATIONS ? process.env.PROJECT_ROLES_WITHOUT_TIMELINE_NOTIFICATIONS.split(',') : ['manager', 'customer'],
30+
2831
IS_CREATE_FORUM: process.env.IS_CREATE_FORUM || true,
2932

3033
CREATE_CHALLENGE_RESOURCE_TOPIC: process.env.CREATE_CHALLENGE_RESOURCE_TOPIC || 'challenge.action.resource.create',
3134
DELETE_CHALLENGE_RESOURCE_TOPIC: process.env.DELETE_CHALLENGE_RESOURCE_TOPIC || 'challenge.action.resource.delete',
3235

3336
CHALLENGE_ORIGINATOR: process.env.CHALLENGE_ORIGINATOR || 'app.challenge.service',
3437

38+
PROJECTS_V5_API_URL: process.env.PROJECTS_V5_API_URL || 'https://api.topcoder-dev.com/v5/projects',
3539
CHALLENGE_API_V4_URL: process.env.CHALLENGE_API_V4_URL || 'https://api.topcoder-dev.com/v4/challenges',
3640
CHALLENGE_API_V5_URL: process.env.CHALLENGE_API_V5_URL || 'http://localhost:3001/v5/challenges',
3741
RESOURCE_ROLE_API_URL: process.env.RESOURCE_ROLE_API_URL || 'http://localhost:3001/v5/resource-roles',

src/app.js

Lines changed: 9 additions & 36 deletions
Original file line numberDiff line numberDiff line change
@@ -4,11 +4,9 @@
44

55
global.Promise = require('bluebird')
66
const config = require('config')
7-
const { isNil, get } = require('lodash')
87
const Kafka = require('no-kafka')
98
const healthcheck = require('topcoder-healthcheck-dropin')
109
const logger = require('./common/logger')
11-
const helper = require('./common/helper')
1210
const { getKafkaOptions } = require('./common/utils')
1311
const ProcessorService = require('./services/ProcessorService')
1412

@@ -40,40 +38,15 @@ const dataHandler = async (messageSet, topic, partition) => Promise.each(message
4038
return
4139
}
4240
try {
43-
const challengeExistsOnLegacy = await ProcessorService.legacyChallengeExist(messageJSON)
44-
logger.debug(`Message ${JSON.stringify(messageJSON)}`)
45-
if (challengeExistsOnLegacy) {
46-
switch (topic) {
47-
case config.CREATE_CHALLENGE_RESOURCE_TOPIC :
48-
await ProcessorService.createChallengeResource(messageJSON)
49-
break
50-
case config.DELETE_CHALLENGE_RESOURCE_TOPIC:
51-
await ProcessorService.deleteChallengeResource(messageJSON)
52-
break
53-
default:
54-
throw new Error(`Invalid topic: ${topic}`)
55-
}
56-
} else {
57-
const challengeId = get(messageJSON, 'payload.challengeId')
58-
if (isNil(challengeId)) {
59-
throw new Error(`Challenge ID ${challengeId} is null, will not queue to retry`)
60-
} else {
61-
logger.warn(`Creating resource failed, no legacy challenge found for ${challengeId} - Retry disabled`)
62-
// const retryCountIdentifier = `${config.KAFKA_GROUP_ID.split(' ').join('_')}_retry_count`
63-
// let currentRetryCount = parseInt(get(messageJSON.payload, retryCountIdentifier, 1), 10)
64-
// if (currentRetryCount <= config.MAX_RETRIES) {
65-
// logger.info(`Challenge does not exist yet. Will post the same message back to the bus API and retry in ${currentRetryCount * (config.RETRY_TIMEOUT / 1000)} seconds`)
66-
// await new Promise((resolve) => {
67-
// setTimeout(async () => {
68-
// currentRetryCount += 1
69-
// await helper.postBusEvent(topic, { ...messageJSON.payload, [retryCountIdentifier]: currentRetryCount })
70-
// resolve()
71-
// }, config.RETRY_TIMEOUT * currentRetryCount)
72-
// })
73-
// } else {
74-
// logger.error(`Failed to process message after ${config.MAX_RETRIES} retries. Aborting...`)
75-
// }
76-
}
41+
switch (topic) {
42+
case config.CREATE_CHALLENGE_RESOURCE_TOPIC :
43+
await ProcessorService.createChallengeResource(messageJSON)
44+
break
45+
case config.DELETE_CHALLENGE_RESOURCE_TOPIC:
46+
await ProcessorService.deleteChallengeResource(messageJSON)
47+
break
48+
default:
49+
throw new Error(`Invalid topic: ${topic}`)
7750
}
7851
// only commit if no errors
7952
await consumer.commitOffset({ topic, partition, offset: m.offset })

src/services/ProcessorService.js

Lines changed: 30 additions & 25 deletions
Original file line numberDiff line numberDiff line change
@@ -15,31 +15,15 @@ const {isStudio} = require('../common/utils')
1515
* Check if a challenge exists on legacy (v4)
1616
* @param {Object} message The message containing the challenge resource information
1717
*/
18-
async function legacyChallengeExist (message) {
19-
let exists = true
20-
const challengeId = _.get(message, 'payload.challengeId')
21-
if (_.isNil(challengeId)) {
22-
throw new Error(`Challenge ID ${challengeId} is null`)
23-
}
18+
async function legacyChallengeExistInV4 (legacyId) {
2419
try {
2520
const m2mToken = await helper.getM2Mtoken()
26-
const res = await helper.getRequest(`${config.CHALLENGE_API_V5_URL}/${challengeId}`, m2mToken)
27-
// logger.debug(`m2m Token: ${m2mToken}`)
28-
// logger.debug(`Getting Challenge from V5 ${config.CHALLENGE_API_V5_URL}/${challengeId}`)
29-
// logger.debug(`Response ${JSON.stringify(res.body)}`)
30-
const v5Challenge = res.body
31-
if (!v5Challenge.legacyId) {
32-
exists = false
33-
logger.warn(`Challenge ${challengeId} does not have a legacyId. Can't fetch details from V4`)
34-
} else {
35-
logger.debug(`Calling V4: ${config.CHALLENGE_API_V4_URL}/${v5Challenge.legacyId}`)
36-
await helper.getRequest(`${config.CHALLENGE_API_V4_URL}/${v5Challenge.legacyId}`, m2mToken)
37-
}
21+
logger.debug(`Calling V4: ${config.CHALLENGE_API_V4_URL}/${legacyId}`)
22+
await helper.getRequest(`${config.CHALLENGE_API_V4_URL}/${legacyId}`, m2mToken)
3823
} catch (e) {
39-
logger.warn(`error getting legacy challenge ${JSON.stringify(e)}`)
40-
exists = false
24+
logger.logFullError(e)
25+
throw new Error(`v4 Challenge not found for ${legacyId}`)
4126
}
42-
return exists
4327
}
4428

4529
/**
@@ -60,9 +44,19 @@ async function _updateChallengeResource (message, isDelete) {
6044
try {
6145
const res = await helper.getRequest(`${config.CHALLENGE_API_V5_URL}/${challengeId}`, m2mToken)
6246
v5Challenge = res.body
47+
if (_.get(v5Challenge, 'legacy.pureV5Task')) {
48+
logger.debug('Challenge is a pure v5 task. Will skip...')
49+
return
50+
}
6351
} catch (err) {
64-
throw new Error(`Challenge with uuid ${challengeId} does not exist.`)
52+
throw new Error(`Challenge with uuid ${challengeId} does not exist in v5 API [GET ${config.CHALLENGE_API_V5_URL}/${challengeId}] - Error: ${JSON.stringify(err)}.`)
6553
}
54+
55+
if (!v5Challenge.legacyId) {
56+
throw new Error(`Challenge ${challengeId} has no Legacy ID: ${JSON.stringify(v5Challenge)}`)
57+
}
58+
await legacyChallengeExistInV4(v5Challenge.legacyId)
59+
6660
let resourceRole = null
6761
let resourceRoleResponse = null
6862
try {
@@ -118,7 +112,19 @@ async function _updateChallengeResource (message, isDelete) {
118112
if (isDelete) {
119113
await notificationService.disableTimelineNotifications(_.get(v5Challenge, 'legacyId'), userId)
120114
} else {
121-
await notificationService.enableTimelineNotifications(_.get(v5Challenge, 'legacyId'), userId, _.get(v5Challenge, 'updatedBy') || _.get(v5Challenge, 'createdBy'))
115+
let shouldEnableNotifications = true
116+
if (resourceRole.id === config.MANAGER_RESOURCE_ROLE_ID) {
117+
// see if notifications should be enabled based on the user's role on the project
118+
const v5ProjectRes = await helper.getRequest(`${config.PROJECTS_V5_API_URL}/${v5Challenge.projectId}`, m2mToken)
119+
const memberRolesOnV5Project = _.map(_.filter(_.get(v5ProjectRes, 'body.members', []), m => _.toString(m.userId) === _.toString(userId)), r => r.role)
120+
if (memberRolesOnV5Project.length > 0 && _.intersection(config.PROJECT_ROLES_WITHOUT_TIMELINE_NOTIFICATIONS, memberRolesOnV5Project).length > 0) {
121+
// notifications should not be enabled
122+
shouldEnableNotifications = false
123+
}
124+
}
125+
if (shouldEnableNotifications) {
126+
await notificationService.enableTimelineNotifications(_.get(v5Challenge, 'legacyId'), userId, _.get(v5Challenge, 'updatedBy') || _.get(v5Challenge, 'createdBy'))
127+
}
122128
}
123129
}
124130
}
@@ -170,8 +176,7 @@ deleteChallengeResource.schema = createChallengeResource.schema
170176

171177
module.exports = {
172178
createChallengeResource,
173-
deleteChallengeResource,
174-
legacyChallengeExist
179+
deleteChallengeResource
175180
}
176181

177182
// logger.buildService(module.exports)

0 commit comments

Comments
 (0)