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 11 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
34 changes: 10 additions & 24 deletions server/models/contests/addGroup.js
Original file line number Diff line number Diff line change
Expand Up @@ -4,39 +4,25 @@ const { pool } = require('../database')
* @param {*} param0
* @param {Object} param0.body
* @param {Object} param0.params
* @param {String} param0.username
* @return {Promise}
*/

function addGroup({ params, body, username }) {
function addGroup({ params, body }) {
ridhishjain-zepto marked this conversation as resolved.
Show resolved Hide resolved
ridhishjain-zepto marked this conversation as resolved.
Show resolved Hide resolved
return new Promise((resolve, reject) => {
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 contest_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')
}
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')
}
)
return resolve('Successfully added group')
}
)
})
Expand Down
31 changes: 9 additions & 22 deletions server/models/contests/addModerator.js
Original file line number Diff line number Diff line change
Expand Up @@ -13,30 +13,17 @@ 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'
)
}
ridhishjain-zepto marked this conversation as resolved.
Show resolved Hide resolved
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!!')
}
)
return resolve('Successfully made moderator!!')
}
)
})
Expand Down
65 changes: 20 additions & 45 deletions server/models/contests/createContest.js
Original file line number Diff line number Diff line change
Expand Up @@ -20,10 +20,25 @@ 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(
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This has to be a transaction..first insert into contests table then insert into contest_moderators table

`INSERT INTO contests (creator, name, show_leaderboard, public, start_time, end_time, participants_count) VALUES (?,?,?,?,?,?,?)`,
[username, name, showLeaderboard, public, startTime, endTime, 0],
`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 reject(error)
Expand All @@ -37,49 +52,9 @@ function createContest({ username, body }) {
if (error || res === undefined) {
return reject(error)
}
ridhishjain-zepto marked this conversation as resolved.
Show resolved Hide resolved
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,
})
}
query += ` WHERE id=?`
arr.push(currentId)
pool.query(query, arr, (error, res) => {
if (error || res === undefined) {
return reject(error)
}
return resolve({
message: 'Contest created successfully',
contestId: currentId,
})
return resolve({
message: 'Contest created successfully',
contestId: currentId,
})
}
)
Expand Down
34 changes: 10 additions & 24 deletions server/models/contests/removeGroup.js
Original file line number Diff line number Diff line change
Expand Up @@ -4,39 +4,25 @@ const { pool } = require('../database')
* @param {*} param0
* @param {Object} param0.body
* @param {Object} param0.params
* @param {String} param0.username
* @return {Promise}
*/

function removeGroup({ params, body, username }) {
function removeGroup({ params, body }) {
return new Promise((resolve, reject) => {
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 contest_id=? AND group_id=?`,
ridhishjain-zepto marked this conversation as resolved.
Show resolved Hide resolved
[contestId, groupId],
ridhishjain-zepto marked this conversation as resolved.
Show resolved Hide resolved
(error, res) => {
if (error || res === undefined) {
return reject(error)
} else if (!results || !results.length) {
return reject(
'Invalid contest ID or you do not have moderator access to the contest'
)
}
ridhishjain-zepto marked this conversation as resolved.
Show resolved Hide resolved
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')
}
)
const { affectedRows } = res
if (affectedRows === 0) {
return reject('The group is not a part of the contest')
}
return resolve('Successfully removed group')
}
)
})
Expand Down
36 changes: 11 additions & 25 deletions server/models/contests/removeModerator.js
Original file line number Diff line number Diff line change
Expand Up @@ -4,40 +4,26 @@ 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) {
return reject(
'Invalid contest ID or you do not have moderator access to the contest'
)
}
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')
}
)
const { affectedRows } = res
if (affectedRows === 0) {
return reject('Either user was a creator or not a moderator')
}
return resolve('Successfully removed as moderator')
}
)
})
Expand Down
Loading