Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

More waiting room override functionality #162

Merged
merged 2 commits into from
Nov 20, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
42 changes: 38 additions & 4 deletions src/stories/hubbles_law/database.ts
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
import { Attributes, FindOptions, Op, QueryTypes, Sequelize, WhereAttributeHash, WhereOptions, col, fn, literal } from "sequelize";
import { AsyncMergedHubbleStudentClasses, Galaxy, HubbleMeasurement, HubbleWaitingRoomOverride, SampleHubbleMeasurement, SyncMergedHubbleClasses } from "./models";
import { classSize, findClassById, findStudentById } from "../../database";

Check warning on line 3 in src/stories/hubbles_law/database.ts

View workflow job for this annotation

GitHub Actions / build

'classSize' is defined but never used. Allowed unused vars must match /^_/u
import { RemoveHubbleMeasurementResult, SubmitHubbleMeasurementResult } from "./request_results";
import { Class, StoryState, Student, StudentsClasses } from "../../models";
import { HubbleStudentData } from "./models/hubble_student_data";
Expand Down Expand Up @@ -849,22 +849,56 @@

}

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
Loading