@@ -2616,20 +2616,25 @@ async function removeAChallenge(challenge: { [x: string]: any; challenger?: any;
26162616
26172617 if ( ! standing ) {
26182618 // Remove from challenger
2619- list . push ( deleteChallengeFromBothLocations (
2620- challenge . challenger . id ,
2621- new Set ( [ challenge . id ] ) ,
2622- "issued" ,
2623- "challenges_issued"
2624- ) ) ;
2619+ const updateChallenger = ddbDocClient . send ( new UpdateCommand ( {
2620+ TableName : process . env . ABSTRACT_PLAY_TABLE ,
2621+ Key : { "pk" : "USER" , "sk" : challenge . challenger . id } ,
2622+ ExpressionAttributeValues : { ":c" : new Set ( [ challenge . id ] ) } ,
2623+ ExpressionAttributeNames : { "#c" : "challenges" , "#ci" : "challenges_issued" } ,
2624+ UpdateExpression : "delete #c.issued :c, #ci :c" ,
2625+ } ) ) ;
2626+ list . push ( updateChallenger ) ;
26252627 // Remove from challenged
26262628 challenge . challengees . forEach ( ( challengee : { id : string ; } ) => {
2627- list . push ( deleteChallengeFromBothLocations (
2628- challengee . id ,
2629- new Set ( [ challenge . id ] ) ,
2630- "received" ,
2631- "challenges_received"
2632- ) ) ;
2629+ list . push (
2630+ ddbDocClient . send ( new UpdateCommand ( {
2631+ TableName : process . env . ABSTRACT_PLAY_TABLE ,
2632+ Key : { "pk" : "USER" , "sk" : challengee . id } ,
2633+ ExpressionAttributeValues : { ":c" : new Set ( [ challenge . id ] ) } ,
2634+ ExpressionAttributeNames : { "#c" : "challenges" , "#cr" : "challenges_received" } ,
2635+ UpdateExpression : "delete #c.received :c, #cr :c" ,
2636+ } ) )
2637+ ) ;
26332638 } )
26342639 } else if (
26352640 revoked
@@ -2638,12 +2643,14 @@ async function removeAChallenge(challenge: { [x: string]: any; challenger?: any;
26382643 ) {
26392644 // Remove from challenger
26402645 console . log ( `removing duplicated challenge ${ standing ? challenge . metaGame + '#' + challenge . id : challenge . id } from challenger ${ challenge . challenger . id } ` ) ;
2641- list . push ( deleteChallengeFromBothLocations (
2642- challenge . challenger . id ,
2643- new Set ( [ challenge . metaGame + '#' + challenge . id ] ) ,
2644- "standing" ,
2645- "challenges_standing"
2646- ) ) ;
2646+ const updateChallenger = ddbDocClient . send ( new UpdateCommand ( {
2647+ TableName : process . env . ABSTRACT_PLAY_TABLE ,
2648+ Key : { "pk" : "USER" , "sk" : challenge . challenger . id } ,
2649+ ExpressionAttributeValues : { ":c" : new Set ( [ challenge . metaGame + '#' + challenge . id ] ) } ,
2650+ ExpressionAttributeNames : { "#c" : "challenges" , "#cs" : "challenges_standing" } ,
2651+ UpdateExpression : "delete #c.standing :c, #cs :c" ,
2652+ } ) ) ;
2653+ list . push ( updateChallenger ) ;
26472654 }
26482655
26492656 // Remove from players that have already accepted
@@ -2655,12 +2662,15 @@ async function removeAChallenge(challenge: { [x: string]: any; challenger?: any;
26552662 }
26562663 playersToUpdate . forEach ( ( player : { id : string ; } ) => {
26572664 console . log ( `removing challenge ${ standing ? challenge . metaGame + '#' + challenge . id : challenge . id } from ${ player . id } ` ) ;
2658- list . push ( deleteChallengeFromBothLocations (
2659- player . id ,
2660- new Set ( [ standing ? challenge . metaGame + '#' + challenge . id : challenge . id ] ) ,
2661- "accepted" ,
2662- "challenges_accepted"
2663- ) ) ;
2665+ list . push (
2666+ ddbDocClient . send ( new UpdateCommand ( {
2667+ TableName : process . env . ABSTRACT_PLAY_TABLE ,
2668+ Key : { "pk" : "USER" , "sk" : player . id } ,
2669+ ExpressionAttributeValues : { ":c" : new Set ( [ standing ? challenge . metaGame + '#' + challenge . id : challenge . id ] ) } ,
2670+ ExpressionAttributeNames : { "#c" : "challenges" , "#ca" : "challenges_accepted" } ,
2671+ UpdateExpression : "delete #c.accepted :c, #ca :c" ,
2672+ } ) )
2673+ ) ;
26642674 } ) ;
26652675
26662676 // Remove challenge
@@ -2852,25 +2862,15 @@ async function acceptChallenge(userid: string, metaGame: string, challengeId: st
28522862 ( { challengeId, work : updateChallenge } = await duplicateStandingChallenge ( challenge , newplayer ) ) ;
28532863 }
28542864 // Update accepter
2855- const challengeValue = new Set ( [ standing ? challenge . metaGame + '#' + challengeId : challengeId ] ) ;
2856-
2857- // Delete from received challenges (both locations)
2858- const deleteFromReceived = deleteChallengeFromBothLocations (
2859- userid ,
2860- challengeValue ,
2861- "received" ,
2862- "challenges_received"
2863- ) ;
2864-
2865- // Add to accepted challenges (top-level only)
2866- const addToAccepted = sendCommandWithRetry ( new UpdateCommand ( {
2865+ const updateAccepter = ddbDocClient . send ( new UpdateCommand ( {
28672866 TableName : process . env . ABSTRACT_PLAY_TABLE ,
28682867 Key : { "pk" : "USER" , "sk" : userid } ,
2869- UpdateExpression : "ADD challenges_accepted :c" ,
2870- ExpressionAttributeValues : { ":c" : challengeValue }
2868+ ExpressionAttributeValues : { ":c" : new Set ( [ standing ? challenge . metaGame + '#' + challengeId : challengeId ] ) } ,
2869+ ExpressionAttributeNames : { "#cr" : "challenges_received" , "#ca" : "challenges_accepted" , "#c" : "challenges" } ,
2870+ UpdateExpression : "delete #c.received :c, #cr :c add #ca :c" ,
28712871 } ) ) ;
28722872
2873- await Promise . all ( [ updateChallenge , deleteFromReceived , addToAccepted ] ) ;
2873+ await Promise . all ( [ updateChallenge , updateAccepter ] ) ;
28742874 return ;
28752875 }
28762876}
@@ -8297,43 +8297,3 @@ export const migrateMetagamesRatings = async (): Promise<any> => {
82978297 throw error ;
82988298 }
82998299} ;
8300-
8301- // Helper function to safely delete from both nested and top-level challenge attributes
8302- async function deleteChallengeFromBothLocations (
8303- userId : string ,
8304- challengeValue : Set < string > ,
8305- nestedAttr : string ,
8306- topLevelAttr : string
8307- ) : Promise < void > {
8308- const promises : Promise < any > [ ] = [ ] ;
8309-
8310- // Delete from nested attribute (this should always work)
8311- promises . push (
8312- sendCommandWithRetry ( new UpdateCommand ( {
8313- TableName : process . env . ABSTRACT_PLAY_TABLE ,
8314- Key : { "pk" : "USER" , "sk" : userId } ,
8315- UpdateExpression : `DELETE #c.${ nestedAttr } :c` ,
8316- ExpressionAttributeNames : { "#c" : "challenges" } ,
8317- ExpressionAttributeValues : { ":c" : challengeValue }
8318- } ) )
8319- ) ;
8320-
8321- // Delete from top-level attribute (might not exist, so catch errors)
8322- promises . push (
8323- sendCommandWithRetry ( new UpdateCommand ( {
8324- TableName : process . env . ABSTRACT_PLAY_TABLE ,
8325- Key : { "pk" : "USER" , "sk" : userId } ,
8326- UpdateExpression : `DELETE ${ topLevelAttr } :c` ,
8327- ExpressionAttributeValues : { ":c" : challengeValue }
8328- } ) ) . catch ( error => {
8329- // Ignore errors if the attribute doesn't exist
8330- if ( error . name === 'ValidationException' && error . message . includes ( 'does not exist' ) ) {
8331- console . log ( `Top-level attribute ${ topLevelAttr } doesn't exist for user ${ userId } , skipping` ) ;
8332- } else {
8333- throw error ;
8334- }
8335- } )
8336- ) ;
8337-
8338- await Promise . all ( promises ) ;
8339- }
0 commit comments