2021-11-22 06:17:43 +00:00
|
|
|
var express = require('express');
|
|
|
|
var router = express.Router();
|
2021-11-26 22:50:43 +00:00
|
|
|
|
2021-11-23 00:48:51 +00:00
|
|
|
var genders = require('../database/scores/genders');
|
|
|
|
var games = require('../database/scores/games');
|
2021-11-23 01:04:36 +00:00
|
|
|
var seasons = require('../database/scores/seasons');
|
2021-11-23 01:47:40 +00:00
|
|
|
var sports = require('../database/scores/sports');
|
2021-11-23 02:11:16 +00:00
|
|
|
var divisions = require('../database/scores/divisions');
|
|
|
|
var genders = require('../database/scores/genders');
|
2021-11-23 02:17:23 +00:00
|
|
|
var teams = require('../database/scores/teams');
|
2021-11-25 19:40:19 +00:00
|
|
|
var accounts = require('../database/accounts/accounts');
|
2021-11-23 00:48:51 +00:00
|
|
|
|
2021-11-26 22:50:43 +00:00
|
|
|
var checkLoginStatus = require('./checkLoginStatus');
|
2021-11-25 04:40:33 +00:00
|
|
|
|
2021-11-22 06:17:43 +00:00
|
|
|
|
2021-11-26 22:50:43 +00:00
|
|
|
router.get('/' ,checkLoginStatus.user, function(req, res, next) {
|
2021-11-27 05:00:43 +00:00
|
|
|
if(req.user[2]) res.render('manage', { title: 'Management Panel', userLoggedIn: !!req.user });
|
2021-11-26 19:37:09 +00:00
|
|
|
else res.render('manage/manage-nonadmin', { title: "My Games", userLoggedIn: !!req.user });
|
2021-11-22 06:17:43 +00:00
|
|
|
});
|
|
|
|
|
2021-11-26 22:50:43 +00:00
|
|
|
router.get('/game', checkLoginStatus.user, function(req, res, next) {
|
2021-11-23 07:49:11 +00:00
|
|
|
let title = req.query.game ? 'Edit Game' : 'Submit Score'
|
|
|
|
|
2021-11-26 23:29:03 +00:00
|
|
|
res.render('manage/addgame', { title, userLoggedIn: !!req.user, message: req.flash('error') });
|
2021-11-23 00:48:51 +00:00
|
|
|
});
|
|
|
|
|
2021-11-26 23:29:03 +00:00
|
|
|
router.post('/game', checkLoginStatus.user, async function(req, res, next) {
|
2021-11-26 23:59:51 +00:00
|
|
|
const id = req.body['game'];
|
|
|
|
const remove = req.body['remove'];
|
|
|
|
|
2021-11-26 23:29:03 +00:00
|
|
|
try {
|
|
|
|
const seasonID = req.body['year'];
|
|
|
|
const sportID = req.body['sport'];
|
|
|
|
const gender = (req.body['gender'] == "female") ? genders.FEMALE : genders.MALE;
|
|
|
|
const divisionID = req.body['division'];
|
|
|
|
const date = req.body['date'];
|
|
|
|
const team1ID = req.body['team1'];
|
|
|
|
const team1Score = req.body['team1-score'];
|
|
|
|
const team2ID = req.body['team2'];
|
|
|
|
const team2Score = req.body['team2-score'];
|
|
|
|
const userID = req.user[0];
|
|
|
|
|
|
|
|
const loggedInUserID = req.user[0];
|
|
|
|
const loggedInUserIsAdmin = req.user[2];
|
|
|
|
|
|
|
|
const game = id ? await games.getFromID(id) : null;
|
|
|
|
|
|
|
|
if(!loggedInUserIsAdmin && game && loggedInUserID != game.submitterID) {
|
|
|
|
res.status(403).send("ACCESS DENIED");
|
|
|
|
}
|
|
|
|
else if(remove) {
|
|
|
|
await games.remove(id);
|
2021-11-27 05:33:44 +00:00
|
|
|
res.redirect('/manage#games');
|
2021-11-26 23:29:03 +00:00
|
|
|
}
|
|
|
|
else if(id) {
|
|
|
|
await games.edit(id, divisionID, seasonID, date, team1ID, team2ID, team1Score, team2Score);
|
2021-11-27 05:33:44 +00:00
|
|
|
res.redirect('/manage#games');
|
2021-11-26 23:29:03 +00:00
|
|
|
}
|
|
|
|
else {
|
|
|
|
await games.add(divisionID, seasonID, date, team1ID, team2ID, team1Score, team2Score, userID);
|
|
|
|
res.redirect('/');
|
|
|
|
}
|
|
|
|
} catch(err) {
|
|
|
|
console.error("ERROR: " + err.message);
|
|
|
|
req.flash("error", "An error has occurred.");
|
2021-11-26 23:59:51 +00:00
|
|
|
res.redirect('/manage/game' + (id ? `?game=${id}` : ''));
|
2021-11-26 23:29:03 +00:00
|
|
|
}
|
2021-11-23 00:48:51 +00:00
|
|
|
});
|
|
|
|
|
2021-11-26 22:50:43 +00:00
|
|
|
router.get('/season', checkLoginStatus.admin, function(req, res, next) {
|
2021-11-26 23:34:26 +00:00
|
|
|
res.render('manage/addseason', { title: 'Add Season', currentYear : (new Date()).getFullYear(), userLoggedIn: !!req.user, message: req.flash('error') });
|
2021-11-23 01:04:36 +00:00
|
|
|
});
|
|
|
|
|
2021-11-26 23:34:26 +00:00
|
|
|
router.post('/season', checkLoginStatus.admin, async function(req, res, next) {
|
2021-11-26 23:59:51 +00:00
|
|
|
const seasonID = req.body['season'];
|
|
|
|
const remove = req.body['remove'];
|
|
|
|
|
2021-11-26 23:34:26 +00:00
|
|
|
try {
|
|
|
|
const year = req.body['year'];
|
2021-11-23 01:04:36 +00:00
|
|
|
|
2021-11-26 23:34:26 +00:00
|
|
|
if(remove) await seasons.remove(seasonID);
|
|
|
|
else await seasons.add(year);
|
2021-11-23 21:48:19 +00:00
|
|
|
|
2021-11-27 05:33:44 +00:00
|
|
|
res.redirect('/manage#seasons');
|
2021-11-26 23:34:26 +00:00
|
|
|
} catch(err) {
|
|
|
|
console.error("ERROR: " + err.message);
|
|
|
|
req.flash("error", "An error has occurred.");
|
2021-11-26 23:59:51 +00:00
|
|
|
res.redirect('/manage/season' + (seasonID ? `?season=${seasonID}` : ''));
|
2021-11-26 23:34:26 +00:00
|
|
|
}
|
2021-11-23 01:04:36 +00:00
|
|
|
});
|
|
|
|
|
2021-11-26 22:50:43 +00:00
|
|
|
router.get('/sport', checkLoginStatus.admin, function(req, res, next) {
|
2021-11-26 23:59:51 +00:00
|
|
|
let title = req.query.sport ? 'Edit Sport' : 'Add Sport';
|
|
|
|
|
|
|
|
res.render('manage/addsport', { title, userLoggedIn: !!req.user, message: req.flash('error') });
|
2021-11-23 01:47:40 +00:00
|
|
|
});
|
|
|
|
|
2021-11-26 23:38:46 +00:00
|
|
|
router.post('/sport', checkLoginStatus.admin, async function(req, res, next) {
|
2021-11-26 23:59:51 +00:00
|
|
|
const id = req.body['sport'];
|
|
|
|
const remove = req.body['remove'];
|
|
|
|
|
2021-11-26 23:38:46 +00:00
|
|
|
try {
|
|
|
|
const name = req.body['name'];
|
2021-11-23 01:47:40 +00:00
|
|
|
|
2021-11-26 23:38:46 +00:00
|
|
|
if(remove) await sports.remove(id);
|
|
|
|
else if(id) await sports.rename(id, name);
|
|
|
|
else await sports.add(name);
|
|
|
|
|
2021-11-27 05:33:44 +00:00
|
|
|
res.redirect('/manage#sports');
|
2021-11-26 23:38:46 +00:00
|
|
|
} catch(err) {
|
|
|
|
console.error("ERROR: " + err.message);
|
|
|
|
req.flash("error", "An error has occurred.");
|
2021-11-26 23:59:51 +00:00
|
|
|
res.redirect('/manage/sport' + (id ? `?sport=${id}` : ''));
|
2021-11-26 23:38:46 +00:00
|
|
|
}
|
2021-11-23 01:47:40 +00:00
|
|
|
});
|
|
|
|
|
2021-11-26 22:50:43 +00:00
|
|
|
router.get('/division', checkLoginStatus.admin, function(req, res, next) {
|
2021-11-23 06:23:57 +00:00
|
|
|
let title = req.query.division ? 'Edit Division' : 'Add Division'
|
|
|
|
|
2021-11-26 23:46:46 +00:00
|
|
|
res.render('manage/adddivision', { title, userLoggedIn: !!req.user, message: req.flash('error') });
|
2021-11-23 02:11:16 +00:00
|
|
|
});
|
|
|
|
|
2021-11-26 23:46:46 +00:00
|
|
|
router.post('/division', checkLoginStatus.admin, async function(req, res, next) {
|
2021-11-26 23:59:51 +00:00
|
|
|
const id = req.body['division'];
|
|
|
|
const remove = req.body['remove'];
|
|
|
|
|
2021-11-26 23:46:46 +00:00
|
|
|
try {
|
|
|
|
const name = req.body['name'];
|
|
|
|
const sport = req.body['sport'];
|
|
|
|
const genderName = req.body['gender'];
|
2021-11-26 23:59:51 +00:00
|
|
|
|
2021-11-26 23:46:46 +00:00
|
|
|
if(remove) await divisions.remove(id);
|
|
|
|
else if(id) await divisions.rename(id, name);
|
2021-11-23 06:23:57 +00:00
|
|
|
else {
|
2021-11-26 23:46:46 +00:00
|
|
|
if(genderName == 'both') {
|
|
|
|
await divisions.add(name, genders.FEMALE, sport);
|
|
|
|
await divisions.add(name, genders.MALE, sport);
|
|
|
|
} else {
|
|
|
|
const gender = (genderName == "female") ? genders.FEMALE : genders.MALE;
|
|
|
|
await divisions.add(name, gender, sport);
|
|
|
|
}
|
2021-11-23 06:23:57 +00:00
|
|
|
}
|
2021-11-27 05:33:44 +00:00
|
|
|
res.redirect('/manage#divisions');
|
2021-11-26 23:46:46 +00:00
|
|
|
} catch(err) {
|
|
|
|
console.error("ERROR: " + err.message);
|
|
|
|
req.flash("error", "An error has occurred.");
|
2021-11-26 23:59:51 +00:00
|
|
|
res.redirect('/manage/division' + (id ? `?division=${id}` : ''));
|
2021-11-23 02:11:16 +00:00
|
|
|
}
|
|
|
|
});
|
|
|
|
|
2021-11-26 22:50:43 +00:00
|
|
|
router.get('/team', checkLoginStatus.admin, function(req, res, next) {
|
2021-11-23 06:45:24 +00:00
|
|
|
let title = req.query.team ? 'Edit Team' : 'Add Team'
|
|
|
|
|
2021-11-27 00:03:09 +00:00
|
|
|
res.render('manage/addteam', { title, userLoggedIn: !!req.user, message: req.flash('error') });
|
2021-11-23 02:17:23 +00:00
|
|
|
});
|
|
|
|
|
2021-11-27 00:03:09 +00:00
|
|
|
router.post('/team', checkLoginStatus.admin, async function(req, res, next) {
|
2021-11-23 06:45:24 +00:00
|
|
|
const id = req.body['team'];
|
|
|
|
const remove = req.body['remove'];
|
|
|
|
|
2021-11-27 00:03:09 +00:00
|
|
|
try {
|
|
|
|
const name = req.body['name'];
|
|
|
|
const sport = req.body['sport'];
|
|
|
|
|
2021-11-27 05:33:44 +00:00
|
|
|
if(remove) teams.remove(id).then(res.redirect('/manage#teams'));
|
|
|
|
else if(id) teams.rename(id, name).then(res.redirect('/manage#teams'));
|
|
|
|
else teams.add(name, sport).then(res.redirect('/manage#teams'));
|
2021-11-27 00:03:09 +00:00
|
|
|
} catch(err) {
|
|
|
|
console.error("ERROR: " + err.message);
|
|
|
|
req.flash("error", "An error has occurred.");
|
|
|
|
res.redirect('/manage/team' + (id ? `?team=${id}` : ''));
|
|
|
|
}
|
2021-11-23 02:17:23 +00:00
|
|
|
});
|
|
|
|
|
2021-11-26 22:50:43 +00:00
|
|
|
router.get('/account', checkLoginStatus.user, (req, res, next) => {
|
2021-11-26 19:08:45 +00:00
|
|
|
const userIsAdmin = req.user[2];
|
|
|
|
const accountID = req.user[0];
|
|
|
|
|
|
|
|
if(userIsAdmin) {
|
|
|
|
let title = req.query.account ? 'Manage User' : 'Create User'
|
|
|
|
|
2021-11-26 22:41:36 +00:00
|
|
|
res.render('accounts/createuser', { title, userLoggedIn: !!req.user, message: req.flash('error') });
|
2021-11-26 19:08:45 +00:00
|
|
|
}
|
|
|
|
else {
|
|
|
|
let title = 'Manage Account';
|
2021-11-25 19:17:53 +00:00
|
|
|
|
2021-11-26 22:41:36 +00:00
|
|
|
res.render('accounts/createuser', { title, accountID, userLoggedIn: !!req.user, message: req.flash('error') });
|
2021-11-26 19:08:45 +00:00
|
|
|
}
|
2021-11-25 05:29:29 +00:00
|
|
|
});
|
|
|
|
|
2021-11-26 22:50:43 +00:00
|
|
|
router.post('/account', checkLoginStatus.user, async function(req, res, next) {
|
2021-12-02 20:16:07 +00:00
|
|
|
const name = req.body.name;
|
2021-11-25 19:40:19 +00:00
|
|
|
const email = req.body.email;
|
|
|
|
const password = req.body.password;
|
|
|
|
|
|
|
|
const accountID = req.body.account;
|
2021-11-25 19:49:31 +00:00
|
|
|
const remove = req.body.remove;
|
2021-11-25 19:40:19 +00:00
|
|
|
|
2021-11-26 19:08:45 +00:00
|
|
|
const loggedInAccountIsAdmin = req.user[2];
|
|
|
|
const loggedInAccountID = req.user[0];
|
|
|
|
|
|
|
|
if(!loggedInAccountIsAdmin && accountID != loggedInAccountID) {
|
|
|
|
res.status(403).send("ACCESS DENIED");
|
|
|
|
}
|
|
|
|
else {
|
2021-11-26 22:41:36 +00:00
|
|
|
try {
|
|
|
|
const isAdmin = loggedInAccountIsAdmin ? !!req.body.admin : false;
|
|
|
|
|
|
|
|
if(remove) await accounts.remove(accountID);
|
2021-12-02 20:16:07 +00:00
|
|
|
else if(accountID) await accounts.edit(accountID, email, password, isAdmin, name);
|
|
|
|
else await accounts.create(email, password, !!req.body.admin, name);
|
2021-11-26 19:08:45 +00:00
|
|
|
|
2021-11-27 05:33:44 +00:00
|
|
|
res.redirect('/manage#accounts');
|
2021-11-26 22:41:36 +00:00
|
|
|
}
|
|
|
|
catch (err) {
|
|
|
|
console.error("ERROR: " + err.message);
|
|
|
|
req.flash("error", "An error has occurred.");
|
2021-11-27 00:08:13 +00:00
|
|
|
let URL = '/manage/account';
|
|
|
|
if(loggedInAccountIsAdmin && accountID) URL += `?account=${accountID}`;
|
|
|
|
res.redirect(URL);
|
2021-11-26 22:41:36 +00:00
|
|
|
}
|
2021-11-26 19:08:45 +00:00
|
|
|
}
|
2021-11-25 19:40:19 +00:00
|
|
|
});
|
|
|
|
|
2021-11-22 06:17:43 +00:00
|
|
|
module.exports = router;
|