Skip to content

fix: task.memberId getting reset #522

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 3 commits into from
Sep 12, 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
4 changes: 1 addition & 3 deletions .circleci/config.yml
Original file line number Diff line number Diff line change
Expand Up @@ -70,9 +70,7 @@ workflows:
branches:
only:
- develop
- fix/challenge-timelines-edit-routes
- test/performance-profile
- July2022Updates
- fix/task-memberId-reset

# Production builds are exectuted only on tagged commits to the
# master branch.
Expand Down
2 changes: 1 addition & 1 deletion src/common/helper.js
Original file line number Diff line number Diff line change
Expand Up @@ -889,7 +889,7 @@ async function listChallengesByMember (memberId) {
// get search is paginated, we need to get all pages' data
let page = 1
while (true) {
let result = {};
let result = {}
try {
result = await axios.get(`${config.RESOURCES_API_URL}/${memberId}/challenges`, {
headers: { Authorization: `Bearer ${token}` },
Expand Down
2 changes: 1 addition & 1 deletion src/routes.js
Original file line number Diff line number Diff line change
Expand Up @@ -70,7 +70,7 @@ module.exports = {
'/challenges/:challengeId/statistics': {
get: {
controller: 'ChallengeController',
method: 'getChallengeStatistics',
method: 'getChallengeStatistics'
}
},
'/challenges/:challengeId/notifications': {
Expand Down
47 changes: 37 additions & 10 deletions src/services/ChallengeService.js
Original file line number Diff line number Diff line change
Expand Up @@ -274,7 +274,7 @@ async function searchChallenges (currentUser, criteria) {
{ wildcard: { name: `*${criteria.search}*` } },
{ wildcard: { name: `${criteria.search}*` } },
{ wildcard: { name: `*${criteria.search}` } },
{ match_phrase: { tags: criteria.search } },
{ match_phrase: { tags: criteria.search } }
]
} })
} else {
Expand Down Expand Up @@ -1649,6 +1649,39 @@ async function update (currentUser, challengeId, data, isFull) {
await validateWinners(data.winners, challengeId)
}

// Only m2m tokens are allowed to modify the `task.*` information on a challenge
if (!_.isUndefined(_.get(data, 'task')) && !currentUser.isMachine) {
if (!_.isUndefined(_.get(challenge, 'task'))) {
logger.info(`User ${currentUser.handle || currentUser.sub} is not allowed to modify the task information on challenge ${challengeId}`)
data.task = challenge.task
logger.info(`Task information on challenge ${challengeId} is reset to ${JSON.stringify(challenge.task)}. Original data: ${JSON.stringify(data.task)}`)
} else {
delete data.task
}
}

// task.memberId goes out of sync due to another processor setting "task.memberId" but subsequent immediate update to the task
// 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
// the sync issue in the processor. However this is quick fix that works since winner.userId is task.memberId.
if (_.get(challenge, 'legacy.pureV5Task') && !_.isUndefined(data.winners)) {
const winnerMemberId = _.get(data.winners, '[0].userId')
logger.info(`Setting task.memberId to ${winnerMemberId} for challenge ${challengeId}. Task ${_.get(data, 'task')} - ${_.get(challenge, 'task')}`)

if (winnerMemberId != null && _.get(data, 'task.memberId') !== winnerMemberId) {
logger.info(`Task ${challengeId} has a winner ${winnerMemberId}`)
data.task = {
isTask: true,
isAssigned: true,
memberId: winnerMemberId
}
logger.warn(`task.memberId mismatched with winner memberId. task.memberId is updated to ${winnerMemberId}`)
} else {
logger.info(`task ${challengeId} has no winner set yet.`)
}
} else {
logger.info(`${challengeId} is not a pureV5 challenge or has no winners set yet.`)
}

data.updated = moment().utc()
data.updatedBy = currentUser.handle || currentUser.sub
const updateDetails = {}
Expand Down Expand Up @@ -1709,6 +1742,9 @@ async function update (currentUser, challengeId, data, isFull) {
op = '$PUT'
} else if (_.isUndefined(challenge[key]) || challenge[key] !== value) {
op = '$PUT'
} else if (_.get(challenge, 'legacy.pureV5Task') && key === 'task') {
// always update task for pureV5 challenges
op = '$PUT'
}

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

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

// Only m2m tokens are allowed to modify the `task.*` information on a challenge
if (!_.isUndefined(_.get(data, 'task')) && !currentUser.isMachine) {
if (!_.isUndefined(_.get(challenge, 'task'))) {
data.task = challenge.task
} else {
delete data.task
}
}

if (_.get(type, 'isTask')) {
if (!_.isEmpty(_.get(data, 'task.memberId'))) {
const challengeResources = await helper.getChallengeResources(challengeId)
Expand Down