Skip to content

Commit

Permalink
Moving assignement transformation away
Browse files Browse the repository at this point in the history
  • Loading branch information
a2br committed Dec 26, 2020
1 parent d2fe6a6 commit 3363094
Show file tree
Hide file tree
Showing 5 changed files with 63 additions and 38 deletions.
37 changes: 10 additions & 27 deletions lib/account_types/Student.ts
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,12 @@ import {
isStudentAccount,
isFailure,
} from "../types";
import { getMainAccount, getTextbookPage, toISODate } from "../functions";
import {
cleanAssignements,
getMainAccount,
getTextbookPage,
toISODate,
} from "../functions";
import { APIError } from "../errors";

export class Student extends Account {
Expand All @@ -36,35 +41,13 @@ export class Student extends Account {

async getHomework(date: Date | string | string) {
const d = toISODate(date);

const textbook = await getTextbookPage(this.account.id, this.token, d);
if (isFailure(textbook))
throw new APIError(`${textbook.code} | ${textbook.message}`);
const homework = textbook.data.matieres;
return homework.map((v) => ({
id: v.id,
interro: v.interrogation,
matiere: {
nom: v.matiere,
code: v.codeMatiere,
},
prof: v.nomProf.startsWith(" par ") ? v.nomProf.substr(5) : v.nomProf,
contenuDeSeance: {
idDevoir: v.contenuDeSeance.idDevoir,
contenu: {
original: v.contenuDeSeance.contenu,
html: Buffer.from(v.contenuDeSeance.contenu, "base64").toString(),
text: htmlToText(
Buffer.from(v.contenuDeSeance.contenu, "base64").toString(),
{
wordwrap: false,
}
),
},
documents: v.contenuDeSeance.documents,
},
_raw: v,
}));

const homework = textbook.data;
const cleaned = cleanAssignements(homework);
return cleaned;
}

async getMessages(direction: "received" | "sent" = "received") {}
Expand Down
20 changes: 12 additions & 8 deletions lib/functions/index.ts
Original file line number Diff line number Diff line change
@@ -1,14 +1,18 @@
import { login, getMainAccount } from "./login";
import { toISODate } from "./util";

export { login, getMainAccount };
export { toISODate };

//! TEXTBOOK

import { getTextbook, getTextbookPage } from "./student/textbook";
import { cleanAssignements } from "./student/textbook";

export { getTextbook, getTextbookPage, cleanAssignements };

//! MAILBOX

import { getMessages } from "./student/mailbox";

export {
login,
toISODate,
getMainAccount,
getTextbook,
getTextbookPage,
getMessages,
};
export { getMessages };
37 changes: 37 additions & 0 deletions lib/functions/student/textbook.ts
Original file line number Diff line number Diff line change
@@ -1,6 +1,9 @@
import fetch from "node-fetch";
import { htmlToText } from "html-to-text";

import { _textbookRes, _textbookDateRes } from "../../types";
import { _textbookDateAssignement, assignement } from "../../types";
import { toISODate } from "../util";

/**
* @param id Account id
Expand Down Expand Up @@ -49,3 +52,37 @@ export async function getTextbookPage(id: number, token: string, date: string) {
let body: _textbookDateRes = await edRes.json();
return body;
}

export function cleanAssignements(data: {
date: string;
matieres: _textbookDateAssignement[];
}) {
const assignements = data.matieres;
const date = toISODate(data.date); // Should not make any change
const cleaned: assignement[] = assignements.map((v) => ({
id: v.id,
date: date,
interro: v.interrogation,
matiere: {
nom: v.matiere,
code: v.codeMatiere,
},
prof: v.nomProf.startsWith(" par ") ? v.nomProf.substr(5) : v.nomProf,
contenuDeSeance: {
idDevoir: v.contenuDeSeance.idDevoir,
contenu: {
original: v.contenuDeSeance.contenu,
html: Buffer.from(v.contenuDeSeance.contenu, "base64").toString(),
text: htmlToText(
Buffer.from(v.contenuDeSeance.contenu, "base64").toString(),
{
wordwrap: false,
}
),
},
documents: v.contenuDeSeance.documents,
},
_raw: v,
}));
return cleaned;
}
5 changes: 3 additions & 2 deletions lib/types/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -17,9 +17,10 @@ export { teacherAccount, isTeacherAccount };
//! TEXTBOOK

import { _textbookRes, _textbookDateRes } from "./student/textbook";
import { assignement } from "./student/textbook";
import { _textbookDateAssignement, assignement } from "./student/textbook";

export { _textbookRes, _textbookDateRes, assignement };
export { _textbookRes, _textbookDateRes };
export { _textbookDateAssignement, assignement };

//! MAILBOX

Expand Down
2 changes: 1 addition & 1 deletion lib/types/student/textbook.ts
Original file line number Diff line number Diff line change
Expand Up @@ -36,7 +36,7 @@ export type _textbookDateResSuccess = {
};
};

type _textbookDateAssignement = {
export type _textbookDateAssignement = {
entityCode: string;
entityLibelle: string;
entityType: string;
Expand Down

0 comments on commit 3363094

Please sign in to comment.