Skip to content

Commit

Permalink
delete + create new transactions
Browse files Browse the repository at this point in the history
  • Loading branch information
Tkaixiang committed Sep 20, 2021
1 parent ef629cf commit b8cde91
Show file tree
Hide file tree
Showing 5 changed files with 446 additions and 165 deletions.
2 changes: 1 addition & 1 deletion api/api.js
Original file line number Diff line number Diff line change
Expand Up @@ -123,7 +123,7 @@ const main = async () => {
app.post('/v1/adminSettings/', misc.adminSettings)
app.get('/v1/submissions', submissions.submissions);
app.post('/v1/submissions/new', submissions.newSubmission);
app.get('/v1/submissions/delete', submissions.deleteSubmission);
app.post('/v1/submissions/delete', submissions.deleteSubmission);
app.get('/v1/about', misc.about);
app.post('/v1/profile/upload', misc.profileUpload)

Expand Down
157 changes: 88 additions & 69 deletions api/controllers/Submissions.js
Original file line number Diff line number Diff line change
@@ -1,12 +1,13 @@
const Connection = require('./../utils/mongoDB.js')
const { broadCastNewSolve } = require('./../controllers/Sockets.js')
const MongoDB = require('mongodb')

const submissions = async (req, res, next) => {
const collections = Connection.collections
try {
if (res.locals.perms < 2) throw new Error('Permissions');
res.send({
success: true,
submissions: await collections.transactions.find({ '$or': [{ type: 'submission' }, { type: 'blocked_submission' }] }).toArray()
submissions: req.app.get("transactionsCache")
});
}
catch (err) {
Expand All @@ -18,62 +19,42 @@ const newSubmission = async (req, res, next) => {
const collections = Connection.collections
try {
if (res.locals.perms < 2) throw new Error('Permissions');
if (await collections.users.countDocuments({ username: req.body.username.toLowerCase() }) == 0) throw new Error('UserNotFound');

const chall = await collections.challs.findOneAndUpdate({
name: req.body.chall
}, {
$addToSet: {
solves: req.body.username.toLowerCase()
}
}, {
projection: {
points: 1,
_id: 0
}
});
if (!chall.ok) throw new Error('Unknown');
if (chall.value === null) throw new Error('NotFound');
const lastScore = await collections.transactions.aggregate([{
$match: {
author: req.body.username.toLowerCase(),
challenge: req.body.chall
}
}, {
$group: {
_id: '$challenge',
points: {
$sum: '$points'
}
}
}]).toArray();
if (lastScore[0].points > req.body.points && !req.body.force) {
res.send({
success: true,
data: 'previous-higher'
});
return;
const GTime = new Date()
let latestSolveSubmissionID = req.app.get("latestSolveSubmissionID")
latestSolveSubmissionID += 1
req.app.set("latestSolveSubmissionID", latestSolveSubmissionID)
let insertDoc = {
author: req.body.author,
challenge: req.body.challenge,
challengeID: MongoDB.ObjectID(req.body.challengeID),
type: req.body.type,
timestamp: GTime,
lastChallengeID: latestSolveSubmissionID,
points: req.body.points
}
await collections.transactions.insertOne({
author: req.body.username.toLowerCase(),
challenge: req.body.chall,
timestamp: new Date(),
type: 'submission',
points: req.body.points - lastScore[0].points,
submission: req.body.flag != null ? req.body.flag : 'No flag provided',
manual: res.locals.username
});
await collections.users.updateOne({
username: req.body.username.toLowerCase()
}, {
$inc: { score: req.body.points - lastScore[0].points }
});
res.send({
success: true,
data: 'recorded'
});
if (req.body.type === "hint") {
insertDoc.hint_id = req.body.hint_id-1
}
else {
insertDoc.correct = req.body.correct
insertDoc.submission = req.body.submission
}
let transactionsCache = req.app.get("transactionsCache")
transactionsCache.push(insertDoc)
req.app.set("transactionsCache", transactionsCache)
await collections.transactions.insertOne(insertDoc)

broadCastNewSolve([{
_id: insertDoc._id,
username: req.body.author,
timestamp: GTime,
points: req.body.points,
lastChallengeID: latestSolveSubmissionID
}])
res.send({ success: true })
}
catch (err) {
console.error(err)
next(err);
}
}
Expand All @@ -82,23 +63,61 @@ const deleteSubmission = async (req, res, next) => {
const collections = Connection.collections
try {
if (res.locals.perms < 2) throw new Error('Permissions');
const delReq = await collections.transactions.findOneAndDelete({
_id: MongoDB.ObjectID(req.body.submissionID)
}, {
solves: 1,
points: 1,
hints: 1,
_id: 0
});
if (!delReq.ok) throw new Error('Unknown');
if (delReq.value === null) throw new Error('NotFound');
res.send({
success: true,
});
let notFoundList = []
for (let i = 0; i < req.body.ids.length; i++) {
const current = req.body.ids[i]
let delReq = await collections.transactions.findOneAndDelete({ _id: MongoDB.ObjectID(current) })
if (delReq.value === null) notFoundList.push(current)
else {
delReq = delReq.value
//const challengeID = MongoDB.ObjectID(delReq.challengeID.toString())
const challDoc = await collections.challs.findOne({ _id: delReq.challengeID})
if (delReq.type === "hint") {
const hints = challDoc.hints
const hintsArray = hints[delReq.hint_id].purchased
const index = hintsArray.indexOf(delReq.author)
if (index !== -1) { // in case the hint purchase record is not found for any reason
hintsArray.splice(index, 1)
await collections.challs.updateOne({ _id: delReq.challengeID }, { $set: { hints: hints } })
}
}
else if (delReq.type === "submission") {
const solves = challDoc.solves
const index = solves.indexOf(delReq.author)
if (index !== -1) { // in case the challenge purchase record is not found for any reason
solves.splice(index, 1)
await collections.challs.updateOne({ _id: delReq.challengeID }, { $set: { solves: solves } })
}

}
}

}
let transactionsCache = req.app.get("transactionsCache")
for (let i = 0; i < transactionsCache.length; i++) {
if (req.body.ids.includes(transactionsCache[i]._id.toString())) {
transactionsCache.splice(i, 1)
}
}
req.app.set("transactionsCache", transactionsCache)

if (notFoundList.length === 0) {
res.send({
success: true,
});
}
else {
res.send({
success: false,
error: "not-found",
ids: notFoundList
})
}

}
catch (err) {
next(err);
}
}

module.exports = {submissions, newSubmission, deleteSubmission}
module.exports = { submissions, newSubmission, deleteSubmission }
4 changes: 1 addition & 3 deletions client/src/AdminPanel/adminManageAnnouncements.js
Original file line number Diff line number Diff line change
Expand Up @@ -231,7 +231,7 @@ class AdminManageAnnouncements extends React.Component {
if (data.success === true) {
message.success({ content: "Deleted announcements [" + ids.join(', ') + "] successfully!" })
this.fillTableData()

this.setState({ selectedTableKeys: [] })
}
else {
message.error({ content: "Oops. Unknown error" })
Expand All @@ -243,8 +243,6 @@ class AdminManageAnnouncements extends React.Component {
message.error({ content: "Oops. There was an issue connecting with the server" });
})
close()
this.setState({ selectedTableKeys: [] })

}

editAnnouncementOpen = (id) => {
Expand Down
Loading

0 comments on commit b8cde91

Please sign in to comment.