@@ -306,21 +306,20 @@ async function GetChallenges() {
306306}
307307export default GetChallenges ;
308308
309- // data = { challenge_name }
309+ // data = { challenge_id }
310310async function GetChallengeInfo ( data ) {
311- const challengeName = SanitizeString ( data . challenge_name )
312- if ( challengeName === null ) {
313- return null
314- } else {
315- const challengeProfile = await ChallengeCollection . findOne ( { name : challengeName . replaceAll ( '_' , ' ' ) } )
316-
311+ const challengeID = SanitizeAlphaNumeric ( data . challenge_id )
312+ const challengeProfile = await ChallengeCollection . findOne ( { _id : challengeID } )
313+ if ( challengeProfile ) {
317314 return {
318315 "name" : challengeProfile . name ,
319316 "description" : challengeProfile . description ,
320317 "category" : challengeProfile . category ,
321318 "difficulty" : challengeProfile . difficulty ,
322319 "rating" : challengeProfile . rating ,
323320 }
321+ } else {
322+ return null ;
324323 }
325324}
326325
@@ -1364,20 +1363,25 @@ async function ConvertCompletions(userCompletions, teamCompletions) {
13641363}
13651364
13661365async function UserRatingChallenge ( ratingData , jwt ) {
1367- const userProfile = await UserCollection . findOne ( { username : jwt . username , email : jwt . email } )
1368- if ( ! userProfile || ! ratingData ) {
1366+ const username = SanitizeString ( jwt . username ) ;
1367+ const email = SanitizeString ( jwt . email ) ;
1368+
1369+ if ( ! ratingData || username === null || email === null ) {
1370+ console . log ( "[-] Error rating username or email in JWT malformed!" ) ;
1371+ return null ;
1372+ }
1373+
1374+ const userProfile = await UserCollection . findOne ( { username : username , email : email } )
1375+ if ( ! userProfile ) {
1376+ console . log ( "[-] Profile could not be Found!" ) ;
13691377 return null ;
13701378 }
13711379
13721380 // check if the userProfile completed the challenge theyre rating
13731381 let completedChallenge = false ;
13741382
13751383 // check that numberRating is a valid number
1376- ratingData . challenge_name = SanitizeString ( ratingData . challenge_name ) ;
1377- if ( ratingData . challenge_name === null ) {
1378- console . log ( "[-] Error rating challenge_name attribute malformed!" ) ;
1379- return null ;
1380- }
1384+ const challengeID = SanitizeAlphaNumeric ( ratingData . challenge_id ) ;
13811385
13821386 // Check if numberRating is a valid number
13831387 if ( ! ValidRatingNumber ( ratingData . rating ) ) {
@@ -1387,27 +1391,26 @@ async function UserRatingChallenge(ratingData, jwt) {
13871391 }
13881392
13891393 const numberRating = ratingData . rating ;
1390- const challengeName = ratingData . challenge_name . replaceAll ( '_' , ' ' ) ;
13911394
1392- console . log ( "Rating Challenge: " + challengeName ) ;
1395+ console . log ( "Rating Challenge ID : " + challengeID ) ;
13931396 console . log ( "|______" + numberRating ) ;
13941397
13951398 // check if this user has already rated the challenge
13961399 // in ratingData
1397- if ( userProfile . ratings . includes ( challengeName ) ) {
1398- console . log ( "[*] " + userProfile . username + " has already submitted a rating for: " + challengeName ) ;
1400+ if ( userProfile . ratings . includes ( challengeID ) ) {
1401+ console . log ( "[*] " + userProfile . username + " has already submitted a rating for challenge_id : " + challengeID ) ;
13991402 return null ;
14001403 }
14011404
14021405 // iterate the users completions
14031406 for ( const data of Object . entries ( userProfile . completions ) ) {
1404- const [ index , { name , time } ] = data ; // break down the entry
1405- const challengeProfile = await ChallengeCollection . findOne ( { name : name . replaceAll ( '_' , ' ' ) } )
1407+ const [ index , { id , time } ] = data ; // break down the entry
1408+ const challengeProfile = await ChallengeCollection . findOne ( { _id : SanitizeAlphaNumeric ( id ) } )
14061409
14071410 if ( challengeProfile ) {
1408- // users completions have the challenge name
1409- // listed as completed
1410- if ( challengeProfile . name === challengeName ) {
1411+ // users completions have the challenge id listed as completed
1412+ if ( challengeProfile . _id . toString ( ) === SanitizeAlphaNumeric ( id ) ) {
1413+ console . log ( "[*] User has completed this challenge!" )
14111414 completedChallenge = true ;
14121415 break ;
14131416 }
@@ -1416,23 +1419,24 @@ async function UserRatingChallenge(ratingData, jwt) {
14161419
14171420 // they didnt complete the challenge
14181421 if ( ! completedChallenge ) {
1422+ console . log ( "[-] User tried rating a challenge they have not completed!" )
14191423 return null ;
14201424 } else {
14211425 // they completed the challenge we can take their number rating
14221426 // and apply it to the challenge entry in the db
14231427 await ChallengeCollection . updateOne (
1424- { name : challengeName } ,
1428+ { _id : challengeID } ,
14251429 { $push : { user_rates : Number ( numberRating ) } } // user_rates are used to calulate rating attribute
1426- )
1430+ ) ;
14271431
14281432 // update the challenges rating attribute based on its user_rates
1429- const updatedChallenge = await ChallengeCollection . findOne ( { name : challengeName } ) ;
1433+ const updatedChallenge = await ChallengeCollection . findOne ( { _id : challengeID } ) ;
14301434 if ( updatedChallenge && updatedChallenge . user_rates . length > 0 ) {
14311435 const total = updatedChallenge . user_rates . reduce ( ( sum , r ) => sum + r , 0 ) ;
14321436 const avg = total / updatedChallenge . user_rates . length ;
14331437
14341438 await ChallengeCollection . updateOne (
1435- { name : challengeName } ,
1439+ { _id : challengeID } ,
14361440 { $set : { rating : avg } }
14371441 ) ;
14381442 }
@@ -1441,8 +1445,10 @@ async function UserRatingChallenge(ratingData, jwt) {
14411445 // they cannot spam ratings for a challenge
14421446 await UserCollection . updateOne (
14431447 { _id : userProfile . _id } ,
1444- { $addToSet : { ratings : challengeName } }
1445- )
1448+ { $addToSet : { ratings : challengeID } }
1449+ ) ;
1450+
1451+ console . log ( "[+] Rate Uploaded Successfully!" )
14461452
14471453 return {
14481454 "message" : "Rate Uploaded Successfully!"
0 commit comments