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

Dashboard class groups #93

Merged
merged 3 commits into from
Oct 18, 2023
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
14 changes: 13 additions & 1 deletion src/database.ts
Original file line number Diff line number Diff line change
Expand Up @@ -9,13 +9,15 @@ import {
Story,
StudentsClasses,
Student,
DummyClass
DummyClass,
DashboardClassGroup,
} from "./models";

import {
createClassCode,
createVerificationCode,
encryptPassword,
isNumberArray,
} from "./utils";


Expand Down Expand Up @@ -661,3 +663,13 @@ export async function getQuestionsForStory(storyName: string, newestOnly=true):
type: QueryTypes.SELECT
});
}


export async function getDashboardGroupClasses(code: string): Promise<number[] | null> {
const group = await DashboardClassGroup.findOne({ where: { code } });
if (group === null) {
return null;
}
const classIDs = group.class_ids;
return isNumberArray(classIDs) ? classIDs : [];
}
36 changes: 36 additions & 0 deletions src/models/dashboard_class_group.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,36 @@
import { Sequelize, DataTypes, Model, InferAttributes, InferCreationAttributes, CreationOptional, DATE } from "sequelize";

export class DashboardClassGroup extends Model<InferAttributes<DashboardClassGroup>, InferCreationAttributes<DashboardClassGroup>> {
declare id: CreationOptional<number>;
declare name: string;
declare code: string;
declare class_ids: number[];
}

export function initializeDashboardClassGroupModel(sequelize: Sequelize) {
DashboardClassGroup.init({
id: {
type: DataTypes.INTEGER.UNSIGNED,
allowNull: false,
autoIncrement: true,
primaryKey: true
},
name: {
type: DataTypes.STRING,
allowNull: false,
unique: true
},
code: {
type: DataTypes.STRING,
allowNull: false,
unique: true
},
class_ids: {
type: DataTypes.JSON,
allowNull: false
}
}, {
sequelize,
engine: "InnoDB"
});
}
3 changes: 3 additions & 0 deletions src/models/index.ts
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
import { Class, initializeClassModel } from "./class";
import { DashboardClassGroup, initializeDashboardClassGroupModel } from "./dashboard_class_group";
import { DummyClass, initializeDummyClassModel } from "./dummy_class";
import { Educator, initializeEducatorModel } from "./educator";
import { IgnoreStudent, initializeIgnoreStudentModel } from "./ignore_student";
Expand All @@ -17,6 +18,7 @@ export {
Class,
ClassStories,
CosmicDSSession,
DashboardClassGroup,
DummyClass,
Educator,
IgnoreStudent,
Expand All @@ -39,4 +41,5 @@ export function initializeModels(db: Sequelize) {
initializeStudentOptionsModel(db);
initializeIgnoreStudentModel(db);
initializeQuestionModel(db);
initializeDashboardClassGroupModel(db);
}
15 changes: 15 additions & 0 deletions src/server.ts
Original file line number Diff line number Diff line change
Expand Up @@ -28,6 +28,7 @@ import {
addQuestion,
currentVersionForQuestion,
getQuestionsForStory,
getDashboardGroupClasses,
} from "./database";

import { getAPIKey, hasPermission } from "./authorization";
Expand Down Expand Up @@ -652,3 +653,17 @@ app.put("/options/:studentID", async (req, res) => {
}
res.json(updatedOptions);
});

app.get("/dashboard-group-classes/:code", async (req, res) => {
const classIDs = await getDashboardGroupClasses(req.params.code);
if (classIDs === null) {
res.statusCode = 404;
res.json({
error: `Could not find a dashboard group for code ${req.params.code}`
});
} else {
res.json({
class_ids: classIDs
});
}
});
8 changes: 8 additions & 0 deletions src/sql/create_dashboard_class_group_table.sql
Original file line number Diff line number Diff line change
@@ -0,0 +1,8 @@
CREATE TABLE DashboardClassGroups (
id int(11) UNSIGNED NOT NULL UNIQUE AUTO_INCREMENT,
name varchar(50) UNIQUE NOT NULL,
code varchar(36) UNIQUE NOT NULL,
class_ids JSON NOT NULL,

PRIMARY KEY(id)
) ENGINE=InnoDB AUTO_INCREMENT=0 DEFAULT CHARSET=utf8 COLLATE=utf8_unicode_ci PACK_KEYS=0;