Skip to content

Commit

Permalink
feat: create student
Browse files Browse the repository at this point in the history
  • Loading branch information
Joabesv committed Dec 12, 2024
1 parent 51bd1a1 commit 5406754
Show file tree
Hide file tree
Showing 4 changed files with 81 additions and 25 deletions.
60 changes: 37 additions & 23 deletions src/entrypoints/sig.content/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +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, syncHistory, type SigHistory } from "@/services/next";
import { createStudent, syncHistory } from "@/services/next";
import { processingToast, errorToast, successToast } from '@/utils/toasts'

export default defineContentScript({
Expand Down Expand Up @@ -46,23 +46,55 @@ export default defineContentScript({
})

await storage.setItem("local:student", mergedStudent);
await createStudent(mergedStudent)
const studentToCreateGraduations = mergedStudent.graduations.map(g => ({
name: g.course as string,
courseId: g.UFCourseId,
turno: g.shift,
quads: calculateQuadrimestres({ entranceQuad: mergedStudent.startedAt }),
}))
await createStudent({
login: mergedStudent.login,
ra: mergedStudent.ra,
graduations: studentToCreateGraduations
})
}
const studentToCreateGraduations = existingStudent.graduations.map(g => ({
name: g.course as string,
courseId: g.UFCourseId,
turno: g.shift,
quads: calculateQuadrimestres({ entranceQuad: existingStudent.startedAt }),
}))
// update regular student - not new and same course
await syncHistory({
ra: existingStudent.ra,
course: currentGraduation.course as string,
grade: currentGraduation.grade,
components: currentGraduation.components
})
await createStudent({
login: existingStudent.login,
ra: existingStudent.ra,
graduations: studentToCreateGraduations
})
} else {
await storage.setItem("local:student", currentStudent);
// Create student record with first graduation
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']
course: currentStudent?.graduations[0].course as string,
grade: currentStudent?.graduations[0].grade,
components: currentStudent?.graduations[0].components
})
const studentToCreateGraduations = currentStudent.graduations.map(g => ({
name: g.course as string,
courseId: g.UFCourseId,
turno: g.shift,
quads: calculateQuadrimestres({ entranceQuad: currentStudent.startedAt }) ?? 0,
}))
await createStudent({
login: currentStudent.login,
ra: currentStudent.ra,
graduations: studentToCreateGraduations
})
}

Expand All @@ -78,21 +110,3 @@ export default defineContentScript({
runAt: "document_end",
matches: ["https://sig.ufabc.edu.br/sigaa/portais/discente/discente.jsf"],
});


// fix here the way a receive the curriculum year, maybe asking for the user
// is the best use case
// this will be accessed in the ufabc matriculas, to be filtered.
// 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.
// 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
// })

2 changes: 2 additions & 0 deletions src/scripts/sig/homepage.ts
Original file line number Diff line number Diff line change
Expand Up @@ -60,6 +60,7 @@ export type Student = {
campus: string;
shift: string;
grade: string;
UFCourseId: number;
components: HydratedComponent[];
}>;
startedAt: string;
Expand Down Expand Up @@ -182,6 +183,7 @@ export async function scrapeMenu(
campus: currentGraduation.campus,
grade: curriculumByRa.grade,
shift: currentGraduation.shift,
UFCourseId: UFCourseIdList[0],
components,
};

Expand Down
18 changes: 16 additions & 2 deletions src/services/next.ts
Original file line number Diff line number Diff line change
Expand Up @@ -143,6 +143,20 @@ export type MatriculaStudent = {
}[];
}

type CreateStudent = {
ra: string;
login: string;
graduations: {
name: string;
courseId: number;
cp?: number | undefined;
cr?: number | undefined;
quads?: number | undefined;
turno: string;
}[];
studentId?: number | undefined;
}

function resolveEndpoint() {
if (import.meta.env.PROD) {
return "https://api.v2.ufabcnext.com";
Expand All @@ -161,8 +175,8 @@ export async function getStudentHistory(ra: number) {
return studentHistory;
}

export async function createStudent(student: Student) {
const createdStudent = await nextService("/entities/student", {
export async function createStudent(student: CreateStudent) {
const createdStudent = await nextService("/entities/students", {
method: "POST",
body: student,
});
Expand Down
26 changes: 26 additions & 0 deletions src/utils/season.ts
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,12 @@ type Season = {
year: number;
};

type QuadrimestreCalculationParams = {
entranceQuad: string;
trancamentos?: string[]; // List of quadrimestres with trancamento
}


export function findQuadFromDate(month: number): number {
if ([0, 1, 2, 10, 11].includes(month)) {
return 1;
Expand Down Expand Up @@ -32,6 +38,26 @@ export function findSeasonKey(date: dayjs.Dayjs = dayjs()): string {
return `${year}:${quad}`;
}

export function calculateQuadrimestres({
entranceQuad,
trancamentos = []
}: QuadrimestreCalculationParams) {
// Parse entrance quad
const [year, quad] = entranceQuad.split('.').map(Number);

// Get current season
const currentSeason = findSeason();
console.log(currentSeason, entranceQuad)
// Calculate total quadrimestres
const totalQuadrimestres = (currentSeason.year - year) * 3 + (currentSeason.quad - quad) + 1;

// Subtract trancamentos
const effectiveQuadrimestres = totalQuadrimestres - trancamentos.length;

// Ensure non-negative result
return Math.max(0, effectiveQuadrimestres);
}

export function findIdeais(date: dayjs.Dayjs = dayjs()): string[] {
const quad = findQuadFromDate(date.month());
const ideais: Record<number,string[]> = {
Expand Down

0 comments on commit 5406754

Please sign in to comment.