Skip to content

Commit b8cde91

Browse files
committed
delete + create new transactions
1 parent ef629cf commit b8cde91

File tree

5 files changed

+446
-165
lines changed

5 files changed

+446
-165
lines changed

api/api.js

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -123,7 +123,7 @@ const main = async () => {
123123
app.post('/v1/adminSettings/', misc.adminSettings)
124124
app.get('/v1/submissions', submissions.submissions);
125125
app.post('/v1/submissions/new', submissions.newSubmission);
126-
app.get('/v1/submissions/delete', submissions.deleteSubmission);
126+
app.post('/v1/submissions/delete', submissions.deleteSubmission);
127127
app.get('/v1/about', misc.about);
128128
app.post('/v1/profile/upload', misc.profileUpload)
129129

api/controllers/Submissions.js

Lines changed: 88 additions & 69 deletions
Original file line numberDiff line numberDiff line change
@@ -1,12 +1,13 @@
11
const Connection = require('./../utils/mongoDB.js')
2+
const { broadCastNewSolve } = require('./../controllers/Sockets.js')
3+
const MongoDB = require('mongodb')
24

35
const 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 }

client/src/AdminPanel/adminManageAnnouncements.js

Lines changed: 1 addition & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -231,7 +231,7 @@ class AdminManageAnnouncements extends React.Component {
231231
if (data.success === true) {
232232
message.success({ content: "Deleted announcements [" + ids.join(', ') + "] successfully!" })
233233
this.fillTableData()
234-
234+
this.setState({ selectedTableKeys: [] })
235235
}
236236
else {
237237
message.error({ content: "Oops. Unknown error" })
@@ -243,8 +243,6 @@ class AdminManageAnnouncements extends React.Component {
243243
message.error({ content: "Oops. There was an issue connecting with the server" });
244244
})
245245
close()
246-
this.setState({ selectedTableKeys: [] })
247-
248246
}
249247

250248
editAnnouncementOpen = (id) => {

0 commit comments

Comments
 (0)