Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

verifyContestModerator middleware #82

Merged
merged 20 commits into from
Dec 15, 2020
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
Show all changes
20 commits
Select commit Hold shift + click to select a range
aad13f0
verifyContestModerator middleware
ridhishjain Dec 15, 2020
2f26358
Update server/routes/contests/updateContest.js
ridhishjain Dec 15, 2020
e6d0d1d
Update server/routes/contests/removeModerator.js
ridhishjain Dec 15, 2020
8f833cb
Update server/routes/contests/removeGroup.js
ridhishjain Dec 15, 2020
536a792
Update server/routes/contests/addModerator.js
ridhishjain Dec 15, 2020
5fb000e
Update server/routes/contests/addGroup.js
ridhishjain Dec 15, 2020
e2ab09a
Update server/models/contests/addGroup.js
ridhishjain Dec 15, 2020
ff35922
Update server/models/contests/addGroup.js
ridhishjain Dec 15, 2020
0d9a7a2
Update server/models/contests/addModerator.js
ridhishjain Dec 15, 2020
c4b07fc
Update server/models/contests/addModerator.js
ridhishjain Dec 15, 2020
7809d5f
Update server/models/contests/addModerator.js
ridhishjain Dec 15, 2020
099ac00
Update server/models/contests/removeGroup.js
ridhishjain Dec 15, 2020
d75a8b6
Update server/models/contests/removeGroup.js
ridhishjain Dec 15, 2020
f39bd98
Update server/models/contests/updateContest.js
ridhishjain Dec 15, 2020
690cb85
Update server/models/contests/updateContest.js
ridhishjain Dec 15, 2020
ea4956b
Update server/models/contests/createContest.js
ridhishjain Dec 15, 2020
d1fe775
Update server/models/contests/createContest.js
ridhishjain Dec 15, 2020
25bbe73
Update server/models/contests/createContest.js
ridhishjain Dec 15, 2020
827e207
Update server/models/contests/createContest.js
ridhishjain Dec 15, 2020
5fcce60
Update server/models/contests/createContest.js
ridhishjain Dec 15, 2020
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
38 changes: 16 additions & 22 deletions server/models/contests/addGroup.js
Original file line number Diff line number Diff line change
Expand Up @@ -13,30 +13,24 @@ function addGroup({ params, body, username }) {
const { contest_id: contestId } = params
const { group_id: groupId } = body
pool.query(
`SELECT * FROM contests_moderators WHERE contest_id=? AND moderator=?`,
[contestId, username],
(error, results) => {
if (error) {
`INSERT INTO contests_groups (contest_id, group_id) SELECT contest_id, ? FROM contests_moderators WHERE contest_id=? AND moderator=?`,
[groupId, contestId, username],
(error, res) => {
if (error || res === undefined) {
const { code } = error
if (code === 'ER_DUP_ENTRY') {
return reject('The group is already added')
}
ridhishjain-zepto marked this conversation as resolved.
Show resolved Hide resolved
return reject(error)
} else if (!results || !results.length) {
return reject(
'Invalid contest ID or you do not have moderator access to the contest'
)
}
pool.query(
`INSERT INTO contests_groups (contest_id, group_id) VALUES (?,?)`,
[contestId, groupId],
(error, res) => {
if (error || res === undefined) {
const { code } = error
if (code === 'ER_DUP_ENTRY') {
return reject('The group is already added')
}
return reject(error)
}
return resolve('Successfully added group')
} else {
const { affectedRows } = res
if (!affectedRows) {
return reject(
'The user do not have moderator access of the contest'
)
}
)
return resolve('Successfully added group')
}
}
)
})
Expand Down
38 changes: 16 additions & 22 deletions server/models/contests/addModerator.js
Original file line number Diff line number Diff line change
Expand Up @@ -13,30 +13,24 @@ function addModerator({ params, body, username }) {
const { contest_id: contestId } = params
const { moderator: moderatorUsername } = body
pool.query(
`SELECT * FROM contests_moderators WHERE contest_id=? AND moderator=?`,
[contestId, username],
(error, results) => {
if (error) {
`INSERT INTO contests_moderators (contest_id, moderator) SELECT contest_id, ? FROM contests_moderators WHERE contest_id=? AND moderator=?`,
[moderatorUsername, contestId, username],
(error, res) => {
if (error || res === undefined) {
const { code } = error
if (code === 'ER_DUP_ENTRY') {
return reject('The user is already a moderator')
}
ridhishjain-zepto marked this conversation as resolved.
Show resolved Hide resolved
return reject(error)
} else if (!results || !results.length) {
return reject(
'Invalid contest ID or you do not have moderator access to the contest'
)
}
pool.query(
`INSERT INTO contests_moderators (contest_id, moderator) VALUES (?,?)`,
[contestId, moderatorUsername],
(error, res) => {
if (error || res === undefined) {
const { code } = error
if (code === 'ER_DUP_ENTRY') {
return reject('The user is already a moderator')
}
return reject(error)
}
return resolve('Successfully made moderator!!')
} else {
const { affectedRows } = res
if (!affectedRows) {
return reject(
'The user do not have moderator access of the contest'
)
}
)
return resolve('Successfully made moderator!!')
}
ridhishjain-zepto marked this conversation as resolved.
Show resolved Hide resolved
}
)
})
Expand Down
119 changes: 61 additions & 58 deletions server/models/contests/createContest.js
Original file line number Diff line number Diff line change
Expand Up @@ -20,71 +20,74 @@ function createContest({ username, body }) {
} = body
const startTime = new Date(start_time)
const endTime = new Date(end_time)
let about = body.about || null
let rules = body.rules || null
let prizes = body.prizes || null
let confidentialQuestions = body.confidential_questions || null
let currentId
pool.query(
`INSERT INTO contests (creator, name, show_leaderboard, public, start_time, end_time, participants_count) VALUES (?,?,?,?,?,?,?)`,
[username, name, showLeaderboard, public, startTime, endTime, 0],
(error, results) => {
if (error || results === undefined) {
return reject(error)
pool.getConnection((error, connection) => {
if (error) {
return reject(error)
}
connection.beginTransaction((error) => {
if (error) {
return connection.rollback(() => {
connection.release()
return reject(error)
ridhishjain-zepto marked this conversation as resolved.
Show resolved Hide resolved
})
}
const { insertId } = results
currentId = insertId
pool.query(
`INSERT INTO contests_moderators (contest_id, moderator) VALUES (?,?)`,
[currentId, username],
(error, res) => {
if (error || res === undefined) {
return reject(error)
}
const {
about,
rules,
prizes,
confidential_questions: confidentialQuestions,
} = body
let query = `UPDATE contests SET `
let arr = []
if (about) {
query += `about=?,`
arr.push(about)
}
if (rules) {
query += `rules=?,`
arr.push(rules)
}
if (prizes) {
query += `prizes=?,`
arr.push(prizes)
}
if (confidentialQuestions !== undefined) {
query += `confidential_questions=?,`
arr.push(confidentialQuestions)
}

if (query[query.length - 1] === ',') {
query = query.substr(0, query.length - 1)
} else {
return resolve({
message: 'Contest created successfully',
contestId: currentId,
connection.query(
`INSERT INTO contests (creator, name, show_leaderboard, public, start_time, end_time, about, rules, prizes, confidential_questions) VALUES (?,?,?,?,?,?,?,?,?,?)`,
[
username,
name,
showLeaderboard,
public,
startTime,
endTime,
about,
rules,
prizes,
confidentialQuestions,
],
(error, results) => {
if (error || results === undefined) {
return connection.rollback(() => {
connection.release()
return reject(error)
ridhishjain-zepto marked this conversation as resolved.
Show resolved Hide resolved
})
}
ridhishjain-zepto marked this conversation as resolved.
Show resolved Hide resolved
query += ` WHERE id=?`
arr.push(currentId)
pool.query(query, arr, (error, res) => {
if (error || res === undefined) {
return reject(error)
const { insertId } = results
currentId = insertId
connection.query(
`INSERT INTO contests_moderators (contest_id, moderator) VALUES (?,?)`,
[currentId, username],
(error, insertionResults) => {
if (error || insertionResults === undefined) {
return connection.rollback(() => {
connection.release()
return reject(error)
ridhishjain-zepto marked this conversation as resolved.
Show resolved Hide resolved
})
}
connection.commit((error) => {
if (error) {
return connection.rollback(() => {
connection.release()
return reject(error)
ridhishjain-zepto marked this conversation as resolved.
Show resolved Hide resolved
})
}
connection.release()
return resolve({
ridhishjain-zepto marked this conversation as resolved.
Show resolved Hide resolved
message: 'Contest created successfully',
contestId: currentId,
})
})
}
return resolve({
message: 'Contest created successfully',
contestId: currentId,
})
})
)
}
)
}
)
})
})
})
}

Expand Down
29 changes: 9 additions & 20 deletions server/models/contests/removeGroup.js
Original file line number Diff line number Diff line change
Expand Up @@ -13,30 +13,19 @@ function removeGroup({ params, body, username }) {
const { contest_id: contestId } = params
const { group_id: groupId } = body
pool.query(
`SELECT * FROM contests_moderators WHERE contest_id=? AND moderator=?`,
[contestId, username],
(error, results) => {
if (error) {
`DELETE FROM contests_groups WHERE (SELECT COUNT(id) FROM contests_moderators WHERE contest_id=? AND moderator=?) AND contest_id=? AND group_id=?`,
[contestId, username, contestId, groupId],
(error, res) => {
if (error || res === undefined) {
ridhishjain-zepto marked this conversation as resolved.
Show resolved Hide resolved
return reject(error)
} else if (!results || !results.length) {
}
ridhishjain-zepto marked this conversation as resolved.
Show resolved Hide resolved
const { affectedRows } = res
if (affectedRows === 0) {
return reject(
'Invalid contest ID or you do not have moderator access to the contest'
'Either the group is not a part of the contest or you do not have the required permissions'
)
}
pool.query(
`DELETE FROM contests_groups WHERE contest_id=? AND group_id=?`,
[contestId, groupId],
(error, res) => {
if (error || res === undefined) {
return reject(error)
}
const { affectedRows } = res
if (affectedRows === 0) {
return reject('The group is not a part of the contest')
}
return resolve('Successfully removed group')
}
)
return resolve('Successfully removed group')
}
)
})
Expand Down
34 changes: 11 additions & 23 deletions server/models/contests/removeModerator.js
Original file line number Diff line number Diff line change
Expand Up @@ -4,40 +4,28 @@ const { pool } = require('../database')
* @param {*} param0
* @param {Object} param0.body
* @param {Object} param0.params
* @param {String} param0.username
* @return {Promise}
*/

function removeModerator({ params, body, username }) {
function removeModerator({ params, body }) {
return new Promise((resolve, reject) => {
const { contest_id: contestId } = params
const { moderator: moderatorUsername } = body
pool.query(
`SELECT * FROM contests_moderators WHERE contest_id=? AND moderator=?`,
[contestId, username],
(error, results) => {
if (error) {
`DELETE FROM contests_moderators WHERE contest_id=? AND moderator=?
AND moderator NOT IN (SELECT creator FROM contests WHERE contest_id=?)`,
[contestId, moderatorUsername, contestId],
(error, res) => {
if (error || res === undefined) {
ridhishjain-zepto marked this conversation as resolved.
Show resolved Hide resolved
return reject(error)
} else if (!results || !results.length) {
}
const { affectedRows } = res
if (!affectedRows) {
return reject(
'Invalid contest ID or you do not have moderator access to the contest'
'Either the person is already not a moderator or you do not have the required permissions'
)
}
pool.query(
`DELETE FROM contests_moderators WHERE contest_id=? AND moderator=?
AND moderator NOT IN (SELECT creator FROM contests WHERE contest_id=?)`,
[contestId, moderatorUsername, contestId],
(error, res) => {
if (error || res === undefined) {
return reject(error)
}
const { affectedRows } = res
if (affectedRows === 0) {
return reject('Either user was a creator or not a moderator')
}
return resolve('Successfully removed as moderator')
}
)
return resolve('Successfully removed as moderator')
}
)
})
Expand Down
Loading