Skip to content

Commit

Permalink
fixed bug where team name == username
Browse files Browse the repository at this point in the history
  • Loading branch information
Tkaixiang committed Dec 26, 2021
1 parent fc3b1e6 commit 19078d6
Show file tree
Hide file tree
Showing 6 changed files with 24 additions and 10 deletions.
2 changes: 1 addition & 1 deletion api/api.js
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
const fastify = require('fastify')()
const fastify = require('fastify')({bodyLimit: 12485760})
const mongoSanitize = require('express-mongo-sanitize');
const fastifyFileUpload = require('fastify-file-upload');

Expand Down
3 changes: 3 additions & 0 deletions api/controllers/Scoreboard.js
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,8 @@ const scoreboard = async (req, res) => {

if (NodeCacheObj.get("teamMode")) {
for (username in transactionsCache) {
// Pushes transaction records of people who are NOT IN A TEAM [OR] THE TEAM TRANSACTIONS
// But it fails for teams whose usernames matches a user
if (!(username in usernameTeamCache)) finalData.push(transactionsCache[username])
}
}
Expand All @@ -16,6 +18,7 @@ const scoreboard = async (req, res) => {
}
}


res.send({
success: true,
users: finalData,
Expand Down
9 changes: 2 additions & 7 deletions api/controllers/Submissions.js
Original file line number Diff line number Diff line change
Expand Up @@ -5,16 +5,11 @@ const createTransactionsCache = require('./../utils/createTransactionsCache.js')

const submissions = async (req, res) => {
if (req.locals.perms < 2) throw new Error('Permissions');
const collections = Connection.collections
const users = await collections.users.find({}).toArray()
const userCatMapping = {}
for (let i = 0; i < users.length; i++) {
if ("category" in users[i]) userCatMapping[users[i].username] = users[i].category
}
const collections = Connection.collections;
res.send({
success: true,
submissions: await collections.transactions.find({}).toArray(),
userCatMapping: userCatMapping,
userCatMapping: NodeCacheObj.get("userCategories"),
categoryList: NodeCacheObj.get("categoryList")
});
}
Expand Down
10 changes: 10 additions & 0 deletions api/controllers/Teams.js
Original file line number Diff line number Diff line change
Expand Up @@ -224,6 +224,7 @@ const create = async (req, res) => {
if (NodeCacheObj.get("teamMode")) {
if (!NodeCacheObj.get("teamChangeDisable")) {
const collections = Connection.collections
const usernames = await collections.users.find({}, {projection: {username: 1, _id: 0}}).toArray()
let teamList = NodeCacheObj.get("teamListCache")
let usernameTeamCache = NodeCacheObj.get("usernameTeamCache")
// Is user already in a team?
Expand All @@ -243,6 +244,15 @@ const create = async (req, res) => {
success: false,
error: "bad-team-name"
})
// Team names must not be the same name as a username if not the transactionCache will fail to create unique entries
for (let i = 0; i < usernames.length; i++) {
if (usernames[i].username === req.body.name) {
return res.send({
success: false,
error: "same-name-as-user"
})
}
}
const newTeam = {
name: req.body.name,
members: [req.locals.username],
Expand Down
4 changes: 2 additions & 2 deletions client/src/AdminPanel/adminSubmissions.js
Original file line number Diff line number Diff line change
Expand Up @@ -386,7 +386,7 @@ class AdminSubmissions extends React.Component {
data.submissions[i].key = data.submissions[i]._id
data.submissions[i].timestamp = new Date(data.submissions[i].timestamp).toLocaleString("en-US", { timeZone: "Asia/Singapore" })

if (data.submissions[i].author in data.userCatMapping) data.submissions[i].category = data.userCatMapping[data.submissions[i].author]
if (data.submissions[i].author in data.userCatMapping && data.userCatMapping[data.submissions[i].author] !== "none") data.submissions[i].category = data.userCatMapping[data.submissions[i].author]
else data.submissions[i].category = "N/A"
}

Expand Down Expand Up @@ -666,7 +666,7 @@ class AdminSubmissions extends React.Component {
<Column title="Points Awarded" dataIndex="points" key="points" sorter={(a, b) => a.points - b.points} />

<Column title="Flag Submitted" dataIndex="submission" key="submission" />
<Column title="Correct" dataIndex="correct" key="correct" filters={[{ text: "True", value: "True" }, { text: "False", value: "False" }]} onFilter={(value, record) => { return value === record.correct }} />
<Column title="Correct" dataIndex="correct" key="correct" filters={[{ text: "", value: "" }, { text: "", value: "" }]} onFilter={(value, record) => { return value === record.correct }} />
<Column
title=""
key="edit"
Expand Down
6 changes: 6 additions & 0 deletions client/src/SidebarDropdown/Teams.js
Original file line number Diff line number Diff line change
Expand Up @@ -49,6 +49,12 @@ const CreateTeamForm = (props) => {
else if (data.error === "in-team") {
message.error("Already in a team. Please leave your team to create a new team")
}
else if (data.error === "same-name-as-user") {
message.error("The team name you have chosen is the same as a username. Please choose another name instead.")
}
else if (data.error === "team-full") {
message.error("This team is full and is unable to take anymore members.")
}
else {
message.error({ content: "Oops. Unknown error." })
}
Expand Down

0 comments on commit 19078d6

Please sign in to comment.