@@ -1573,35 +1573,60 @@ async function DeleteChallenge(data, adminUsername) {
15731573 "password": STRING,
15741574 }
15751575 */
1576+ try {
1577+ // check if password matches admin username
1578+ const adminProfile = await AdminCollection . findOne ( { username : adminUsername } ) ;
1579+ if ( adminProfile ) {
1580+ // check calculated password hash
1581+ const SALT = process . env . SALT ;
1582+ const salted_password = SALT + data . password ;
1583+ const hashed_passwd = Hash_SHA256 ( salted_password ) ;
1584+
1585+ if ( adminProfile . password === hashed_passwd ) {
1586+ console . log ( "Attempting to Delete Challenge Reference from Sections of DB. . ." )
1587+
1588+ // we need to remove all references to this challenge from various
1589+ // entries: user.completions | team.completions
1590+ await UserCollection . updateMany (
1591+ { } ,
1592+ {
1593+ $pull : {
1594+ completions : { id : data . challenge_id }
1595+ }
1596+ }
1597+ ) ;
15761598
1577- // check if password matches admin username
1578- const adminProfile = await UserCollection . findOne ( { username : adminUsername } ) ;
1579- if ( adminProfile ) {
1580- // check calculated password hash
1581- const SALT = process . env . SALT ;
1582- const salted_password = SALT + data . password ;
1583- const hashed_passwd = Hash_SHA256 ( salted_password ) ;
1584-
1585- if ( adminProfile . password === hashed_passwd ) {
1586- // we need to remove all references to this challenge from various
1587- // entries: user.completions | team.completions
1588- return { acknowledge : false , "message" : "In Development!" }
1599+ await TeamCollection . updateMany (
1600+ { } ,
1601+ {
1602+ $pull : {
1603+ completions : { id : data . challenge_id }
1604+ }
1605+ }
1606+ ) ;
1607+ } else {
1608+ console . log ( "Bad Admin Auth" )
1609+ console . log ( `${ adminUsername } :${ data . password } | ${ hashed_passwd } --> ${ adminProfile . password } ` )
1610+ return { acknowledge : false , "message" : "Error Deleting Challenge!" }
1611+ }
15891612 } else {
15901613 console . log ( "Bad Admin Auth" )
1614+ console . log ( `Username: ${ adminUsername } ` )
15911615 return { acknowledge : false , "message" : "Error Deleting Challenge!" }
15921616 }
1593- } else {
1594- console . log ( "Bad Admin Auth" )
1595- return { acknowledge : false , "message" : "Error Deleting Challenge!" }
1596- }
15971617
1598- const action = await ChallengeCollection . deleteOne ( { _id : data . challenge_id } )
1599-
1600- if ( action . acknowledged && action . deletedCount !== 0 ) {
1601- console . log ( "Deleted Challenge" )
1602- return { acknowledge : true , "message" : "Challenge Deleted!" }
1603- } else {
1604- console . log ( "Error Deleting Challenge" )
1618+ console . log ( "Attempting to Delete Challenge Entry from DB. . ." )
1619+ const action = await ChallengeCollection . deleteOne ( { _id : data . challenge_id } )
1620+
1621+ if ( action . acknowledged && action . deletedCount !== 0 ) {
1622+ console . log ( "Deleted Challenge" )
1623+ return { acknowledge : true , "message" : "Challenge Deleted Successfully!" }
1624+ } else {
1625+ console . log ( "Error Deleting Challenge" )
1626+ return { acknowledge : false , "message" : "Error Deleting Challenge!" }
1627+ }
1628+ } catch ( error ) {
1629+ console . log ( error ) ;
16051630 return { acknowledge : false , "message" : "Error Deleting Challenge!" }
16061631 }
16071632}
0 commit comments