@@ -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