diff --git a/src/parent.ts b/src/parent.ts index 006570b..787027a 100644 --- a/src/parent.ts +++ b/src/parent.ts @@ -9,6 +9,8 @@ import { API_BASE_PARENT, BASE_URL } from "./consts"; export class ClasschartsParentClient extends ClasschartsClient { private password = ""; private email = ""; + // @ts-expect-error Init in .login + public pupils: GetPupilsResponse; /** * * @param email Parents email address @@ -55,13 +57,11 @@ export class ClasschartsParentClient extends ClasschartsClient { sessionID = JSON.parse( sessionID.substring(sessionID.indexOf("{"), sessionID.length) ); - this.sessionId = sessionID.session_id; - - const pupil = await this.getPupils(); - - this.studentId = pupil[0].id; - this.studentName = pupil[0].name; + this.pupils = await this.getPupils(); + if (!this.pupils) throw new Error("Account has no pupils attached"); + this.studentId = this.pupils[0].id; + this.studentName = this.pupils[0].name; } /** * Get Pupil details @@ -72,4 +72,21 @@ export class ClasschartsParentClient extends ClasschartsClient { method: "GET", }); } + /** + * + * @param pupilId Pupil ID obtained from this.pupils or getPupils + */ + async selectPupil(pupilId: number): Promise { + if (!pupilId) throw new Error("No pupil ID specified"); + const pupils = this.pupils; + for (let i = 0; i < pupils.length; i++) { + const pupil = pupils[i]; + if (pupil.id == pupilId) { + this.studentId = pupil.id; + this.studentName = pupil.name; + return; + } + } + throw new Error("No pupil with specified ID returned"); + } } diff --git a/src/types.ts b/src/types.ts index 409457e..dcd0ba0 100644 --- a/src/types.ts +++ b/src/types.ts @@ -299,4 +299,4 @@ export interface Pupil extends Student { announcements_count: number; messages_count: number; } -export type GetPupilsResponse = Array; +export type GetPupilsResponse = Array; \ No newline at end of file