2023-10-08 21:02:01 +01:00
|
|
|
import type { ChangePasswordResponse, GetPupilsResponse } from "../types.ts";
|
2022-03-12 11:30:03 +00:00
|
|
|
|
2023-09-24 13:36:35 +01: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
|
|
|
/**
|
2024-05-28 14:07:34 +01:00
|
|
|
* Parent Client.
|
|
|
|
|
* See {@link BaseClient} for all shared methods.
|
|
|
|
|
*
|
|
|
|
|
* @example
|
|
|
|
|
* ```ts
|
|
|
|
|
* import { ParentClient } from "classcharts-api";
|
|
|
|
|
* const client = new ParentClient("username", "password");
|
|
|
|
|
* await client.login();
|
|
|
|
|
* ```
|
2022-03-12 11:30:03 +00:00
|
|
|
*/
|
2023-04-16 20:47:40 +01:00
|
|
|
export class ParentClient extends BaseClient {
|
2023-11-29 00:03:56 +00:00
|
|
|
private password = "";
|
|
|
|
|
private email = "";
|
|
|
|
|
public pupils: GetPupilsResponse;
|
|
|
|
|
/**
|
|
|
|
|
* @param email Parent's email address
|
|
|
|
|
* @param password Parent's password
|
|
|
|
|
*/
|
|
|
|
|
constructor(email: string, password: string) {
|
|
|
|
|
super(API_BASE_PARENT);
|
|
|
|
|
this.email = String(email);
|
|
|
|
|
this.password = String(password);
|
2024-05-28 14:07:34 +01:00
|
|
|
this.pupils = [];
|
2023-11-29 00:03:56 +00:00
|
|
|
}
|
2022-03-12 11:30:03 +00:00
|
|
|
|
2023-11-29 00:03:56 +00:00
|
|
|
/**
|
|
|
|
|
* Authenticates with ClassCharts
|
|
|
|
|
*/
|
|
|
|
|
async login(): Promise<void> {
|
2024-01-10 10:57:33 +00:00
|
|
|
if (!this.email) {
|
|
|
|
|
throw new Error("Email not provided");
|
|
|
|
|
}
|
|
|
|
|
if (!this.password) {
|
|
|
|
|
throw new Error("Password not provided");
|
|
|
|
|
}
|
2023-11-29 00:03:56 +00:00
|
|
|
const formData = new URLSearchParams();
|
|
|
|
|
formData.append("_method", "POST");
|
|
|
|
|
formData.append("email", this.email);
|
|
|
|
|
formData.append("logintype", "existing");
|
|
|
|
|
formData.append("password", this.password);
|
|
|
|
|
formData.append("recaptcha-token", "no-token-available");
|
|
|
|
|
const headers = new Headers({
|
|
|
|
|
"Content-Type": "application/x-www-form-urlencoded",
|
|
|
|
|
});
|
|
|
|
|
const response = await fetch(`${BASE_URL}/parent/login`, {
|
|
|
|
|
method: "POST",
|
|
|
|
|
body: formData,
|
|
|
|
|
headers: headers,
|
|
|
|
|
redirect: "manual",
|
|
|
|
|
});
|
|
|
|
|
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",
|
|
|
|
|
);
|
|
|
|
|
}
|
2022-03-12 11:30:03 +00:00
|
|
|
|
2026-03-13 21:52:22 +00:00
|
|
|
// Get ALL Set-Cookie headers (get() only returns the first one!)
|
|
|
|
|
const setCookieHeaders = response.headers.getSetCookie();
|
|
|
|
|
if (!setCookieHeaders || setCookieHeaders.length < 2) {
|
|
|
|
|
await response.body?.cancel();
|
|
|
|
|
throw new Error("Unauthenticated: Missing Set-Cookie headers");
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
// Parse both cookies
|
|
|
|
|
const cookie1 = parseCookies(setCookieHeaders[0]);
|
|
|
|
|
const cookie2 = parseCookies(setCookieHeaders[1]);
|
|
|
|
|
|
|
|
|
|
// Store only the name=value portion of each Set-Cookie header
|
|
|
|
|
this.authCookies = setCookieHeaders.map((h) => h.split(";")[0].trim());
|
|
|
|
|
|
|
|
|
|
// Get session ID from parent_session_credentials cookie
|
2023-11-29 00:03:56 +00:00
|
|
|
const sessionID = JSON.parse(
|
2026-03-13 21:52:22 +00:00
|
|
|
String(cookie1.parent_session_credentials || cookie2.parent_session_credentials),
|
2023-11-29 00:03:56 +00:00
|
|
|
);
|
|
|
|
|
this.sessionId = sessionID.session_id;
|
|
|
|
|
this.pupils = await this.getPupils();
|
2024-01-10 10:57:33 +00:00
|
|
|
if (!this.pupils) {
|
|
|
|
|
throw new Error("Account has no pupils attached");
|
|
|
|
|
}
|
2023-11-29 00:03:56 +00:00
|
|
|
this.studentId = this.pupils[0].id;
|
|
|
|
|
}
|
|
|
|
|
/**
|
|
|
|
|
* Get a list of pupils connected to this parent's account
|
|
|
|
|
* @returns an array of Pupils connected to this parent's account
|
|
|
|
|
*/
|
|
|
|
|
async getPupils(): Promise<GetPupilsResponse> {
|
|
|
|
|
const response = await this.makeAuthedRequest(`${this.API_BASE}/pupils`, {
|
|
|
|
|
method: "GET",
|
|
|
|
|
});
|
|
|
|
|
return response.data;
|
|
|
|
|
}
|
|
|
|
|
/**
|
|
|
|
|
* Selects a pupil to be used with API requests
|
|
|
|
|
* @param pupilId Pupil ID obtained from this.pupils or getPupils()
|
|
|
|
|
*
|
|
|
|
|
* @see getPupils
|
|
|
|
|
*/
|
|
|
|
|
selectPupil(pupilId: number) {
|
2024-01-10 10:57:33 +00:00
|
|
|
if (!pupilId) {
|
|
|
|
|
throw new Error("No pupil ID specified");
|
|
|
|
|
}
|
2023-11-29 00:03:56 +00:00
|
|
|
const pupils = this.pupils;
|
|
|
|
|
for (let i = 0; i < pupils.length; i++) {
|
|
|
|
|
const pupil = pupils[i];
|
|
|
|
|
if (pupil.id === pupilId) {
|
|
|
|
|
this.studentId = pupil.id;
|
|
|
|
|
return;
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
throw new Error("No pupil with specified ID returned");
|
|
|
|
|
}
|
|
|
|
|
/**
|
|
|
|
|
* Changes the login password for the current parent account
|
|
|
|
|
* @param currentPassword Current password
|
|
|
|
|
* @param newPassword New password
|
|
|
|
|
* @returns Whether the request was successful
|
|
|
|
|
*/
|
|
|
|
|
async changePassword(
|
|
|
|
|
currentPassword: string,
|
|
|
|
|
newPassword: string,
|
|
|
|
|
): Promise<ChangePasswordResponse> {
|
|
|
|
|
const formData = new URLSearchParams();
|
|
|
|
|
formData.append("current", currentPassword);
|
|
|
|
|
formData.append("new", newPassword);
|
|
|
|
|
formData.append("repeat", newPassword);
|
|
|
|
|
return await this.makeAuthedRequest(`${this.API_BASE}/password`, {
|
|
|
|
|
method: "POST",
|
|
|
|
|
body: formData,
|
|
|
|
|
headers: {
|
|
|
|
|
"Content-Type": "application/x-www-form-urlencoded",
|
|
|
|
|
},
|
|
|
|
|
});
|
|
|
|
|
}
|
2022-03-12 11:30:03 +00:00
|
|
|
}
|