From 884dc218b06768c9dc1efd62df9d2dfd4e9fb935 Mon Sep 17 00:00:00 2001 From: Anatole Debierre <62328077+a2br@users.noreply.github.com> Date: Tue, 1 Nov 2022 00:02:00 +0100 Subject: [PATCH] Add first teachers features! --- lib/accounts/Teacher.ts | 25 ++++++++++ lib/classes/School.ts | 92 ++++++++++++++++++++++++++++++++++++ lib/util/teacher/schools.ts | 21 ++++++++ lib/util/teacher/students.ts | 23 +++++++++ lib/util/util.ts | 9 ++-- package-lock.json | 41 ++++++++-------- package.json | 2 +- 7 files changed, 190 insertions(+), 23 deletions(-) create mode 100644 lib/classes/School.ts create mode 100644 lib/util/teacher/schools.ts create mode 100644 lib/util/teacher/students.ts diff --git a/lib/accounts/Teacher.ts b/lib/accounts/Teacher.ts index 725d9a4..4149816 100644 --- a/lib/accounts/Teacher.ts +++ b/lib/accounts/Teacher.ts @@ -5,8 +5,13 @@ import { loginResSuccess, teacherAccount, isTeacherAccount, + schoolsResSuccess, + studentsResSuccess, } from "ecoledirecte-api-types/v3"; import { getMainAccount, fetchPhoto } from "../util"; +import { getTeachersSchools } from "../util/teacher/schools"; +import { TeachersSchool, TeachersStudent } from "../classes/School"; +import { getTeachersStudents } from "../util/teacher/students"; export class Teacher extends Account { public type: "teacher" = "teacher"; @@ -27,6 +32,26 @@ export class Teacher extends Account { this.account = mainAccount; this.token = session.token; } + // Get schools + async getSchools(): Promise< + [schools: TeachersSchool[], _raw: schoolsResSuccess] + > { + // Get schools + const res = await getTeachersSchools(this.token); + this.token = res.token; + const schools = res.data.etablissements.map(e => new TeachersSchool(e)); + return [schools, res]; + } + + // Get students (from class id) + async getStudents( + classId: number + ): Promise<[students: TeachersStudent[], _raw: studentsResSuccess]> { + const res = await getTeachersStudents(this.token, classId); + this.token = res.token; + const students = res.data.eleves.map(e => new TeachersStudent(e)); + return [students, res]; + } private _photo?: Buffer; private _photoUri?: string; diff --git a/lib/classes/School.ts b/lib/classes/School.ts new file mode 100644 index 0000000..3baf4ea --- /dev/null +++ b/lib/classes/School.ts @@ -0,0 +1,92 @@ +import { + teacherSchool as _school, + teacherLevel as _level, + teacherClass as _class, + teacherStudent as _student, +} from "ecoledirecte-api-types"; + +export class TeachersSchool { + id: number; + code: string; + name: string; + + levels: TeachersLevel[]; + + _raw: _school; + + constructor(o: _school) { + this.id = o.id; + this.code = o.code; + this.name = o.libelle; + + this.levels = o.niveaux.map(l => new TeachersLevel(l)); + + this._raw = o; + } +} + +export class TeachersLevel { + id: number; + code: string; + name: string; + + classes: TeachersClass[]; + + _raw: _level; + + constructor(o: _level) { + this.id = o.id; + this.code = o.code; + this.name = o.libelle; + + this.classes = o.classes.map(c => new TeachersClass(c)); + + this._raw = o; + } +} + +export class TeachersClass { + id: number; + code: string; + name: string; + + isHeadTeacher: boolean; + headTeachers: { + id: number; + firstName: string; + lastName: string; + }[]; + + _raw: _class; + + constructor(o: _class) { + this.id = o.id; + this.code = o.code; + this.name = o.libelle; + + this.isHeadTeacher = o.isPP; + this.headTeachers = o.tabPP.map(r => ({ + id: r.id, + firstName: r.prenom, + lastName: r.nom, + })); + + this._raw = o; + } +} + +export class TeachersStudent { + id: number; + firstName: string; + lastName: string; + + _raw: _student; + + constructor(o: _student) { + this.id = o.id; + this.firstName = o.prenom; + this.lastName = o.nom; + + this._raw = o; + } +} diff --git a/lib/util/teacher/schools.ts b/lib/util/teacher/schools.ts new file mode 100644 index 0000000..f225f7f --- /dev/null +++ b/lib/util/teacher/schools.ts @@ -0,0 +1,21 @@ +// Function that fetches school (input: token) + +import { makeRequest } from "../util"; +import { schoolsResSuccess, Routes } from "ecoledirecte-api-types"; + +export async function getTeachersSchools( + token: string +): Promise { + const body: schoolsResSuccess = await makeRequest( + { + method: "POST", + path: Routes.teacherSchools(), + body: { token }, + guard: true, + teachRoot: true, + }, + { action: "getTeachersSchools" } + ); + + return body; +} diff --git a/lib/util/teacher/students.ts b/lib/util/teacher/students.ts new file mode 100644 index 0000000..bf88f0a --- /dev/null +++ b/lib/util/teacher/students.ts @@ -0,0 +1,23 @@ +// Function that retrieves students for specific class +// input: token, class id + +import { Routes, studentsResSuccess } from "ecoledirecte-api-types"; +import { makeRequest } from "../util"; + +export async function getTeachersStudents( + token: string, + classId: number +): Promise { + const body: studentsResSuccess = await makeRequest( + { + method: "POST", + path: Routes.teacherStudents(classId), + body: { token }, + guard: true, + teachRoot: true, + }, + { action: "getTeachersStudents" } + ); + + return body; +} diff --git a/lib/util/util.ts b/lib/util/util.ts index e4c9d94..856fd80 100644 --- a/lib/util/util.ts +++ b/lib/util/util.ts @@ -1,5 +1,5 @@ import fetch, { RequestInit } from "node-fetch"; -import { failureRes, isFailure, root } from "ecoledirecte-api-types/v3"; +import { failureRes, isFailure, root, rootp } from "ecoledirecte-api-types/v3"; import logs from "../events"; import { EcoleDirecteAPIError } from "../errors"; @@ -45,14 +45,15 @@ export async function makeRequest( path: string; body?: Record; guard?: boolean; + teachRoot?: boolean; }, context: Record = {}, account?: Account, token?: string // eslint-disable-next-line @typescript-eslint/no-explicit-any ): Promise { - const { method, path, body, guard } = options; - const url = Config.get("root") + path; + const { method, path, body, guard, teachRoot } = options; + const url = Config.get(teachRoot ? "rootp" : "root") + path; const resListener = new EventEmitter(); function onRes(callback: (res: Response) => void) { resListener.on("response", callback); @@ -118,6 +119,7 @@ export const EdHeaders = { export type FullConfig = { root: string; + rootp: string; addedHeaders: Record; }; @@ -125,6 +127,7 @@ export type PartialConfig = Partial; export const DefaultConfig: FullConfig = { root: root, + rootp: rootp, addedHeaders: {}, }; diff --git a/package-lock.json b/package-lock.json index 6bcad61..c05edca 100644 --- a/package-lock.json +++ b/package-lock.json @@ -9,7 +9,7 @@ "version": "0.31.1", "license": "MIT", "dependencies": { - "ecoledirecte-api-types": "^0.12.0", + "ecoledirecte-api-types": "^0.13.1", "html-to-text": "^8.0.0", "node-fetch": "^2.6.7" }, @@ -2297,9 +2297,9 @@ } }, "node_modules/ecoledirecte-api-types": { - "version": "0.12.0", - "resolved": "https://registry.npmjs.org/ecoledirecte-api-types/-/ecoledirecte-api-types-0.12.0.tgz", - "integrity": "sha512-v6pfCs6g6pX0YbfHos3a7BY+wbw5X9ud1oh9pTgPQCSoEgaT8SOho44kl+oWUHsJKYpucViz5D02M5ip9ue03A==" + "version": "0.13.1", + "resolved": "https://registry.npmjs.org/ecoledirecte-api-types/-/ecoledirecte-api-types-0.13.1.tgz", + "integrity": "sha512-tIC34cQJPi8S//QMhURJAWbIpaUoytZumXT9wt3YZ1bJX0S5h0xmDJBavo5JZeU4HqHFEyV2W11xaebRoO4n6w==" }, "node_modules/electron-to-chromium": { "version": "1.3.761", @@ -4599,9 +4599,9 @@ } }, "node_modules/minimatch": { - "version": "3.0.4", - "resolved": "https://registry.npmjs.org/minimatch/-/minimatch-3.0.4.tgz", - "integrity": "sha512-yJHVQEhyqPLUTgt9B83PXu6W3rx4MvvHvSUvToogpwoGDOUQ+yDrR0HRot+yOCdCO7u4hX3pWft6kWBBcqh0UA==", + "version": "3.1.2", + "resolved": "https://registry.npmjs.org/minimatch/-/minimatch-3.1.2.tgz", + "integrity": "sha512-J7p63hRiAjw1NDEww1W7i37+ByIrOWO5XQQAzZ3VOcL0PNybwpfmV/N05zFAzwQ9USyEcX6t3UO+K5aqBQOIHw==", "dev": true, "dependencies": { "brace-expansion": "^1.1.7" @@ -4611,9 +4611,12 @@ } }, "node_modules/minimist": { - "version": "1.2.5", - "resolved": "https://registry.npmjs.org/minimist/-/minimist-1.2.5.tgz", - "integrity": "sha512-FM9nNUYrRBAELZQT3xeZQ7fmMOBg6nWNmJKTcgsJeaLstP/UODVpGsr5OhXhhXg6f+qtJ8uiZ+PUxkDWcgIXLw==" + "version": "1.2.7", + "resolved": "https://registry.npmjs.org/minimist/-/minimist-1.2.7.tgz", + "integrity": "sha512-bzfL1YUZsP41gmu/qjrEk0Q6i2ix/cVeAhbCbqH9u3zYutS1cLg00qhrD0M2MVdCcx4Sc0UpP2eBWo9rotpq6g==", + "funding": { + "url": "https://github.com/sponsors/ljharb" + } }, "node_modules/mkdirp": { "version": "1.0.4", @@ -7694,9 +7697,9 @@ "dev": true }, "ecoledirecte-api-types": { - "version": "0.12.0", - "resolved": "https://registry.npmjs.org/ecoledirecte-api-types/-/ecoledirecte-api-types-0.12.0.tgz", - "integrity": "sha512-v6pfCs6g6pX0YbfHos3a7BY+wbw5X9ud1oh9pTgPQCSoEgaT8SOho44kl+oWUHsJKYpucViz5D02M5ip9ue03A==" + "version": "0.13.1", + "resolved": "https://registry.npmjs.org/ecoledirecte-api-types/-/ecoledirecte-api-types-0.13.1.tgz", + "integrity": "sha512-tIC34cQJPi8S//QMhURJAWbIpaUoytZumXT9wt3YZ1bJX0S5h0xmDJBavo5JZeU4HqHFEyV2W11xaebRoO4n6w==" }, "electron-to-chromium": { "version": "1.3.761", @@ -9413,18 +9416,18 @@ "dev": true }, "minimatch": { - "version": "3.0.4", - "resolved": "https://registry.npmjs.org/minimatch/-/minimatch-3.0.4.tgz", - "integrity": "sha512-yJHVQEhyqPLUTgt9B83PXu6W3rx4MvvHvSUvToogpwoGDOUQ+yDrR0HRot+yOCdCO7u4hX3pWft6kWBBcqh0UA==", + "version": "3.1.2", + "resolved": "https://registry.npmjs.org/minimatch/-/minimatch-3.1.2.tgz", + "integrity": "sha512-J7p63hRiAjw1NDEww1W7i37+ByIrOWO5XQQAzZ3VOcL0PNybwpfmV/N05zFAzwQ9USyEcX6t3UO+K5aqBQOIHw==", "dev": true, "requires": { "brace-expansion": "^1.1.7" } }, "minimist": { - "version": "1.2.5", - "resolved": "https://registry.npmjs.org/minimist/-/minimist-1.2.5.tgz", - "integrity": "sha512-FM9nNUYrRBAELZQT3xeZQ7fmMOBg6nWNmJKTcgsJeaLstP/UODVpGsr5OhXhhXg6f+qtJ8uiZ+PUxkDWcgIXLw==" + "version": "1.2.7", + "resolved": "https://registry.npmjs.org/minimist/-/minimist-1.2.7.tgz", + "integrity": "sha512-bzfL1YUZsP41gmu/qjrEk0Q6i2ix/cVeAhbCbqH9u3zYutS1cLg00qhrD0M2MVdCcx4Sc0UpP2eBWo9rotpq6g==" }, "mkdirp": { "version": "1.0.4", diff --git a/package.json b/package.json index e7b4761..26b8e9f 100644 --- a/package.json +++ b/package.json @@ -42,7 +42,7 @@ "typescript": "^4.2.2" }, "dependencies": { - "ecoledirecte-api-types": "^0.12.0", + "ecoledirecte-api-types": "^0.13.1", "html-to-text": "^8.0.0", "node-fetch": "^2.6.7" }