Skip to content

Commit

Permalink
feat: sync student history
Browse files Browse the repository at this point in the history
  • Loading branch information
Joabesv committed Nov 22, 2024
1 parent 60517b7 commit f524d5f
Show file tree
Hide file tree
Showing 3 changed files with 48 additions and 5 deletions.
25 changes: 22 additions & 3 deletions src/entrypoints/sig.content/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -2,8 +2,7 @@ import { storage } from "wxt/storage";
import { scrapeMenu, type Student } from "@/scripts/sig/homepage";
import "toastify-js/src/toastify.css";
import '@/assets/tailwind.css'
import { createStudent } from "@/services/next";
import { calculateCoefficients } from "@/utils/calculateCoefficients";
import { createStudent, syncHistory, type SigHistory } from "@/services/next";

export default defineContentScript({
async main() {
Expand All @@ -23,7 +22,6 @@ export default defineContentScript({
}

const existingStudent = await storage.getItem<Student>("local:student");

if (existingStudent?.ra === currentStudent.ra) {
// Check if this graduation already exists
const currentGraduation = currentStudent.graduations[0];
Expand All @@ -38,11 +36,32 @@ export default defineContentScript({
lastUpdate: Date.now()
};

// update with new course
await syncHistory({
ra: existingStudent.ra,
course: currentGraduation.course as string,
grade: currentGraduation.grade,
components: currentGraduation.components
})

await storage.setItem("local:student", mergedStudent);
}
// update regular student - not new and same course
await syncHistory({
ra: existingStudent.ra,
course: currentGraduation.course as string,
grade: currentGraduation.grade,
components: currentGraduation.components
})
} else {
// Create student record with first graduation
await storage.setItem("local:student", currentStudent);
await syncHistory({
ra: currentStudent.ra,
course: existingStudent?.graduations[0].course as string,
grade: existingStudent?.graduations[0].grade as string,
components: existingStudent?.graduations[0].components as SigHistory['components']
})
}

successToast.showToast();
Expand Down
4 changes: 3 additions & 1 deletion src/scripts/sig/homepage.ts
Original file line number Diff line number Diff line change
Expand Up @@ -37,7 +37,7 @@ type ShallowStudent = {
type SigComponent = {
UFCode: string;
name: string;
grade: string;
grade: "A" | "B" | "C" | "D" | "O" | "F" | "E" | null;
status: string;
year: string;
period: "1" | "2" | "3";
Expand All @@ -58,6 +58,7 @@ export type Student = {
course: Course;
campus: string;
shift: string;
grade: string;
components: HydratedComponent[];
}>;
startedAt: string;
Expand Down Expand Up @@ -168,6 +169,7 @@ export async function scrapeMenu(
const graduation = {
course: currentGraduation.course,
campus: currentGraduation.campus,
grade: curriculumByRa.appliedAt,
shift: currentGraduation.shift,
components,
};
Expand Down
24 changes: 23 additions & 1 deletion src/services/next.ts
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,21 @@ export type PaginatedSubjects = {
}[]
}

const SUBJECTS_CACHE_KEY = 'next:subjects'
export type SigHistory = {
ra: string;
grade: string;
course: string;
components: {
grade: "A" | "B" | "C" | "D" | "O" | "F" | "E" | null;
name: string;
status: string | null;
year: string;
period: "1" | "2" | "3";
UFCode: string;
category: "mandatory" | "free" | "limited";
credits: number;
}[];
}

function resolveEndpoint() {
if (import.meta.env.PROD) {
Expand Down Expand Up @@ -45,3 +59,11 @@ export async function createStudent(student: Student) {
})
return createdStudent;
}

export async function syncHistory(student: SigHistory) {
const syncedStudent = await nextService('/history', {
method: 'POST',
body: student
})
return syncedStudent
}

0 comments on commit f524d5f

Please sign in to comment.