Skip to content

Commit

Permalink
Merge pull request #93 from Carifio24/dashboard-class-groups
Browse files Browse the repository at this point in the history
Dashboard class groups
  • Loading branch information
Carifio24 authored Oct 18, 2023
2 parents 33fb0f4 + f5e0a38 commit e15e63b
Show file tree
Hide file tree
Showing 5 changed files with 75 additions and 1 deletion.
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;

0 comments on commit e15e63b

Please sign in to comment.