Skip to content

Commit e8a9d42

Browse files
authoredSep 12, 2022
Merge pull request #522 from topcoder-platform/fix/task-memberId-reset
fix: task.memberId getting reset
2 parents 03b64ee + e610d82 commit e8a9d42

File tree

4 files changed

+40
-15
lines changed

4 files changed

+40
-15
lines changed
 

‎.circleci/config.yml

Lines changed: 1 addition & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -70,9 +70,7 @@ workflows:
7070
branches:
7171
only:
7272
- develop
73-
- fix/challenge-timelines-edit-routes
74-
- test/performance-profile
75-
- July2022Updates
73+
- fix/task-memberId-reset
7674

7775
# Production builds are exectuted only on tagged commits to the
7876
# master branch.

‎src/common/helper.js

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -889,7 +889,7 @@ async function listChallengesByMember (memberId) {
889889
// get search is paginated, we need to get all pages' data
890890
let page = 1
891891
while (true) {
892-
let result = {};
892+
let result = {}
893893
try {
894894
result = await axios.get(`${config.RESOURCES_API_URL}/${memberId}/challenges`, {
895895
headers: { Authorization: `Bearer ${token}` },

‎src/routes.js

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -70,7 +70,7 @@ module.exports = {
7070
'/challenges/:challengeId/statistics': {
7171
get: {
7272
controller: 'ChallengeController',
73-
method: 'getChallengeStatistics',
73+
method: 'getChallengeStatistics'
7474
}
7575
},
7676
'/challenges/:challengeId/notifications': {

‎src/services/ChallengeService.js

Lines changed: 37 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -274,7 +274,7 @@ async function searchChallenges (currentUser, criteria) {
274274
{ wildcard: { name: `*${criteria.search}*` } },
275275
{ wildcard: { name: `${criteria.search}*` } },
276276
{ wildcard: { name: `*${criteria.search}` } },
277-
{ match_phrase: { tags: criteria.search } },
277+
{ match_phrase: { tags: criteria.search } }
278278
]
279279
} })
280280
} else {
@@ -1649,6 +1649,39 @@ async function update (currentUser, challengeId, data, isFull) {
16491649
await validateWinners(data.winners, challengeId)
16501650
}
16511651

1652+
// Only m2m tokens are allowed to modify the `task.*` information on a challenge
1653+
if (!_.isUndefined(_.get(data, 'task')) && !currentUser.isMachine) {
1654+
if (!_.isUndefined(_.get(challenge, 'task'))) {
1655+
logger.info(`User ${currentUser.handle || currentUser.sub} is not allowed to modify the task information on challenge ${challengeId}`)
1656+
data.task = challenge.task
1657+
logger.info(`Task information on challenge ${challengeId} is reset to ${JSON.stringify(challenge.task)}. Original data: ${JSON.stringify(data.task)}`)
1658+
} else {
1659+
delete data.task
1660+
}
1661+
}
1662+
1663+
// task.memberId goes out of sync due to another processor setting "task.memberId" but subsequent immediate update to the task
1664+
// will not have the memberId set. So we need to set it using winners to ensure it is always in sync. The proper fix is to correct
1665+
// the sync issue in the processor. However this is quick fix that works since winner.userId is task.memberId.
1666+
if (_.get(challenge, 'legacy.pureV5Task') && !_.isUndefined(data.winners)) {
1667+
const winnerMemberId = _.get(data.winners, '[0].userId')
1668+
logger.info(`Setting task.memberId to ${winnerMemberId} for challenge ${challengeId}. Task ${_.get(data, 'task')} - ${_.get(challenge, 'task')}`)
1669+
1670+
if (winnerMemberId != null && _.get(data, 'task.memberId') !== winnerMemberId) {
1671+
logger.info(`Task ${challengeId} has a winner ${winnerMemberId}`)
1672+
data.task = {
1673+
isTask: true,
1674+
isAssigned: true,
1675+
memberId: winnerMemberId
1676+
}
1677+
logger.warn(`task.memberId mismatched with winner memberId. task.memberId is updated to ${winnerMemberId}`)
1678+
} else {
1679+
logger.info(`task ${challengeId} has no winner set yet.`)
1680+
}
1681+
} else {
1682+
logger.info(`${challengeId} is not a pureV5 challenge or has no winners set yet.`)
1683+
}
1684+
16521685
data.updated = moment().utc()
16531686
data.updatedBy = currentUser.handle || currentUser.sub
16541687
const updateDetails = {}
@@ -1709,6 +1742,9 @@ async function update (currentUser, challengeId, data, isFull) {
17091742
op = '$PUT'
17101743
} else if (_.isUndefined(challenge[key]) || challenge[key] !== value) {
17111744
op = '$PUT'
1745+
} else if (_.get(challenge, 'legacy.pureV5Task') && key === 'task') {
1746+
// always update task for pureV5 challenges
1747+
op = '$PUT'
17121748
}
17131749

17141750
if (op) {
@@ -1848,15 +1884,6 @@ async function update (currentUser, challengeId, data, isFull) {
18481884

18491885
const { track, type } = await validateChallengeData(_.pick(challenge, ['trackId', 'typeId']))
18501886

1851-
// Only m2m tokens are allowed to modify the `task.*` information on a challenge
1852-
if (!_.isUndefined(_.get(data, 'task')) && !currentUser.isMachine) {
1853-
if (!_.isUndefined(_.get(challenge, 'task'))) {
1854-
data.task = challenge.task
1855-
} else {
1856-
delete data.task
1857-
}
1858-
}
1859-
18601887
if (_.get(type, 'isTask')) {
18611888
if (!_.isEmpty(_.get(data, 'task.memberId'))) {
18621889
const challengeResources = await helper.getChallengeResources(challengeId)

0 commit comments

Comments
 (0)
Please sign in to comment.