1
0
Fork 0
mirror of https://github.com/classchartsapi/classcharts-api-js.git synced 2026-05-14 19:59:37 +00:00
classcharts-api-js/src/core/parentClient.ts

116 lines
3.8 KiB
TypeScript
Raw Normal View History

import type { ChangePasswordResponse, GetPupilsResponse } from "../types.ts";
2022-03-12 11:30:03 +00:00
import { BaseClient } from "../core/baseClient.ts";
import { API_BASE_PARENT, BASE_URL } from "../utils/consts.ts";
import { parseCookies } from "../utils/utils.ts";
2022-03-12 11:30:03 +00:00
/**
2023-04-07 13:47:08 +01:00
* Parent Client
2022-03-12 11:30:03 +00:00
*/
2023-04-16 20:47:40 +01:00
export class ParentClient extends BaseClient {
2022-03-12 11:34:56 +00:00
private password = "";
private email = "";
2022-03-12 12:00:28 +00:00
// @ts-expect-error Init in .login
public pupils: GetPupilsResponse;
2022-03-12 11:30:03 +00:00
/**
2023-04-16 20:47:40 +01:00
* @param email Parent's email address
* @param password Parent's password
2022-03-12 11:30:03 +00:00
*/
constructor(email: string, password: string) {
super(API_BASE_PARENT);
2022-03-12 11:30:03 +00:00
this.email = String(email);
this.password = String(password);
}
/**
2023-05-14 00:59:21 +01:00
* Authenticates with ClassCharts
2022-03-12 11:30:03 +00:00
*/
async login(): Promise<void> {
2023-08-30 12:28:49 +01:00
if (!this.email) throw new Error("Email not provided");
if (!this.password) throw new Error("Password not provided");
2022-03-12 11:30:03 +00:00
const formData = new URLSearchParams();
formData.append("_method", "POST");
formData.append("email", this.email);
formData.append("logintype", "existing");
formData.append("password", this.password);
2023-05-14 00:59:21 +01:00
formData.append("recaptcha-token", "no-token-available");
const headers = new Headers({
"Content-Type": "application/x-www-form-urlencoded",
2022-03-12 11:30:03 +00:00
});
2023-05-30 18:37:24 +01:00
const response = await fetch(BASE_URL + "/parent/login", {
method: "POST",
body: formData,
headers: headers,
2023-09-16 19:52:03 +01:00
redirect: "manual",
});
2023-09-10 13:02:05 +01:00
if (response.status != 302 || !response.headers.has("set-cookie")) {
await response.body?.cancel(); // Make deno tests happy by closing the body, unsure whether this is needed for the actual library
throw new Error(
"Unauthenticated: ClassCharts didn't return authentication cookies",
);
2023-08-30 12:28:49 +01:00
}
2022-03-12 11:30:03 +00:00
const cookies = String(response.headers.get("set-cookie"));
2023-04-07 13:47:08 +01:00
// this.authCookies = cookies.split(";");
2022-08-03 16:53:46 +01:00
const sessionCookies = parseCookies(cookies);
const sessionID = JSON.parse(
2023-08-30 13:43:25 +01:00
String(sessionCookies["parent_session_credentials"]),
2022-03-12 11:30:03 +00:00
);
2023-09-16 19:52:03 +01:00
this.sessionId = sessionID.session_id;
2022-03-12 12:00:28 +00:00
this.pupils = await this.getPupils();
if (!this.pupils) throw new Error("Account has no pupils attached");
2023-09-16 19:52:03 +01:00
this.studentId = this.pupils[0].id;
2022-03-12 11:30:03 +00:00
}
/**
2023-04-16 20:47:40 +01:00
* Get a list of pupils connected to this parent's account
2023-04-07 13:47:08 +01:00
* @returns an array of Pupils connected to this parent's account
2022-03-12 11:30:03 +00:00
*/
2022-03-12 11:34:56 +00:00
async getPupils(): Promise<GetPupilsResponse> {
2023-09-16 19:52:03 +01:00
const response = await this.makeAuthedRequest(this.API_BASE + "/pupils", {
2022-03-12 11:34:56 +00:00
method: "GET",
});
2023-09-16 19:52:03 +01:00
return response.data;
2022-03-12 11:34:56 +00:00
}
2022-03-12 12:00:28 +00:00
/**
2022-03-12 12:02:30 +00:00
* Selects a pupil to be used with API requests
2023-04-16 20:47:40 +01:00
* @param pupilId Pupil ID obtained from this.pupils or getPupils()
*
* @see getPupils
2022-03-12 12:00:28 +00:00
*/
2023-08-30 12:28:49 +01:00
selectPupil(pupilId: number) {
2022-03-12 12:00:28 +00:00
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) {
2023-09-16 19:52:03 +01:00
this.studentId = pupil.id;
2022-03-12 12:00:28 +00:00
return;
}
}
throw new Error("No pupil with specified ID returned");
}
/**
* Changes the login password for the current parent account
* @param current Current password
* @param new New password
* @returns Whether the request was successful
*/
async changePassword(this: ParentClient, current: string, newPassword: string): Promise<ChangePasswordResponse>{
const formData = new URLSearchParams();
formData.append("current", current);
formData.append("new", newPassword);
formData.append("repeat", newPassword);
"Content-Type": "application/x-www-form-urlencoded",
});
return (
await this.makeAuthedRequest(
this.API_BASE + "/password",
{
method: "POST",
body: formData,
headers: headers,
},
)
);
}
2022-03-12 11:30:03 +00:00
}