Skip to content

Commit

Permalink
Add first teachers features!
Browse files Browse the repository at this point in the history
  • Loading branch information
a2br committed Oct 31, 2022
1 parent 2d8380d commit 884dc21
Show file tree
Hide file tree
Showing 7 changed files with 190 additions and 23 deletions.
25 changes: 25 additions & 0 deletions lib/accounts/Teacher.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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";
Expand All @@ -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;
Expand Down
92 changes: 92 additions & 0 deletions lib/classes/School.ts
Original file line number Diff line number Diff line change
@@ -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;
}
}
21 changes: 21 additions & 0 deletions lib/util/teacher/schools.ts
Original file line number Diff line number Diff line change
@@ -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<schoolsResSuccess> {
const body: schoolsResSuccess = await makeRequest(
{
method: "POST",
path: Routes.teacherSchools(),
body: { token },
guard: true,
teachRoot: true,
},
{ action: "getTeachersSchools" }
);

return body;
}
23 changes: 23 additions & 0 deletions lib/util/teacher/students.ts
Original file line number Diff line number Diff line change
@@ -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<studentsResSuccess> {
const body: studentsResSuccess = await makeRequest(
{
method: "POST",
path: Routes.teacherStudents(classId),
body: { token },
guard: true,
teachRoot: true,
},
{ action: "getTeachersStudents" }
);

return body;
}
9 changes: 6 additions & 3 deletions lib/util/util.ts
Original file line number Diff line number Diff line change
@@ -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";
Expand Down Expand Up @@ -45,14 +45,15 @@ export async function makeRequest(
path: string;
body?: Record<string, unknown>;
guard?: boolean;
teachRoot?: boolean;
},
context: Record<string, unknown> = {},
account?: Account,
token?: string
// eslint-disable-next-line @typescript-eslint/no-explicit-any
): Promise<any> {
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);
Expand Down Expand Up @@ -118,13 +119,15 @@ export const EdHeaders = {

export type FullConfig = {
root: string;
rootp: string;
addedHeaders: Record<string, string>;
};

export type PartialConfig = Partial<FullConfig>;

export const DefaultConfig: FullConfig = {
root: root,
rootp: rootp,
addedHeaders: {},
};

Expand Down
41 changes: 22 additions & 19 deletions package-lock.json

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

2 changes: 1 addition & 1 deletion package.json
Original file line number Diff line number Diff line change
Expand Up @@ -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"
}
Expand Down

0 comments on commit 884dc21

Please sign in to comment.