Skip to content

Commit

Permalink
Add Wallets
Browse files Browse the repository at this point in the history
  • Loading branch information
a2br committed Jun 17, 2022
1 parent 5a2f2f4 commit 8818ee8
Show file tree
Hide file tree
Showing 7 changed files with 153 additions and 31 deletions.
12 changes: 12 additions & 0 deletions lib/accounts/Student.ts
Original file line number Diff line number Diff line change
Expand Up @@ -19,12 +19,14 @@ import {
fetchPhoto,
getCloudFolder,
getTimetable,
getWallets,
} from "../util";
import { Cloud, TimelineElem } from "../classes";
import { Message, Grade, Period, Assignement, Course } from "../classes";

import { getUpcomingAssignementDates } from "../util/student/textbook";
import { cleanMessages } from "../util/student/mailbox";
import { Wallet } from "../classes/Wallet";

export class Student extends Account {
public type: "student" = "student";
Expand Down Expand Up @@ -156,6 +158,16 @@ export class Student extends Account {
return { grades, periods, _raw: _all };
}

/**
* @returns Every wallet available
*/
async getWallets(context: Record<string, unknown> = {}): Promise<Wallet[]> {
const _wallets = await getWallets(this.token, context);
this.token = _wallets.token;
const wallets = _wallets.data.comptes.map(c => new Wallet(c));
return wallets;
}

async timeline(
context: Record<string, unknown> = {}
): Promise<TimelineElem[]> {
Expand Down
71 changes: 71 additions & 0 deletions lib/classes/Wallet.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,71 @@
import {
compte as _compte,
ecriture as _ecriture,
} from "ecoledirecte-api-types";

export type walletType = "wallet" | "activity";

export class Wallet {
id: number;
/**
* Should be identical to `id`
*/
studentId: number;
type: walletType;
available: boolean;

unitCost: number;
editableAmount: boolean;
editableQuantity: boolean;
code: string;

classServiceId: number;
name: string;

balance: number;

future: unknown[];
transactions: Transaction[];

_raw: _compte;

constructor(o: _compte) {
this.id = o.id;
this.studentId = o.idEleve;
this.type = o.typeCompte === "portemonnaie" ? "wallet" : "activity";
this.available = o.disponible;
this.unitCost = o.montantVersement;
this.editableAmount = o.montantModifiable;
this.editableQuantity = o.quantiteModifiable;
this.code = o.codeCompte;
this.classServiceId = o.idServiceClasse;
this.name = o.libelleCompte;
this.balance = o.solde;
this.future = o.avenir;
this.transactions = o.ecritures && o.ecritures.map(e => new Transaction(e));

this._raw = o;
}
}

export class Transaction {
date: Date;
name?: string;
lettering?: string;
compInfo?: string;
amount: number;
transactions?: Transaction[];

_raw: _ecriture;

constructor(o: _ecriture) {
this.date = new Date(o.date);
this.name = o.libelle;
this.lettering = o.lettrage || undefined;
this.compInfo = o.infoComp || undefined;
this.amount = o.montant;
this.transactions = o.ecritures && o.ecritures.map(e => new Transaction(e));

this._raw = o;
}
}
4 changes: 4 additions & 0 deletions lib/util/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,10 @@ export * from "./student/grades";

export * from "./student/timelines";

//! WALLETS

export * from "./student/wallets";

//! CLOUD

export * from "./global/cloud";
Expand Down
22 changes: 22 additions & 0 deletions lib/util/student/wallets.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,22 @@
//TODO See ed-api-types & Postman research

import { Routes, walletsResSuccess } from "ecoledirecte-api-types/v3";
import { makeRequest } from "../util";

export async function getWallets(
token: string,
context: Record<string, unknown> = {}
): Promise<walletsResSuccess> {
const body: walletsResSuccess = await makeRequest(
{
method: "POST",
path: Routes.studentWallets(),
body: {},
guard: true,
},
{ action: "getWallets", ...context },
undefined,
token
);
return body;
}
19 changes: 16 additions & 3 deletions lib/util/util.ts
Original file line number Diff line number Diff line change
Expand Up @@ -47,7 +47,8 @@ export async function makeRequest(
guard?: boolean;
},
context: Record<string, unknown> = {},
account?: Account
account?: Account,
token?: string
// eslint-disable-next-line @typescript-eslint/no-explicit-any
): Promise<any> {
const { method, path, body, guard } = options;
Expand All @@ -59,11 +60,23 @@ export async function makeRequest(
function offRes(callback: (res: Response) => void) {
resListener.off("response", callback);
}
logs.emit("request", { method, url, body, context, onRes, offRes });
const params: RequestInit = {
method: method,
headers: { ...EdHeaders, ...Config.get("addedHeaders") },
headers: {
...EdHeaders,
"x-token": (token || account?.token) ?? "",
...Config.get("addedHeaders"),
},
};
logs.emit("request", {
method,
url,
body,
headers: params.headers,
context,
onRes,
offRes,
});

if (method === "POST") {
const urlencoded = new URLSearchParams();
Expand Down
52 changes: 26 additions & 26 deletions package-lock.json

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

4 changes: 2 additions & 2 deletions package.json
Original file line number Diff line number Diff line change
Expand Up @@ -42,8 +42,8 @@
"typescript": "^4.2.2"
},
"dependencies": {
"ecoledirecte-api-types": "^0.11.0",
"ecoledirecte-api-types": "^0.12.0",
"html-to-text": "^8.0.0",
"node-fetch": "^3.2.0"
"node-fetch": "^3.2.6"
}
}

0 comments on commit 8818ee8

Please sign in to comment.