11const Connection = require ( './../utils/mongoDB.js' )
2+ const { broadCastNewSolve } = require ( './../controllers/Sockets.js' )
3+ const MongoDB = require ( 'mongodb' )
24
35const submissions = async ( req , res , next ) => {
4- const collections = Connection . collections
56 try {
67 if ( res . locals . perms < 2 ) throw new Error ( 'Permissions' ) ;
78 res . send ( {
89 success : true ,
9- submissions : await collections . transactions . find ( { '$or' : [ { type : 'submission' } , { type : 'blocked_submission' } ] } ) . toArray ( )
10+ submissions : req . app . get ( "transactionsCache" )
1011 } ) ;
1112 }
1213 catch ( err ) {
@@ -18,62 +19,42 @@ const newSubmission = async (req, res, next) => {
1819 const collections = Connection . collections
1920 try {
2021 if ( res . locals . perms < 2 ) throw new Error ( 'Permissions' ) ;
21- if ( await collections . users . countDocuments ( { username : req . body . username . toLowerCase ( ) } ) == 0 ) throw new Error ( 'UserNotFound' ) ;
22-
23- const chall = await collections . challs . findOneAndUpdate ( {
24- name : req . body . chall
25- } , {
26- $addToSet : {
27- solves : req . body . username . toLowerCase ( )
28- }
29- } , {
30- projection : {
31- points : 1 ,
32- _id : 0
33- }
34- } ) ;
35- if ( ! chall . ok ) throw new Error ( 'Unknown' ) ;
36- if ( chall . value === null ) throw new Error ( 'NotFound' ) ;
37- const lastScore = await collections . transactions . aggregate ( [ {
38- $match : {
39- author : req . body . username . toLowerCase ( ) ,
40- challenge : req . body . chall
41- }
42- } , {
43- $group : {
44- _id : '$challenge' ,
45- points : {
46- $sum : '$points'
47- }
48- }
49- } ] ) . toArray ( ) ;
50- if ( lastScore [ 0 ] . points > req . body . points && ! req . body . force ) {
51- res . send ( {
52- success : true ,
53- data : 'previous-higher'
54- } ) ;
55- return ;
22+ const GTime = new Date ( )
23+ let latestSolveSubmissionID = req . app . get ( "latestSolveSubmissionID" )
24+ latestSolveSubmissionID += 1
25+ req . app . set ( "latestSolveSubmissionID" , latestSolveSubmissionID )
26+ let insertDoc = {
27+ author : req . body . author ,
28+ challenge : req . body . challenge ,
29+ challengeID : MongoDB . ObjectID ( req . body . challengeID ) ,
30+ type : req . body . type ,
31+ timestamp : GTime ,
32+ lastChallengeID : latestSolveSubmissionID ,
33+ points : req . body . points
5634 }
57- await collections . transactions . insertOne ( {
58- author : req . body . username . toLowerCase ( ) ,
59- challenge : req . body . chall ,
60- timestamp : new Date ( ) ,
61- type : 'submission' ,
62- points : req . body . points - lastScore [ 0 ] . points ,
63- submission : req . body . flag != null ? req . body . flag : 'No flag provided' ,
64- manual : res . locals . username
65- } ) ;
66- await collections . users . updateOne ( {
67- username : req . body . username . toLowerCase ( )
68- } , {
69- $inc : { score : req . body . points - lastScore [ 0 ] . points }
70- } ) ;
71- res . send ( {
72- success : true ,
73- data : 'recorded'
74- } ) ;
35+ if ( req . body . type === "hint" ) {
36+ insertDoc . hint_id = req . body . hint_id - 1
37+ }
38+ else {
39+ insertDoc . correct = req . body . correct
40+ insertDoc . submission = req . body . submission
41+ }
42+ let transactionsCache = req . app . get ( "transactionsCache" )
43+ transactionsCache . push ( insertDoc )
44+ req . app . set ( "transactionsCache" , transactionsCache )
45+ await collections . transactions . insertOne ( insertDoc )
46+
47+ broadCastNewSolve ( [ {
48+ _id : insertDoc . _id ,
49+ username : req . body . author ,
50+ timestamp : GTime ,
51+ points : req . body . points ,
52+ lastChallengeID : latestSolveSubmissionID
53+ } ] )
54+ res . send ( { success : true } )
7555 }
7656 catch ( err ) {
57+ console . error ( err )
7758 next ( err ) ;
7859 }
7960}
@@ -82,23 +63,61 @@ const deleteSubmission = async (req, res, next) => {
8263 const collections = Connection . collections
8364 try {
8465 if ( res . locals . perms < 2 ) throw new Error ( 'Permissions' ) ;
85- const delReq = await collections . transactions . findOneAndDelete ( {
86- _id : MongoDB . ObjectID ( req . body . submissionID )
87- } , {
88- solves : 1 ,
89- points : 1 ,
90- hints : 1 ,
91- _id : 0
92- } ) ;
93- if ( ! delReq . ok ) throw new Error ( 'Unknown' ) ;
94- if ( delReq . value === null ) throw new Error ( 'NotFound' ) ;
95- res . send ( {
96- success : true ,
97- } ) ;
66+ let notFoundList = [ ]
67+ for ( let i = 0 ; i < req . body . ids . length ; i ++ ) {
68+ const current = req . body . ids [ i ]
69+ let delReq = await collections . transactions . findOneAndDelete ( { _id : MongoDB . ObjectID ( current ) } )
70+ if ( delReq . value === null ) notFoundList . push ( current )
71+ else {
72+ delReq = delReq . value
73+ //const challengeID = MongoDB.ObjectID(delReq.challengeID.toString())
74+ const challDoc = await collections . challs . findOne ( { _id : delReq . challengeID } )
75+ if ( delReq . type === "hint" ) {
76+ const hints = challDoc . hints
77+ const hintsArray = hints [ delReq . hint_id ] . purchased
78+ const index = hintsArray . indexOf ( delReq . author )
79+ if ( index !== - 1 ) { // in case the hint purchase record is not found for any reason
80+ hintsArray . splice ( index , 1 )
81+ await collections . challs . updateOne ( { _id : delReq . challengeID } , { $set : { hints : hints } } )
82+ }
83+ }
84+ else if ( delReq . type === "submission" ) {
85+ const solves = challDoc . solves
86+ const index = solves . indexOf ( delReq . author )
87+ if ( index !== - 1 ) { // in case the challenge purchase record is not found for any reason
88+ solves . splice ( index , 1 )
89+ await collections . challs . updateOne ( { _id : delReq . challengeID } , { $set : { solves : solves } } )
90+ }
91+
92+ }
93+ }
94+
95+ }
96+ let transactionsCache = req . app . get ( "transactionsCache" )
97+ for ( let i = 0 ; i < transactionsCache . length ; i ++ ) {
98+ if ( req . body . ids . includes ( transactionsCache [ i ] . _id . toString ( ) ) ) {
99+ transactionsCache . splice ( i , 1 )
100+ }
101+ }
102+ req . app . set ( "transactionsCache" , transactionsCache )
103+
104+ if ( notFoundList . length === 0 ) {
105+ res . send ( {
106+ success : true ,
107+ } ) ;
108+ }
109+ else {
110+ res . send ( {
111+ success : false ,
112+ error : "not-found" ,
113+ ids : notFoundList
114+ } )
115+ }
116+
98117 }
99118 catch ( err ) {
100119 next ( err ) ;
101120 }
102121}
103122
104- module . exports = { submissions, newSubmission, deleteSubmission}
123+ module . exports = { submissions, newSubmission, deleteSubmission }
0 commit comments