diff --git a/database/accounts/accounts.js b/database/accounts/accounts.js index a8bab35..84c7a35 100644 --- a/database/accounts/accounts.js +++ b/database/accounts/accounts.js @@ -12,6 +12,24 @@ class User { } +async function checkForAdminAccount() { + + const adminUsersQuery = `SELECT * + FROM accounts.users + WHERE admin = true;`; + const adminUsers = await database.executeQuery(adminUsersQuery); + + if(adminUsers.length == 0) { + const passwordHash = await generateHash('admin'); + const createTempAdminQuery = `INSERT INTO accounts.users(email, password, admin) + VALUES('admin@example.com', $1, true);`; + database.executeQuery(createTempAdminQuery, [passwordHash]); + console.log("Created temp admin account 'admin@example.com' with password 'admin'."); + } +} +checkForAdminAccount(); + + passport.use(new localStrategy({ usernameField: 'email', passwordField: 'password'}, diff --git a/database/database.js b/database/database.js index 2f9277e..349e2bc 100644 --- a/database/database.js +++ b/database/database.js @@ -27,13 +27,13 @@ async function Initialize() { async function checkForDatabaseInitialization() { - const query = `SELECT schema_name FROM information_schema.schemata WHERE schema_name = 'scores'`; - let result = await executeQuery(query); + const scoresSchemaExistsQuery = `SELECT schema_name FROM information_schema.schemata WHERE schema_name = 'scores'`; + let result = await executeQuery(scoresSchemaExistsQuery); const scoresSchemaExists = result.length !== 0; if(!scoresSchemaExists) { - Initialize(); + await Initialize(); } } checkForDatabaseInitialization(); diff --git a/database/scores/games.js b/database/scores/games.js index 270941b..3506356 100644 --- a/database/scores/games.js +++ b/database/scores/games.js @@ -19,12 +19,12 @@ class Game { -async function add(divisionID, seasonID, date, team1ID, team2ID, team1Score, team2Score) { - const query = `INSERT INTO scores.games(division_id, season_id, game_date, team1_id, team2_id, team1_score, team2_score) - VALUES($1, $2, $3, $4, $5, $6, $7) +async function add(divisionID, seasonID, date, team1ID, team2ID, team1Score, team2Score, userID) { + const query = `INSERT INTO scores.games(division_id, season_id, game_date, team1_id, team2_id, team1_score, team2_score, submitter_id) + VALUES($1, $2, $3, $4, $5, $6, $7, $8) RETURNING game_id;`; - const id = (await database.executeQuery(query, [divisionID, seasonID, date, team1ID, team2ID, team1Score, team2Score]))[0][0]; + const id = (await database.executeQuery(query, [divisionID, seasonID, date, team1ID, team2ID, team1Score, team2Score, userID]))[0][0]; return new Game(id, date, team1ID, team2ID, team1Score, team2Score); } @@ -71,6 +71,20 @@ async function retrieve(teamID, divisionID, seasonID) { return gamesList; } +async function retrieveByUser(userID) { + const query = `SELECT game_id, division_id, season_id, game_date, team1_id, team2_id, team1_score, team2_score + FROM scores.games + WHERE submitter_id = $1 + ORDER BY game_date DESC;`; + const table = await database.executeQuery(query, [userID]); + + const gamesList = []; + table.forEach((row) => { + gamesList.push(new Game(row[0], row[3].toISOString().slice(0,10), row[4], row[5], row[6], row[7], row[1], row[2])); + }); + return gamesList; +} + async function edit(gameID, divisionID, seasonID, date, team1ID, team2ID, team1Score, team2Score) { const query = `UPDATE scores.games SET division_id = $2, @@ -100,5 +114,6 @@ async function getFromID(gameID) { exports.add = add; exports.remove = remove; exports.retrieve = retrieve; +exports.retrieveByUser = retrieveByUser; exports.edit = edit; exports.getFromID = getFromID; \ No newline at end of file diff --git a/public/scripts/data.js b/public/scripts/data.js index ae8dc7f..e8c5898 100644 --- a/public/scripts/data.js +++ b/public/scripts/data.js @@ -65,6 +65,13 @@ export async function getGames(teamID = undefined, divisionID = undefined, seaso return gamesList; } +export async function getGamesByUser() { + let URL = '/data/games?user=1'; + const response = await fetch(URL); + const gamesList = await response.json(); + return gamesList; +} + export async function getGame(gameID) { const response = await fetch(`/data/game?game=${gameID}`); const game = await response.json(); diff --git a/public/scripts/manage/manage-nonadmin.js b/public/scripts/manage/manage-nonadmin.js index 4fc4b02..d549030 100644 --- a/public/scripts/manage/manage-nonadmin.js +++ b/public/scripts/manage/manage-nonadmin.js @@ -97,7 +97,7 @@ async function editGame(id) { async function listItems() { - const gamesList = await Data.getGames(); + const gamesList = await Data.getGamesByUser(); await listGameHeaders(); diff --git a/routes/data.js b/routes/data.js index d010bd1..3d71892 100644 --- a/routes/data.js +++ b/routes/data.js @@ -62,8 +62,9 @@ router.get('/team', function(req, res, next) { }) router.get('/games', function(req, res, next) { - games.retrieve(req.query.team, req.query.division, req.query.season) - .then(data => res.json(data)); + const userID = req.user[0]; + if(req.query.user) games.retrieveByUser(userID).then(data => res.json(data)); + else games.retrieve(req.query.team, req.query.division, req.query.season).then(data => res.json(data)); }) router.get('/game', function(req, res, next) { diff --git a/routes/manage.js b/routes/manage.js index aece4fc..b01b477 100644 --- a/routes/manage.js +++ b/routes/manage.js @@ -31,7 +31,7 @@ function adminLoggedIn(req, res, next) { router.get('/' ,userLoggedIn, function(req, res, next) { if(req.user[2]) res.render('manage', { title: 'Score Management' }); - else res.render('manage/manage-nonadmin', { title: "Manage Games" }); + else res.render('manage/manage-nonadmin', { title: "My Games" }); }); router.get('/game', userLoggedIn, function(req, res, next) { @@ -50,6 +50,7 @@ router.post('/game', userLoggedIn, function(req, res, next) { const team1Score = req.body['team1-score']; const team2ID = req.body['team2']; const team2Score = req.body['team2-score']; + const userID = req.user[0]; const id = req.body['game']; const remove = req.body['remove']; @@ -58,7 +59,7 @@ router.post('/game', userLoggedIn, function(req, res, next) { .then(res.redirect("/manage")); else if(id) games.edit(id, divisionID, seasonID, date, team1ID, team2ID, team1Score, team2Score) .then(res.redirect('/manage')); - else games.add(divisionID, seasonID, date, team1ID, team2ID, team1Score, team2Score) + else games.add(divisionID, seasonID, date, team1ID, team2ID, team1Score, team2Score, userID) .then(res.redirect("/manage")); });