Skip to content

Commit

Permalink
Merge pull request #162 from Carifio24/more-waiting-room-override
Browse files Browse the repository at this point in the history
More waiting room override functionality
  • Loading branch information
Carifio24 authored Nov 20, 2024
2 parents 2d42c70 + 485f3db commit 6605598
Show file tree
Hide file tree
Showing 2 changed files with 74 additions and 8 deletions.
42 changes: 38 additions & 4 deletions src/stories/hubbles_law/database.ts
Original file line number Diff line number Diff line change
Expand Up @@ -849,22 +849,56 @@ export async function addClassToMergeGroup(classID: number): Promise<number | nu

}

export async function removeClassFromMergeGroup(classID: number): Promise<number> {
return HubbleClassMergeGroup.destroy({
where: {
class_id: classID,
}
});
}

export async function getWaitingRoomOverride(classID: number): Promise<HubbleWaitingRoomOverride | null> {
return HubbleWaitingRoomOverride.findOne({
where: {
class_id: classID,
}
});
}

export async function setWaitingRoomOverride(classID: number): Promise<boolean | Error> {
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<boolean> {
export async function removeWaitingRoomOverride(classID: number): Promise<number> {
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);
}
40 changes: 36 additions & 4 deletions src/stories/hubbles_law/router.ts
Original file line number Diff line number Diff line change
Expand Up @@ -40,7 +40,8 @@ import {
getMergedIDsForClass,
addClassToMergeGroup,
setWaitingRoomOverride,
removeWaitingRoomOverride
removeWaitingRoomOverride,
getWaitingRoomOverride
} from "./database";

import {
Expand Down Expand Up @@ -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);
Expand Down Expand Up @@ -623,22 +649,28 @@ 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}`,
});
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,
});
});

Expand Down

0 comments on commit 6605598

Please sign in to comment.