Skip to content

Commit

Permalink
Merge pull request #161 from Carifio24/hubble-s5-overrides
Browse files Browse the repository at this point in the history
Add waiting room overrides for Hubble story
  • Loading branch information
Carifio24 authored Nov 19, 2024
2 parents 264693c + 523d45c commit 2d42c70
Show file tree
Hide file tree
Showing 5 changed files with 156 additions and 4 deletions.
22 changes: 21 additions & 1 deletion src/stories/hubbles_law/database.ts
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
import { Attributes, FindOptions, Op, QueryTypes, Sequelize, WhereAttributeHash, WhereOptions, col, fn, literal } from "sequelize";
import { AsyncMergedHubbleStudentClasses, Galaxy, HubbleMeasurement, SampleHubbleMeasurement, SyncMergedHubbleClasses } from "./models";
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";
Expand Down Expand Up @@ -848,3 +848,23 @@ export async function addClassToMergeGroup(classID: number): Promise<number | nu
return mergeGroup.group_id;

}

export async function setWaitingRoomOverride(classID: number): Promise<boolean | Error> {
return HubbleWaitingRoomOverride.findOrCreate({
where: {
class_id: classID,
}
})
.then(result => result[1])
.catch((error: Error) => error);
}

export async function removeWaitingRoomOverride(classID: number): Promise<boolean> {
return HubbleWaitingRoomOverride.destroy({
where: {
class_id: classID,
}
})
.then(_result => true)
.catch(_error => false);
}
28 changes: 28 additions & 0 deletions src/stories/hubbles_law/models/hubble_waiting_room_override.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,28 @@
import { Class } from "../../../models";
import { Sequelize, DataTypes, Model, InferAttributes, InferCreationAttributes, CreationOptional } from "sequelize";

export class HubbleWaitingRoomOverride extends Model<InferAttributes<HubbleWaitingRoomOverride>, InferCreationAttributes<HubbleWaitingRoomOverride>> {
declare class_id: number;
declare timestamp: CreationOptional<Date>;
}

export function initializeHubbleWaitingRoomOverrideModel(sequelize: Sequelize) {
HubbleWaitingRoomOverride.init({
class_id: {
type: DataTypes.INTEGER.UNSIGNED,
allowNull: false,
primaryKey: true,
references: {
model: Class,
key: "id",
}
},
timestamp: {
type: DataTypes.DATE,
allowNull: false,
defaultValue: Sequelize.literal("CURRENT_TIMESTAMP"),
}
}, {
sequelize,
});
}
5 changes: 4 additions & 1 deletion src/stories/hubbles_law/models/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -7,13 +7,15 @@ import { Sequelize } from "sequelize";
import { initializeHubbleStudentDataModel } from "./hubble_student_data";
import { initializeHubbleClassDataModel } from "./hubble_class_data";
import { initializeHubbleClassMergeGroupModel } from "./hubble_class_merge_group";
import { initializeHubbleWaitingRoomOverrideModel, HubbleWaitingRoomOverride } from "./hubble_waiting_room_override";

export {
Galaxy,
HubbleMeasurement,
SampleHubbleMeasurement,
AsyncMergedHubbleStudentClasses,
SyncMergedHubbleClasses
SyncMergedHubbleClasses,
HubbleWaitingRoomOverride
};

export function initializeModels(db: Sequelize) {
Expand All @@ -25,4 +27,5 @@ export function initializeModels(db: Sequelize) {
initializeHubbleStudentDataModel(db);
initializeHubbleClassDataModel(db);
initializeHubbleClassMergeGroupModel(db);
initializeHubbleWaitingRoomOverrideModel(db);
}
94 changes: 92 additions & 2 deletions src/stories/hubbles_law/router.ts
Original file line number Diff line number Diff line change
Expand Up @@ -38,7 +38,9 @@ import {
getClassMeasurementCount,
getStudentsWithCompleteMeasurementsCount,
getMergedIDsForClass,
addClassToMergeGroup
addClassToMergeGroup,
setWaitingRoomOverride,
removeWaitingRoomOverride
} from "./database";

import {
Expand All @@ -47,7 +49,7 @@ import {
} from "./request_results";

import { Express, Router } from "express";
import { Sequelize } from "sequelize";
import { Sequelize, ForeignKeyConstraintError, UniqueConstraintError } from "sequelize";
import { classForStudentStory, findClassById, findStudentById } from "../../database";
import { initializeModels } from "./models";
import { setUpHubbleAssociations } from "./associations";
Expand Down Expand Up @@ -553,6 +555,94 @@ router.get("/spectra/:type/:name", async (req, res) => {
});


const WaitingRoomOverrideSchema = S.struct({
class_id: S.number.pipe(S.int()),
});

router.put("/waiting-room-override", async (req, res) => {
const body = req.body;
const maybe = S.decodeUnknownEither(WaitingRoomOverrideSchema)(body);
if (Either.isLeft(maybe)) {
res.status(400).json({
error: "Invalid format. Request body should have the form { class_id: <integer> }",
});
return;
}

const right = maybe.right;
const result = await setWaitingRoomOverride(right.class_id);
const success = !(result instanceof Error);
const responseData = {
success,
class_id: right.class_id,
};
if (!success) {
if (result instanceof ForeignKeyConstraintError) {
res.status(404).json({
...responseData,
error: `No class found with ID ${right.class_id}`,
});

// It's fine if the override already exists
} else if (result instanceof UniqueConstraintError) {
res.status(200).json({
class_id: right.class_id,
success: true,
message: `The waiting room override for class ${right.class_id} was already set`,
});
} else {
res.status(500).json({
...responseData,
error: `An error occurred while setting the waiting room override for class ${right.class_id}`,
});
}
return;
}

if (result) {
res.status(201).json({
...responseData,
message: `Successfully set waiting room override for class ${right.class_id}`,
});
} else {
res.status(200).json({
message: `The waiting room override for class ${right.class_id} was already set`,
});
}
});


router.delete("/waiting-room-override", async (req, res) => {
const body = req.body;
const maybe = S.decodeUnknownEither(WaitingRoomOverrideSchema)(body);
if (Either.isLeft(maybe)) {
res.status(400).json({
error: "Invalid format. Request body should have the form { class_id: <integer> }",
});
return;
}

const right = maybe.right;
const success = await removeWaitingRoomOverride(right.class_id);
const responseData = {
success,
class_id: right.class_id,
};
if (!success) {
res.status(500).json({
...responseData,
error: `An error occurred while removing the waiting room override for class ${right.class_id}`,
});
return;
}

res.json({
...responseData,
message: `The waiting room override for class ${right.class_id} was removed, if one existed.`,
});
});


/** These endpoints are specifically for the spectrum-checking branch */

router.get("/unchecked-galaxies", async (_req, res) => {
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,11 @@
CREATE TABLE HubbleWaitingRoomOverrides (
class_id int(11) UNSIGNED NOT NULL,
timestamp TIMESTAMP NOT NULL DEFAULT CURRENT_TIMESTAMP
ON UPDATE CURRENT_TIMESTAMP,

PRIMARY KEY(class_id),
FOREIGN KEY(class_id)
REFERENCES Classes(id)
ON UPDATE CASCADE
ON DELETE CASCADE
) ENGINE=InnoDB AUTO_INCREMENT=0 DEFAULT CHARSET=utf8 COLLATE=utf8_unicode_ci PACK_KEYS=0;

0 comments on commit 2d42c70

Please sign in to comment.