Skip to content

Commit

Permalink
feat: Live enrollment count
Browse files Browse the repository at this point in the history
  • Loading branch information
mathhulk committed Oct 17, 2024
1 parent 6d2d30d commit 84d5dcc
Show file tree
Hide file tree
Showing 7 changed files with 105 additions and 1 deletion.
1 change: 1 addition & 0 deletions apps/backend/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -38,6 +38,7 @@
"@graphql-tools/utils": "^10.3.2",
"@keyv/redis": "^3.0.1",
"@repo/common": "*",
"@repo/sis-api": "*",
"compression": "^1.7.4",
"connect-redis": "^7.1.1",
"cors": "^2.8.5",
Expand Down
48 changes: 48 additions & 0 deletions apps/backend/src/modules/enrollment/controller.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,48 @@
import { SectionModel, SectionType, TermModel } from "@repo/common";
import { ClassesAPI } from "@repo/sis-api/classes";

import { config } from "../../config";
import { Semester } from "../../generated-types/graphql";
import { formatSection } from "../class/formatter";

export const getEnrollment = async (
year: number,
semester: Semester,
subject: string,
courseNumber: string,
number: string
) => {
const term = await TermModel.findOne({
name: `${year} ${semester}`,
});

if (!term) throw new Error("Term not found");

const section = await SectionModel.findOne({
"class.session.term.id": term.id,
"class.course.subjectArea.code": subject,
"class.course.catalogNumber.formatted": courseNumber,
number: number,
});

if (!section) throw new Error("Section not found");

const client = new ClassesAPI();

const response = await client.v1.getClassSectionByTermAndSectionIdUsingGet(
section.id,
{ "term-id": term.id },
{
headers: {
app_key: config.sis.CLASS_APP_KEY,
app_id: config.sis.CLASS_APP_ID,
},
}
);

const raw = response.data.apiResponse?.response.classSections?.[0];

if (!raw) throw new Error("Something went error");

return formatSection(raw as unknown as SectionType);
};
7 changes: 7 additions & 0 deletions apps/backend/src/modules/enrollment/index.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
import resolver from "./resolver";
import typeDef from "./typedefs/enrollment";

export default {
resolver,
typeDef,
};
23 changes: 23 additions & 0 deletions apps/backend/src/modules/enrollment/resolver.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,23 @@
import { getEnrollment } from "./controller";
import { EnrollmentModule } from "./generated-types/module-types";

const resolvers: EnrollmentModule.Resolvers = {
Query: {
enrollment: async (
_,
{ year, semester, subject, courseNumber, number }
) => {
const enrollment = await getEnrollment(
year,
semester,
subject,
courseNumber,
number
);

return enrollment as unknown as EnrollmentModule.Section;
},
},
};

export default resolvers;
13 changes: 13 additions & 0 deletions apps/backend/src/modules/enrollment/typedefs/enrollment.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,13 @@
import { gql } from "graphql-tag";

export default gql`
type Query {
enrollment(
year: Int!
semester: Semester!
subject: String!
courseNumber: String!
number: String!
): Section!
}
`;
13 changes: 12 additions & 1 deletion apps/backend/src/modules/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -4,12 +4,23 @@ import Catalog from "./catalog";
import Class from "./class";
import Common from "./common";
import Course from "./course";
import Enrollment from "./enrollment";
import Grade from "./grade";
import Schedule from "./schedule";
import Term from "./term";
import User from "./user";

const modules = [User, Grade, Catalog, Common, Schedule, Term, Course, Class];
const modules = [
User,
Grade,
Catalog,
Common,
Schedule,
Term,
Course,
Class,
Enrollment,
];

export const resolvers = merge(modules.map((module) => module.resolver));

Expand Down
1 change: 1 addition & 0 deletions package-lock.json

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

0 comments on commit 84d5dcc

Please sign in to comment.