@@ -1859,7 +1859,16 @@ function validateTask(currentUser, challenge, data, challengeResources) {
18591859 * @param {Object } data the challenge data to be updated
18601860 * @returns {Object } the updated challenge
18611861 */
1862- async function updateChallenge ( currentUser , challengeId , data ) {
1862+ // Note: `options` may be a boolean for backward compatibility (emitEvent flag),
1863+ // or an object { emitEvent?: boolean }.
1864+ async function updateChallenge ( currentUser , challengeId , data , options = { } ) {
1865+ // Backward compatibility for callers passing a boolean as the 4th arg
1866+ let emitEvent = true ;
1867+ if ( typeof options === "boolean" ) {
1868+ emitEvent = options ;
1869+ } else if ( options && Object . prototype . hasOwnProperty . call ( options , "emitEvent" ) ) {
1870+ emitEvent = options . emitEvent !== false ;
1871+ }
18631872 const challenge = await prisma . challenge . findUnique ( {
18641873 where : { id : challengeId } ,
18651874 include : includeReturnFields ,
@@ -2437,7 +2446,15 @@ async function updateChallenge(currentUser, challengeId, data) {
24372446 include : includeReturnFields ,
24382447 } ) ;
24392448 } ) ;
2440- await indexChallengeAndPostToKafka ( updatedChallenge , track , type ) ;
2449+ // Re-fetch the challenge outside the transaction to ensure we publish
2450+ // only after the commit succeeds and using the committed snapshot.
2451+ if ( emitEvent ) {
2452+ const committed = await prisma . challenge . findUnique ( {
2453+ where : { id : challengeId } ,
2454+ include : includeReturnFields ,
2455+ } ) ;
2456+ await indexChallengeAndPostToKafka ( committed , track , type ) ;
2457+ }
24412458
24422459 if ( updatedChallenge . legacy . selfService ) {
24432460 const creator = await helper . getMemberByHandle ( updatedChallenge . createdBy ) ;
0 commit comments