UNPKG

ecoledirecte.js

Version:

Good-looking client for EcoleDirecte's private API.

163 lines (162 loc) 6.68 kB
"use strict"; Object.defineProperty(exports, "__esModule", { value: true }); exports.Student = void 0; const Account_1 = require("./Account"); const v3_1 = require("ecoledirecte-api-types/v3"); const util_1 = require("../util"); const classes_1 = require("../classes"); const classes_2 = require("../classes"); const textbook_1 = require("../util/student/textbook"); const mailbox_1 = require("../util/student/mailbox"); const Wallet_1 = require("../classes/Wallet"); class Student extends Account_1.Account { constructor(session) { super(session); this.session = session; this.type = "student"; const mainAccount = (0, util_1.getMainAccount)(session.loginRes.data.accounts); if (!(0, v3_1.isStudentAccount)(mainAccount)) throw new Error("Family class's main account is wrong"); if (!session.token) throw new Error("Account class MUST have token"); this.account = mainAccount; this.token = session.token; } /** * Fetches the homework * @param dates (Array of) variable(s) which can be converted into Date object(s). Preffered type: "YYYY-MM-DD" * @param onlyWithWork If true, will ignore all assignements objects that do not contain any homework */ async getHomework(params = {}, context = {}) { let { dates } = params; const { onlyWithWork } = params; if (!dates) { const upcomingAssignementDates = await (0, textbook_1.getUpcomingAssignementDates)(this.account.id, this.token, context); dates = upcomingAssignementDates.dates; this.token = upcomingAssignementDates.token; } if (!Array.isArray(dates)) dates = [dates]; const resultsArray = (await Promise.all(dates.map(async (date) => { const d = (0, util_1.toISODate)(date); const textbook = await (0, util_1.getTextbookPage)(this.account.id, this.token, d, context); this.token = textbook.token; const homework = textbook.data; const cleaned = homework.matieres.map(s => new classes_2.Assignement(s, homework.date, this)); if (onlyWithWork) return cleaned.filter(v => !!("job" in v)); return cleaned; }))) .flat() .sort((a, b) => a.date.getTime() - b.date.getTime()); return resultsArray; } /** * @returns Every sent and received message, in ascending order by id */ async getMessages(context = {}) { const received = await (0, util_1.getMessages)(this.account.id, this.token, "received", context); this.token = received.token; const sent = await (0, util_1.getMessages)(this.account.id, this.token, "sent", context); this.token = sent.token; const messages = received; messages.data.messages.sent = sent.data.messages.sent; const cleaned = (0, mailbox_1.cleanMessages)(messages, this); return cleaned; } /** * @returns Every grade */ async getGrades(context = {}) { const _grades = await (0, util_1.getGrades)(this.account.id, this.token, context); this.token = _grades.token; const grades = _grades.data.notes.map(g => new classes_2.Grade(g)); return grades; } /** * @returns Every periods with their subjects. Useful to get more infos about grades. * It is recommended to cache them. */ async getPeriods(context = {}) { const _grades = await (0, util_1.getGrades)(this.account.id, this.token, context); this.token = _grades.token; const periods = _grades.data.periodes.map(p => new classes_2.Period(p)); return periods; } /** * @returns The equivalent of `getGrades` and `getPeriods`, with only 1 request */ async getGradesAndPeriods(context = {}) { const _all = await (0, util_1.getGrades)(this.account.id, this.token, context); this.token = _all.token; const grades = _all.data.notes.map(g => new classes_2.Grade(g)); const periods = _all.data.periodes.map(p => new classes_2.Period(p)); return { grades, periods, _raw: _all }; } /** * @returns Every wallet available */ async getWallets(context = {}) { const _wallets = await (0, util_1.getWallets)(this.token, context); this.token = _wallets.token; const wallets = _wallets.data.comptes.map(c => new Wallet_1.Wallet(c)); return wallets; } async timeline(context = {}) { const _timeline = await (0, util_1.getTimeline)(this.account.id, this.token, context); this.token = _timeline.token; const tlElems = _timeline.data ? _timeline.data.map(e => new classes_1.TimelineElem(e)) : []; return tlElems; } async getPhoto() { const r = await (0, util_1.fetchPhoto)(this); if (!r) return; const [buf, str] = r; this._photo = buf; this._photoUri = str; return buf; } get photo() { return { buffer: this._photo, uri: this._photoUri, }; } async getCloud() { const _cloud = await (0, util_1.getCloudFolder)(this); const cloud = new classes_1.Cloud(_cloud.data[0], this); return cloud; } /** * @param dates (Array of) variable(s) which can be converted into Date object(s). Preffered type: "YYYY-MM-DD" * @returns The timetable as an array of courses */ async getTimetable(dates) { const _timetable = await (0, util_1.getTimetable)(this, dates); this.token = _timetable.token; const timetable = _timetable.data.map((c) => new classes_2.Course(c)); return timetable; } async getDocuments(archiveYear) { const _documents = await (0, util_1.getDocuments)(this.token, archiveYear); this.token = _documents.token; const documents = { admin: _documents.data.administratifs.map(d => new classes_1.Document(d, this, archiveYear)), bills: _documents.data.factures.map(d => new classes_1.Document(d, this, archiveYear)), grades: _documents.data.notes.map(d => new classes_1.Document(d, this, archiveYear)), schoolLife: _documents.data.viescolaire.map(d => new classes_1.Document(d, this, archiveYear)), _raw: _documents.data, }; return documents; } hasModule(module) { return this._raw.modules.some(m => m.code === module && m.enable); } get _raw() { return this.account; } } exports.Student = Student;