Skip to content

Commit

Permalink
Merge pull request #251 from cmu15122/alexb/persistentsettings
Browse files Browse the repository at this point in the history
Store / load admin settings from a file
  • Loading branch information
jacksontromero authored Jan 10, 2024
2 parents b39f1d0 + 60a0ddf commit ae85617
Show file tree
Hide file tree
Showing 3 changed files with 84 additions and 37 deletions.
1 change: 1 addition & 0 deletions .gitignore
Original file line number Diff line number Diff line change
Expand Up @@ -117,3 +117,4 @@ dist

# Config Files
config.json
adminSettings.json
17 changes: 11 additions & 6 deletions client/src/components/settings/admin/QueueRejoinSettings.tsx
Original file line number Diff line number Diff line change
@@ -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';
Expand All @@ -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 (
<BaseCard>
<CardContent>
Expand All @@ -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'}}}
/>
Expand Down
103 changes: 72 additions & 31 deletions server/controllers/settings.js
Original file line number Diff line number Diff line change
Expand Up @@ -11,14 +11,38 @@ 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",
slackURL: null,
questionsURL: '',
rejoinTime: 10
rejoinTime: 15,
dayDictionary: {}
};
// 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: "S23",
slackURL: null,
questionsURL: '',
rejoinTime: 15,
dayDictionary: {}
};
}

return adminSettings;
}

Expand Down Expand Up @@ -50,6 +74,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) {
Expand Down Expand Up @@ -178,6 +209,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";
Expand All @@ -200,6 +232,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`);
}
Expand All @@ -219,6 +252,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`);
}
Expand All @@ -238,6 +272,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`);
}
Expand Down Expand Up @@ -714,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) {
Expand All @@ -754,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`);
}
Expand All @@ -778,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
Expand All @@ -801,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)
}
}

Expand All @@ -830,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`);
}

0 comments on commit ae85617

Please sign in to comment.