diff --git a/src/entrypoints/sig.content/index.ts b/src/entrypoints/sig.content/index.ts index e584906..7863e48 100644 --- a/src/entrypoints/sig.content/index.ts +++ b/src/entrypoints/sig.content/index.ts @@ -1,8 +1,9 @@ import { storage } from "wxt/storage"; -import { scrapeMenu } from "@/scripts/sig/homepage"; +import { scrapeMenu, type Student } from "@/scripts/sig/homepage"; import { successToast } from "@/utils/toasts"; import "toastify-js/src/toastify.css"; import '@/assets/tailwind.css' +import { createStudent } from "@/services/next"; export default defineContentScript({ async main() { @@ -19,10 +20,25 @@ export default defineContentScript({ if (shouldFormatItinerary) { // fix here the way a receive the curriculum year, maybe asking for the user // is the best use case - const student = await scrapeMenu($trs); + const student = await scrapeMenu($trs) as NonNullable; + storage.setItem("sync:student", student); + // this will be acessed in the ufabc matriculas, to be filtered. + storage.setItem('session:studied', student?.graduation.components) + // create the student for next - update code to handle the same ra in BCT and BCC + // it should increment the graduation with the BCC data. console.log(student) - // storage.setItem("sync:student", student); - // storage.setItem('session:studied', student?.graduation.components) + // await createStudent({ + // ra: student.ra, + // components: student.graduation.components, + // // grade: student.graduation.curriculumYear, + // // graduation data + // "mandatory_credits_number": 90, + // "limited_credits_number": 57, + // "free_credits_number": 43, + // "credits_total": 190 + // }) + + successToast.showToast(); } diff --git a/src/scripts/sig/homepage.ts b/src/scripts/sig/homepage.ts index ad38532..1f7f4d6 100644 --- a/src/scripts/sig/homepage.ts +++ b/src/scripts/sig/homepage.ts @@ -9,7 +9,7 @@ import { } from "@/services/ufabc-parser"; import { transformCourseName, type Course } from "@/utils/transformCourse"; import { capitalizeStr } from "@/utils/capitalizeStr"; -import { getPaginatedSubjects } from "@/services/next"; +import { findSubjectByName, getPaginatedSubjects, type PaginatedSubjects } from "@/services/next"; type SigStudent = { matricula: string; @@ -158,8 +158,9 @@ export async function scrapeMenu( curriculumByRa?.grade, ); - - const hydrateSigComponentsPromises = graduationHistory.map(c => hydrateSigComponent(c)) + const subjects = await getPaginatedSubjects(); + console.log(subjects) + const hydrateSigComponentsPromises = graduationHistory.map(c => hydrateSigComponent(c, subjects)) const hydrateSigComponents = await Promise.all(hydrateSigComponentsPromises) const components = hydrateSigComponents.map((component) => hydrateComponents(component, curriculumComponents.components), @@ -281,13 +282,19 @@ function hydrateComponents( }; } -async function hydrateSigComponent(sigComponent: SigComponent) { - const subjects = await getPaginatedSubjects(true); - const match = subjects.data.find( - subject => normalizeDiacritics(subject.name.toLowerCase()) === normalizeDiacritics(sigComponent.name.toLowerCase()) - ); +async function hydrateSigComponent(sigComponent: SigComponent, subjects: PaginatedSubjects) { + + const match = subjects.data.find(s => s.name.toLowerCase() === sigComponent.name.toLowerCase()) + + if(!match) { + return { + ...sigComponent, + credits: 0, + } + } + return { ...sigComponent, - credits: match?.credits ?? 0, // Default to 0 if no match found + credits: match.credits, }; } diff --git a/src/services/next.ts b/src/services/next.ts index 8699fe7..21f8753 100644 --- a/src/services/next.ts +++ b/src/services/next.ts @@ -1,6 +1,7 @@ +import type { Student } from "@/scripts/sig/homepage"; import { ofetch } from "ofetch"; -type PaginatedSubjects = { +export type PaginatedSubjects = { total: number; pages: number data: { @@ -9,6 +10,8 @@ type PaginatedSubjects = { }[] } +const SUBJECTS_CACHE_KEY = 'next:subjects' + function resolveEndpoint() { if (import.meta.env.PROD) { return 'https://api.v2.ufabcnext.com' @@ -22,35 +25,32 @@ export const nextService = ofetch.create({ baseURL: resolveEndpoint(), }) -export async function getPaginatedSubjects(fetchAll = false) { - const ITEMS_PER_PAGE = 200; - const firstPage = await nextService("/entities/subjects", { +export async function getPaginatedSubjects(page = 1, limit = 2_000) { + const cachedSubjects = await storage.getItem( + `session:${SUBJECTS_CACHE_KEY}`, + ); + + if (cachedSubjects) { + return cachedSubjects; + } + + const paginatedSubjects = await nextService("/entities/subjects", { params: { - page: 1, - limit: ITEMS_PER_PAGE, + page: page, + limit: limit, }, }); - if (!fetchAll || firstPage.pages <= 1) { - return firstPage; - } - const remainingPages = Array.from({ length: firstPage.pages - 1 }, (_, i) => - nextService("/entities/subjects", { - params: { - page: i + 2, // Start from page 2 - limit: ITEMS_PER_PAGE, - }, - }) - ); - - const additionalPages = await Promise.all(remainingPages); - - const allData = { - total: firstPage.total, - pages: firstPage.pages, - data: [...firstPage.data, ...additionalPages.flatMap((page) => page.data)], - }; - - return allData; + storage.setItem(`session:${SUBJECTS_CACHE_KEY}`, paginatedSubjects) + return paginatedSubjects +} + + +export async function createStudent(student: Student) { + const createdStudent = await nextService('/entities/student', { + method: 'POST', + body: student, + }) + return createdStudent; }