diff --git a/src/stories/hubbles_law/database.ts b/src/stories/hubbles_law/database.ts index 65fad45..493af39 100644 --- a/src/stories/hubbles_law/database.ts +++ b/src/stories/hubbles_law/database.ts @@ -849,22 +849,56 @@ export async function addClassToMergeGroup(classID: number): Promise { + return HubbleClassMergeGroup.destroy({ + where: { + class_id: classID, + } + }); +} + +export async function getWaitingRoomOverride(classID: number): Promise { + return HubbleWaitingRoomOverride.findOne({ + where: { + class_id: classID, + } + }); +} + export async function setWaitingRoomOverride(classID: number): Promise { - return HubbleWaitingRoomOverride.findOrCreate({ + let successOrError = await HubbleWaitingRoomOverride.findOrCreate({ where: { class_id: classID, } }) .then(result => result[1]) .catch((error: Error) => error); + + // Once we've set the override, that means that we need to add this class to a merge group + const mergeGroup = await addClassToMergeGroup(classID); + if (mergeGroup === null) { + successOrError = new Error(`Error adding class ${classID} to a merge group`); + } + + return successOrError; } -export async function removeWaitingRoomOverride(classID: number): Promise { +export async function removeWaitingRoomOverride(classID: number): Promise { return HubbleWaitingRoomOverride.destroy({ where: { class_id: classID, } }) - .then(_result => true) - .catch(_error => false); + .then(async (count) => { + const cls = await findClassById(classID); + + // This condition should always be satisfied, since we should only be doing overrides + // for non-small classes anyways (if the class is small, there shouldn't be any need + // to want an override to begin with) + if (cls !== null && !cls.small_class) { + await removeClassFromMergeGroup(classID); + } + return count; + }) + .catch(_error => NaN); } diff --git a/src/stories/hubbles_law/router.ts b/src/stories/hubbles_law/router.ts index 01db3ba..46b77ae 100644 --- a/src/stories/hubbles_law/router.ts +++ b/src/stories/hubbles_law/router.ts @@ -40,7 +40,8 @@ import { getMergedIDsForClass, addClassToMergeGroup, setWaitingRoomOverride, - removeWaitingRoomOverride + removeWaitingRoomOverride, + getWaitingRoomOverride } from "./database"; import { @@ -559,6 +560,31 @@ const WaitingRoomOverrideSchema = S.struct({ class_id: S.number.pipe(S.int()), }); +router.get("/waiting-room-override/:classID", async (req, res) => { + const classID = Number(req.params.classID); + const cls = await findClassById(classID); + if (cls === null) { + res.status(404).json({ + error: `No class found with ID ${classID}`, + }); + return; + } + + getWaitingRoomOverride(classID) + .then(override => { + res.json({ + class_id: classID, + override_status: override !== null, + }); + }) + .catch(_error => { + res.status(500).json({ + error: `Error determining waiting room override status for class with ID ${classID}`, + }); + }); + +}); + router.put("/waiting-room-override", async (req, res) => { const body = req.body; const maybe = S.decodeUnknownEither(WaitingRoomOverrideSchema)(body); @@ -623,12 +649,14 @@ router.delete("/waiting-room-override", async (req, res) => { } const right = maybe.right; - const success = await removeWaitingRoomOverride(right.class_id); + const countRemoved = await removeWaitingRoomOverride(right.class_id); + const success = !isNaN(countRemoved); const responseData = { success, + removed: success && countRemoved > 0, class_id: right.class_id, }; - if (!success) { + if (isNaN(countRemoved)) { res.status(500).json({ ...responseData, error: `An error occurred while removing the waiting room override for class ${right.class_id}`, @@ -636,9 +664,13 @@ router.delete("/waiting-room-override", async (req, res) => { return; } + const message = countRemoved > 0 ? + `The waiting room override for class ${right.class_id} was removed` : + `No waiting room override for class ${right.class_id} existed`; + res.json({ ...responseData, - message: `The waiting room override for class ${right.class_id} was removed, if one existed.`, + message, }); });