Skip to content

Commit

Permalink
feat: support new data property instead of donnees on version>=20…
Browse files Browse the repository at this point in the history
…24.3.9
  • Loading branch information
Vexcited committed Jan 8, 2025
1 parent 03a6dbb commit 0c3f70b
Show file tree
Hide file tree
Showing 34 changed files with 152 additions and 59 deletions.
3 changes: 2 additions & 1 deletion src/api/account.ts
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
import { RequestFN } from "~/core/request-function";
import { decodeAccount } from "~/decoders/account";
import { type Account, type SessionHandle, TabLocation } from "~/models";
import { dataProperty } from "./private/data-property";

export const account = async (session: SessionHandle): Promise<Account> => {
const request = new RequestFN(session, "PageInfosPerso", {
Expand All @@ -15,5 +16,5 @@ export const account = async (session: SessionHandle): Promise<Account> => {
});

const response = await request.send();
return decodeAccount(response.data.donnees, session);
return decodeAccount(response.data[dataProperty(session)], session);
};
3 changes: 2 additions & 1 deletion src/api/assignment-remove-file.ts
Original file line number Diff line number Diff line change
@@ -1,11 +1,12 @@
import { RequestFN } from "~/core/request-function";
import { EntityState, TabLocation, type SessionHandle } from "~/models";
import { dataProperty } from "./private/data-property";

export const assignmentRemoveFile = async (session: SessionHandle, assignmentID: string): Promise<void> => {
const request = new RequestFN(session, "SaisieTAFARendreEleve", {
_Signature_: { onglet: TabLocation.Assignments },

donnees: {
[dataProperty(session)]: {
listeFichiers: [{
E: EntityState.DELETION,
TAF: { N: assignmentID }
Expand Down
3 changes: 2 additions & 1 deletion src/api/assignment-status.ts
Original file line number Diff line number Diff line change
@@ -1,11 +1,12 @@
import { RequestFN } from "~/core/request-function";
import { EntityState, SessionHandle, TabLocation } from "~/models";
import { dataProperty } from "./private/data-property";

export const assignmentStatus = async (session: SessionHandle, assignmentID: string, done: boolean): Promise<void> => {
const request = new RequestFN(session, "SaisieTAFFaitEleve", {
_Signature_: { onglet: TabLocation.Assignments },

donnees: {
[dataProperty(session)]: {
listeTAF: [{
E: EntityState.MODIFICATION,
TAFFait: done,
Expand Down
3 changes: 2 additions & 1 deletion src/api/assignment-upload-file.ts
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@ import type { FormDataFile } from "@literate.ink/utilities";
import { createEntityID } from "~/api/helpers/entity-id";
import { RequestUpload } from "~/core/request-upload";
import { RequestFN } from "~/core/request-function";
import { dataProperty } from "./private/data-property";

export const assignmentUploadFile = async (session: SessionHandle, assignmentID: string, file: FormDataFile, fileName: string): Promise<void> => {
// Check if the file can be uploaded.
Expand All @@ -22,7 +23,7 @@ export const assignmentUploadFile = async (session: SessionHandle, assignmentID:
const request = new RequestFN(session, "SaisieTAFARendreEleve", {
_Signature_: { onglet: TabLocation.Assignments },

donnees: {
[dataProperty(session)]: {
listeFichiers: [{
E: EntityState.CREATION,
G: DocumentKind.FILE,
Expand Down
7 changes: 5 additions & 2 deletions src/api/discussion-messages.ts
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
import { RequestFN } from "~/core/request-function";
import { decodeDiscussionMessages } from "~/decoders/discussion-messages";
import { type Discussion, DiscussionMessages, type SessionHandle, TabLocation } from "~/models";
import { dataProperty } from "./private/data-property";

/**
* Fetches the messages and writes them in the discussion.
Expand All @@ -12,18 +13,20 @@ import { type Discussion, DiscussionMessages, type SessionHandle, TabLocation }
* @param markAsRead Whether to mark the messages as read after fetching them.
*/
export const discussionMessages = async (session: SessionHandle, discussion: Discussion, markAsRead = false): Promise<DiscussionMessages> => {
const property = dataProperty(session);

const request = new RequestFN(session, "ListeMessages", {
_Signature_: { onglet: TabLocation.Discussions },

donnees: {
[property]: {
listePossessionsMessages: discussion.possessions,
marquerCommeLu: markAsRead,
nbMessagesVus: 0 // fetch all messages
}
});

const response = await request.send();
const messages = decodeDiscussionMessages(response.data.donnees, session);
const messages = decodeDiscussionMessages(response.data[property], session);

if (!discussion.messages) // setup the reference
discussion.messages = messages;
Expand Down
7 changes: 5 additions & 2 deletions src/api/discussion-recipients.ts
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
import { RequestFN } from "~/core/request-function";
import { decodeDiscussionRecipient } from "~/decoders/discussion-recipient";
import { Discussion, DiscussionRecipient, SessionHandle, TabLocation } from "~/models";
import { dataProperty } from "./private/data-property";

/**
* Fetches the recipients of the discussion.
Expand All @@ -9,16 +10,18 @@ import { Discussion, DiscussionRecipient, SessionHandle, TabLocation } from "~/m
* They don't have to send a message to be considered as a recipient.
*/
export const discussionRecipients = async (session: SessionHandle, discussion: Discussion): Promise<Array<DiscussionRecipient>> => {
const property = dataProperty(session);

const request = new RequestFN(session, "SaisiePublicMessage", {
_Signature_: { onglet: TabLocation.Discussions },

donnees: {
[property]: {
message: { N: discussion.participantsMessageID },
estPublicParticipant: true,
estDestinatairesReponse: false
}
});

const response = await request.send();
return response.data.donnees.listeDest.V.map(decodeDiscussionRecipient);
return response.data[property].listeDest.V.map(decodeDiscussionRecipient);
};
4 changes: 2 additions & 2 deletions src/api/discussion-send-message.ts
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@ import { RequestFN } from "~/core/request-function";
import { discussionMessages } from "./discussion-messages";
import { createEntityID } from "./helpers/entity-id";
import { discussions } from "./discussions";
import { dataProperty } from "./private/data-property";

export const discussionSendMessage = async (
session: SessionHandle,
Expand All @@ -20,7 +21,7 @@ export const discussionSendMessage = async (

const action = encodeDiscussionSendAction(discussion.messages.sendAction, includeParentsAndStudents);
const request = new RequestFN(session, "SaisieMessage", {
donnees: {
[dataProperty(session)]: {
contenu: session.user.authorizations.hasAdvancedDiscussionEditor ? {
_T: 21,
V: content
Expand Down Expand Up @@ -50,4 +51,3 @@ export const discussionSendMessage = async (
await discussions(session, discussion.cache);
await discussionMessages(session, discussion);
};

7 changes: 5 additions & 2 deletions src/api/discussions.ts
Original file line number Diff line number Diff line change
Expand Up @@ -3,20 +3,23 @@ import { decodeDiscussion } from "~/decoders/discussion";
import { decodeDiscussionFolder } from "~/decoders/discussion-folder";
import { type Discussion, type Discussions, TabLocation, type SessionHandle } from "~/models";
import type { _DiscussionsCache } from "./private/discussions-cache";
import { dataProperty } from "./private/data-property";

export const discussions = async (session: SessionHandle, cache: _DiscussionsCache = {_:[]}): Promise<Discussions> => {
const property = dataProperty(session);

const request = new RequestFN(session, "ListeMessagerie", {
_Signature_: { onglet: TabLocation.Discussions },

donnees: {
[property]: {
avecLu: true,
avecMessage: true,
possessionMessageDiscussionUnique: null
}
});

const response = await request.send();
const data = response.data.donnees;
const data = response.data[property];

const folders = data.listeEtiquettes.V.map(decodeDiscussionFolder);

Expand Down
7 changes: 5 additions & 2 deletions src/api/evaluations.ts
Original file line number Diff line number Diff line change
Expand Up @@ -2,18 +2,21 @@ import { RequestFN } from "~/core/request-function";
import { decodeEvaluation } from "~/decoders/evaluation";
import { encodePeriod } from "~/encoders/period";
import { type Evaluation, type Period, type SessionHandle, TabLocation } from "~/models";
import { dataProperty } from "./private/data-property";

export const evaluations = async (session: SessionHandle, period: Period): Promise<Array<Evaluation>> => {
const property = dataProperty(session);

const request = new RequestFN(session, "DernieresEvaluations", {
_Signature_: { onglet: TabLocation.Evaluations },

donnees: {
[property]: {
periode: encodePeriod(period)
}
});

const response = await request.send();

return response.data.donnees.listeEvaluations.V
return response.data[property].listeEvaluations.V
.map(decodeEvaluation);
};
7 changes: 5 additions & 2 deletions src/api/gradebook-pdf.ts
Original file line number Diff line number Diff line change
@@ -1,14 +1,17 @@
import { RequestFN } from "~/core/request-function";
import { encodePeriod } from "~/encoders/period";
import { type Period, type SessionHandle, TabLocation } from "~/models";
import { dataProperty } from "./private/data-property";

/**
* @param period - Period the grades report will be from.
* @returns URL to download the PDF file.
*/
export const gradebookPDF = async (session: SessionHandle, period: Period): Promise<string> => {
const property = dataProperty(session);

const request = new RequestFN(session, "GenerationPDF", {
donnees: {
[property]: {
avecCodeCompetences: false,
genreGenerationPDF: 2,

Expand All @@ -33,5 +36,5 @@ export const gradebookPDF = async (session: SessionHandle, period: Period): Prom
});

const response = await request.send();
return session.information.url + "/" + encodeURI(response.data.donnees.url.V);
return session.information.url + "/" + encodeURI(response.data[property].url.V);
};
7 changes: 5 additions & 2 deletions src/api/grades-overview.ts
Original file line number Diff line number Diff line change
Expand Up @@ -2,20 +2,23 @@ import { RequestFN } from "~/core/request-function";
import { decodeGradesOverview } from "~/decoders/grades-overview";
import { encodePeriod } from "~/encoders/period";
import { type Period, type GradesOverview, type SessionHandle, TabLocation } from "~/models";
import { dataProperty } from "./private/data-property";

/**
* Get grades overview for a specific period.
* Including student's grades with averages and the global averages
*/
export const gradesOverview = async (session: SessionHandle, period: Period): Promise<GradesOverview> => {
const property = dataProperty(session);

const request = new RequestFN(session, "DernieresNotes", {
_Signature_: { onglet: TabLocation.Grades },

donnees: {
[property]: {
Periode: encodePeriod(period)
}
});

const response = await request.send();
return decodeGradesOverview(response.data.donnees, session);
return decodeGradesOverview(response.data[property], session);
};
3 changes: 2 additions & 1 deletion src/api/helpers/login.ts
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
import { type AccountKind, type RefreshInformation, type SessionHandle, BadCredentialsError, DoubleAuthClientAction, DoubleAuthMode, SecurityError } from "~/models";
import { type AccountKind, type RefreshInformation, type SessionHandle, BadCredentialsError, SecurityError } from "~/models";
import { sessionInformation } from "../session-information";
import { instanceParameters } from "../private/instance-parameters";
import { cleanURL } from "./clean-url";
Expand Down Expand Up @@ -36,6 +36,7 @@ export const loginCredentials = async (session: SessionHandle, auth: {
cookies: [], // none
params: {
...BASE_PARAMS,
// bypasss delegation
bydlg: "A6ABB224-12DD-4E31-AD3E-8A39A1C2C335"
}
}, session.fetcher);
Expand Down
7 changes: 5 additions & 2 deletions src/api/homepage.ts
Original file line number Diff line number Diff line change
Expand Up @@ -4,18 +4,21 @@ import { TabLocation, type SessionHandle } from "~/models";
import { translateToWeekNumber } from "./helpers/week-number";
import { decodeHomepage } from "~/decoders/homepage";
import type { Homepage } from "~/models/homepage";
import { dataProperty } from "./private/data-property";

/**
* Retrieve data from the homepage for the given session.
*/
export const homepage = async (session: SessionHandle, day = session.instance.nextBusinessDay): Promise<Homepage> => {
const property = dataProperty(session);

const weekNumber = translateToWeekNumber(day, session.instance.firstMonday);
const next = encodePronoteDate(day);

const request = new RequestFN(session, "PageAccueil", {
_Signature_: { onglet: TabLocation.Presence },

donnees: {
[property]: {
avecConseilDeClasse: true,

dateGrille: {
Expand Down Expand Up @@ -102,5 +105,5 @@ export const homepage = async (session: SessionHandle, day = session.instance.ne
});

const response = await request.send();
return decodeHomepage(response.data.donnees);
return decodeHomepage(response.data[property]);
};
7 changes: 5 additions & 2 deletions src/api/menus.ts
Original file line number Diff line number Diff line change
Expand Up @@ -2,11 +2,14 @@ import { RequestFN } from "~/core/request-function";
import { decodeWeekMenu } from "~/decoders/week-menu";
import { encodePronoteDate } from "~/encoders/pronote-date";
import { TabLocation, type WeekMenu, type SessionHandle } from "~/models";
import { dataProperty } from "./private/data-property";

export const menus = async (session: SessionHandle, date = new Date()): Promise<WeekMenu> => {
const property = dataProperty(session);

const request = new RequestFN(session, "PageMenus", {
_Signature_: { onglet: TabLocation.Menus },
donnees: {
[property]: {
date: {
_T: 7,
V: encodePronoteDate(date)
Expand All @@ -15,5 +18,5 @@ export const menus = async (session: SessionHandle, date = new Date()): Promise<
});

const response = await request.send();
return decodeWeekMenu(response.data.donnees);
return decodeWeekMenu(response.data[property]);
};
6 changes: 4 additions & 2 deletions src/api/new-discussion-recipients.ts
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
import { RequestFN } from "~/core/request-function";
import { decodeNewDiscussionRecipient } from "~/decoders/new-discussion-recipient";
import { type SessionHandle, TabLocation, type EntityKind, type NewDiscussionRecipient } from "~/models";
import { dataProperty } from "./private/data-property";

/**
* Returns a list of possible recipients when creating a discussion.
Expand All @@ -11,13 +12,14 @@ import { type SessionHandle, TabLocation, type EntityKind, type NewDiscussionRec
* @param kind The kind of entity to create a discussion with. Only `Teacher`, `Student` and `Personal` are allowed.
*/
export const newDiscussionRecipients = async (session: SessionHandle, kind: EntityKind): Promise<Array<NewDiscussionRecipient>> => {
const property = dataProperty(session);
const user = session.userResource;

// TODO: use `ListePublics` for teachers.
const request = new RequestFN(session, "ListeRessourcesPourCommunication", {
_Signature_: { onglet: TabLocation.Discussions },

donnees: {
[property]: {
filtreElement: {
G: user.kind,
L: user.name,
Expand All @@ -32,7 +34,7 @@ export const newDiscussionRecipients = async (session: SessionHandle, kind: Enti
});

const response = await request.send();
return response.data.donnees.listeRessourcesPourCommunication.V
return response.data[property].listeRessourcesPourCommunication.V
.filter((recipient: any) => recipient.avecDiscussion)
.map(decodeNewDiscussionRecipient);
};
3 changes: 2 additions & 1 deletion src/api/new-discussion.ts
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
import { RequestFN } from "~/core/request-function";
import { EntityState, NewDiscussionRecipient, SessionHandle, TabLocation } from "~/models";
import { dataProperty } from "./private/data-property";

/**
* Create a discussion.
Expand All @@ -17,7 +18,7 @@ export const newDiscussion = async (
const request = new RequestFN(session, "SaisieMessage", {
_Signature_: { onglet: TabLocation.Discussions },

donnees: {
[dataProperty(session)]: {
contenu: session.user.authorizations.hasAdvancedDiscussionEditor ? {
_T: 21,
V: content
Expand Down
7 changes: 5 additions & 2 deletions src/api/news.ts
Original file line number Diff line number Diff line change
Expand Up @@ -2,12 +2,15 @@ import { RequestFN } from "~/core/request-function";
import { decodeNews } from "~/decoders/news";
import { encodeDomain } from "~/encoders/domain";
import { type News, type SessionHandle, TabLocation } from "~/models";
import { dataProperty } from "./private/data-property";

export const news = async (session: SessionHandle): Promise<News> => {
const property = dataProperty(session);

const request = new RequestFN(session, "PageActualites", {
_Signature_: { onglet: TabLocation.News },

donnees: {
[property]: {
modesAffActus: {
_T: 26,
V: encodeDomain([0])
Expand All @@ -16,5 +19,5 @@ export const news = async (session: SessionHandle): Promise<News> => {
});

const response = await request.send();
return decodeNews(response.data.donnees, session);
return decodeNews(response.data[property], session);
};
Loading

0 comments on commit 0c3f70b

Please sign in to comment.