Skip to content

Commit

Permalink
Include locations in saved admin settings
Browse files Browse the repository at this point in the history
  • Loading branch information
jacksontromero committed Dec 26, 2023
1 parent cb0b546 commit 4ebed9d
Showing 1 changed file with 41 additions and 33 deletions.
74 changes: 41 additions & 33 deletions server/controllers/settings.js
Original file line number Diff line number Diff line change
Expand Up @@ -16,15 +16,16 @@ 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')) {
var json = JSON.stringify(adminSettings)
fs.writeFile('../adminSettings.json', json, 'utf8', function () {
console.log('Created admin settings JSON');
});
}
}

exports.get_admin_settings = function () {

Expand All @@ -37,7 +38,8 @@ exports.get_admin_settings = function () {
currSem: "F23",
slackURL: null,
questionsURL: '',
rejoinTime: 15
rejoinTime: 15,
dayDictionary: {}
};
}

Expand Down Expand Up @@ -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) {
Expand All @@ -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`);
}
Expand All @@ -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
Expand All @@ -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)
}
}

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

0 comments on commit 4ebed9d

Please sign in to comment.