From 373e4e7923724b238cd3d78ea00400bd3a0ae39a Mon Sep 17 00:00:00 2001 From: Alex B Date: Sat, 23 Dec 2023 19:35:23 +0100 Subject: [PATCH 1/5] Important admin settings (Semester, Slack Webhooks, Questions URL, Rejoin Time) are now saved to a file and loaded if the queue is restarted --- server/controllers/settings.js | 37 ++++++++++++++++++++++++++++++++-- 1 file changed, 35 insertions(+), 2 deletions(-) diff --git a/server/controllers/settings.js b/server/controllers/settings.js index 1551df9..c3ba35c 100644 --- a/server/controllers/settings.js +++ b/server/controllers/settings.js @@ -11,14 +11,36 @@ const home = require('./home'); // Global admin settings // FIXME: some default values are set to simplify testing; // In production, these should be cleared +var fs = require('fs'); let adminSettings = { - currSem: "S23", + currSem: "F23", slackURL: null, questionsURL: '', - rejoinTime: 10 + rejoinTime: 15 }; +// If no admin setting have been generated, use the above default values +if (!fs.existsSync('../adminSettings.json')) { + var json = JSON.stringify(adminSettings) + fs.writeFile('../adminSettings.json', json, 'utf8', function () { + console.log('Created admin settings JSON'); + }); +} exports.get_admin_settings = function () { + + let data = fs.readFileSync('../adminSettings.json', 'utf8', flag = 'r+'); + if (data) { + adminSettings = JSON.parse(data); + } else { + console.log('No admin settings found'); + adminSettings = { + currSem: "F23", + slackURL: null, + questionsURL: '', + rejoinTime: 15 + }; + } + return adminSettings; } @@ -50,6 +72,13 @@ function respond_success(req, res, message = null) { respond(req, res, message, {}, 200); } +function writeAdminSettings (settings) { + var json = JSON.stringify(settings) + fs.writeFileSync('../adminSettings.json', json, 'utf8', function () { + return; + }); +} + /** General Settings **/ exports.post_update_video_chat = function (req, res) { if (!req.user || !req.user.isTA) { @@ -178,6 +207,7 @@ exports.post_update_semester = function (req, res) { } }).then(function (results) { adminSettings.currSem = results[0].sem_id; + writeAdminSettings(adminSettings); respond_success(req, res, `Current semester set to ${sem_id} successfully`); }).catch(err => { message = err.message || "An error occurred while updating current semester"; @@ -200,6 +230,7 @@ exports.post_update_slack_url = function (req, res) { if (adminSettings.slackURL == slackURL) return; adminSettings.slackURL = slackURL; + writeAdminSettings(adminSettings); slack.update_slack(); respond_success(req, res, `Slack Webhook URL updated successfully`); } @@ -219,6 +250,7 @@ exports.post_update_questions_url = function (req, res) { if (adminSettings.questionsURL == questionsURL) return; adminSettings.questionsURL = questionsURL; + writeAdminSettings(adminSettings); home.emit_new_queue_data(); respond_success(req, res, `Questions Guide URL updated successfully`); } @@ -238,6 +270,7 @@ exports.post_update_rejoin_time = function (req, res) { if (adminSettings.rejoinTime == rejoinTime) return; adminSettings.rejoinTime = rejoinTime; + writeAdminSettings(adminSettings); home.emit_new_queue_data(); respond_success(req, res, `Rejoin time updated successfully to ${rejoinTime} minutes`); } From d052ba7bbc23920fea6fb50277fe7c96b2ba67e8 Mon Sep 17 00:00:00 2001 From: Jackson Romero Date: Tue, 26 Dec 2023 10:59:44 -0500 Subject: [PATCH 2/5] add adminSettings.json to gitignore --- .gitignore | 1 + 1 file changed, 1 insertion(+) diff --git a/.gitignore b/.gitignore index 0c30ee3..7b8fc7d 100644 --- a/.gitignore +++ b/.gitignore @@ -117,3 +117,4 @@ dist # Config Files config.json +adminSettings.json From cb0b546ba5fba2f869cdd63f4b437c01ad9ebbbf Mon Sep 17 00:00:00 2001 From: Jackson Romero Date: Tue, 26 Dec 2023 11:00:08 -0500 Subject: [PATCH 3/5] fix queue rejoin field on settings page --- .../settings/admin/QueueRejoinSettings.tsx | 17 +++++++++++------ 1 file changed, 11 insertions(+), 6 deletions(-) diff --git a/client/src/components/settings/admin/QueueRejoinSettings.tsx b/client/src/components/settings/admin/QueueRejoinSettings.tsx index 75caa64..a79247c 100644 --- a/client/src/components/settings/admin/QueueRejoinSettings.tsx +++ b/client/src/components/settings/admin/QueueRejoinSettings.tsx @@ -1,5 +1,5 @@ -import React, {useContext} from 'react'; +import React, {useContext, useEffect, useState} from 'react'; import { Button, CardContent, Typography, TextField, Grid, } from '@mui/material'; @@ -11,18 +11,23 @@ import {QueueDataContext} from '../../../contexts/QueueDataContext'; export default function QueueRejoinSettings(props) { const {queueData} = useContext(QueueDataContext); + + const [rejoinTime, setRejoinTime] = useState(15); + + useEffect(() => { + setRejoinTime(queueData.rejoinTime); + }, [queueData]); + const onSubmit = (event) => { event.preventDefault(); SettingsService.updateRejoinTime( JSON.stringify({ - rejoinTime: rejoinTimeInput, + rejoinTime: rejoinTime, }), ); }; - let rejoinTimeInput = queueData.rejoinTime; - return ( @@ -39,9 +44,9 @@ export default function QueueRejoinSettings(props) { variant="standard" sx={{mx: 1, mt: -1}} style={{width: '50px'}} - defaultValue={queueData.rejoinTime ? queueData.rejoinTime : 10} + value={rejoinTime} onChange={(e) => { - rejoinTimeInput = parseInt(e.target.value, 10); + setRejoinTime(parseInt(e.target.value, 10)); }} inputProps={{min: 0, style: {textAlign: 'center'}}} /> From 4ebed9d20983acc55293559919eba73fd223541d Mon Sep 17 00:00:00 2001 From: Jackson Romero Date: Tue, 26 Dec 2023 12:05:41 -0500 Subject: [PATCH 4/5] Include locations in saved admin settings --- server/controllers/settings.js | 74 +++++++++++++++++++--------------- 1 file changed, 41 insertions(+), 33 deletions(-) diff --git a/server/controllers/settings.js b/server/controllers/settings.js index c3ba35c..9cd700c 100644 --- a/server/controllers/settings.js +++ b/server/controllers/settings.js @@ -16,7 +16,8 @@ let adminSettings = { currSem: "F23", slackURL: null, questionsURL: '', - rejoinTime: 15 + rejoinTime: 15, + dayDictionary: {} }; // If no admin setting have been generated, use the above default values if (!fs.existsSync('../adminSettings.json')) { @@ -24,7 +25,7 @@ if (!fs.existsSync('../adminSettings.json')) { fs.writeFile('../adminSettings.json', json, 'utf8', function () { console.log('Created admin settings JSON'); }); -} +} exports.get_admin_settings = function () { @@ -37,7 +38,8 @@ exports.get_admin_settings = function () { currSem: "F23", slackURL: null, questionsURL: '', - rejoinTime: 15 + rejoinTime: 15, + dayDictionary: {} }; } @@ -747,32 +749,35 @@ exports.post_upload_ta_csv = function (req, res) { } /* BEGIN LOCATIONS */ -let dayDictionary = {} // invariant: rooms are held at -1 to make sure they appear in the options, but could be empty days // when removing a room, need to remove it from -1 as well // mapping is 1-to-1 where Sunday = 0... Saturday = 6 const dayToRoomDictionary = (obj) => { - return Object.entries(obj).reduce((ret, entry) => { - const [key, rooms] = entry; - for (let roomIdx in rooms) { - let room = rooms[roomIdx] - if (ret[room]) { - // seen before - let keyInt = parseInt(key) - if (keyInt != null) { - ret[room].push(keyInt) - } - } else { - let keyInt = parseInt(key) - if (keyInt != null) { - ret[room] = [keyInt] + if (obj) { + return Object.entries(obj).reduce((ret, entry) => { + const [key, rooms] = entry; + for (let roomIdx in rooms) { + let room = rooms[roomIdx] + if (ret[room]) { + // seen before + let keyInt = parseInt(key) + if (keyInt != null) { + ret[room].push(keyInt) + } + } else { + let keyInt = parseInt(key) + if (keyInt != null) { + ret[room] = [keyInt] + } } } - } - return ret; - }, {}) + return ret; + }, {}) + } else { + return {} + } } exports.add_location = function (req, res) { @@ -787,12 +792,13 @@ exports.add_location = function (req, res) { return; } - if (dayDictionary["-1"]) { - dayDictionary["-1"].push(room) + if (adminSettings.dayDictionary["-1"]) { + adminSettings.dayDictionary["-1"].push(room) } else { - dayDictionary["-1"] = [room] + adminSettings.dayDictionary["-1"] = [room] } + writeAdminSettings(adminSettings); home.emit_new_queue_data(); respond_success(req, res, `Location added successfully`); } @@ -811,7 +817,7 @@ exports.post_update_locations = function (req, res) { return; } - var newDayDictionary = dayDictionary + var newDayDictionary = adminSettings.dayDictionary for (var day in daysOfWeek) { if (days.includes(daysOfWeek[day])) { // day is selected for room @@ -834,16 +840,17 @@ exports.post_update_locations = function (req, res) { } } } - dayDictionary = newDayDictionary + adminSettings.dayDictionary = newDayDictionary + writeAdminSettings(adminSettings); home.emit_new_queue_data(); respond_success(req, res, `Location changed successfully`); } exports.internal_get_locations = function () { return { - dayDictionary: dayDictionary, - roomDictionary: dayToRoomDictionary(dayDictionary) + dayDictionary: adminSettings.dayDictionary, + roomDictionary: dayToRoomDictionary(adminSettings.dayDictionary) } } @@ -863,25 +870,26 @@ exports.remove_location = function (req, res) { for (dayIdx in days) { if (dayIdx && dayIdx != null) { let dayInt = days[dayIdx] - if (!dayDictionary[dayInt].includes(room)) { + if (!adminSettings.dayDictionary[dayInt].includes(room)) { console.log("hmm shouldn't really have a day selected for this room") } else { - let roomArrForDay = dayDictionary[dayInt] + let roomArrForDay = adminSettings.dayDictionary[dayInt] // safe because room is in roomArrForDay so idx >= 0 roomArrForDay.splice(roomArrForDay.indexOf(room), 1) - dayDictionary[dayInt] = roomArrForDay + adminSettings.dayDictionary[dayInt] = roomArrForDay } } } // REMOVE ROOM FROM -1!! - let emptyRoomArr = dayDictionary["-1"] + let emptyRoomArr = adminSettings.dayDictionary["-1"] let idx = emptyRoomArr.indexOf(room) if (idx >= 0) { emptyRoomArr.splice(idx, 1) } - dayDictionary["-1"] = emptyRoomArr + adminSettings.dayDictionary["-1"] = emptyRoomArr + writeAdminSettings(adminSettings); home.emit_new_queue_data(); respond_success(req, res, `Location removed successfully`); } From 60a0ddfc1ddfa105ed458d0257f15e4e9215fa9a Mon Sep 17 00:00:00 2001 From: Jackson Romero Date: Tue, 9 Jan 2024 21:44:54 -0500 Subject: [PATCH 5/5] Change default semester to S23 because of legacy reasons lol --- server/controllers/settings.js | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/server/controllers/settings.js b/server/controllers/settings.js index 9cd700c..180e207 100644 --- a/server/controllers/settings.js +++ b/server/controllers/settings.js @@ -13,7 +13,7 @@ const home = require('./home'); // In production, these should be cleared var fs = require('fs'); let adminSettings = { - currSem: "F23", + currSem: "S23", slackURL: null, questionsURL: '', rejoinTime: 15, @@ -35,7 +35,7 @@ exports.get_admin_settings = function () { } else { console.log('No admin settings found'); adminSettings = { - currSem: "F23", + currSem: "S23", slackURL: null, questionsURL: '', rejoinTime: 15,