Skip to content

Commit

Permalink
chore(update): Update Pawdirect
Browse files Browse the repository at this point in the history
Modifications EcoleDirecte
  • Loading branch information
godetremy authored Dec 13, 2024
2 parents 898a3c8 + dad36e4 commit ef3de10
Show file tree
Hide file tree
Showing 11 changed files with 125 additions and 80 deletions.
9 changes: 5 additions & 4 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 @@ -71,7 +71,7 @@
"lottie-react-native": "^6.7.0",
"lucide-react-native": "^0.378.0",
"openid-client": "^5.7.0",
"pawdirecte": "^1.6.0",
"pawdirecte": "^1.7.0",
"pawnilim": "^0.2.0",
"pawnote": "^1.2.2",
"pawrd": "^0.6.1",
Expand Down
8 changes: 4 additions & 4 deletions src/services/ecoledirecte/attendance.ts
Original file line number Diff line number Diff line change
Expand Up @@ -2,13 +2,13 @@ import ecoledirecte, {AttendanceItem, AttendanceItemKind} from "pawdirecte";
import type { EcoleDirecteAccount } from "@/stores/account/types";
import { ErrorServiceUnauthenticated } from "../shared/errors";
import type { Attendance } from "../shared/Attendance";
import {dateStringAsTimeInterval, getDuration} from "@/services/ecoledirecte/time-interval";
import {dateStringAsTimeInterval, getDuration, getDurationInHours} from "@/services/ecoledirecte/time-interval";
import {Punishment} from "@/services/shared/Punishment";
import {Absence} from "@/services/shared/Absence";
import {Delay} from "@/services/shared/Delay";

const decodeDelay = (item: AttendanceItem): Delay => {
const timeInterval = dateStringAsTimeInterval(item.displayDate);
const timeInterval = dateStringAsTimeInterval(item.displayDate);
const duration = (timeInterval?.end && timeInterval.start) ? getDuration(timeInterval).getTime() / (60 * 1000): 0;
return {
id: item.id.toString(),
Expand All @@ -22,15 +22,15 @@ const decodeDelay = (item: AttendanceItem): Delay => {

const decodeAbsence = (item: AttendanceItem): Absence => {
const timeInterval = dateStringAsTimeInterval(item.displayDate);
const duration = (timeInterval?.end && timeInterval.start) ? getDuration(timeInterval): new Date();
const duration = (timeInterval?.end && timeInterval.start) ? getDurationInHours(timeInterval) : "";
const fromTimestamp = timeInterval?.start ? new Date(timeInterval.start).getTime(): 0;
const toTimestamp = timeInterval?.end ? new Date(timeInterval.end).getTime(): 0;
return {
id: item.id.toString(),
fromTimestamp,
toTimestamp,
justified: item.justified,
hours: duration.getHours() + "h" + duration.getMinutes(),
hours: duration,
administrativelyFixed: item.justified,
reasons: item.reason,
};
Expand Down
6 changes: 5 additions & 1 deletion src/services/ecoledirecte/chats.ts
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,10 @@ export const getChats = async (account: EcoleDirecteAccount): Promise<Chat[]> =>
}));
};

const cleanMessage = (message: string) => {
return message.replace(/>\s+/g, '>').replace(/&nbsp;/g, ' ');
}

export const getChatMessages = async (account: EcoleDirecteAccount, chat: Chat): Promise<ChatMessage> => {
if (!account.instance)
throw new ErrorServiceUnauthenticated("ecoledirecte");
Expand All @@ -26,7 +30,7 @@ export const getChatMessages = async (account: EcoleDirecteAccount, chat: Chat):

return {
id: message.id.toString(),
content: message.content,
content: cleanMessage(message.content),
author: message.sender,
date: message.date,
subject: chat.subject,
Expand Down
37 changes: 12 additions & 25 deletions src/services/ecoledirecte/grades.ts
Original file line number Diff line number Diff line change
Expand Up @@ -22,39 +22,26 @@ const decodePeriod = (p: PawdirectePeriod): Period => {
};
};

const decodeGradeKind = (kind: GradeKind): GradeInformation | undefined => {
switch (kind) {
case GradeKind.Error:
const decodeGradeValue = (
value: ecoledirecte.GradeValue | undefined,
): GradeValue => {
if (typeof value === "undefined")
return { value: null, disabled: true };

switch (value.kind) {
case GradeKind.Grade:
return undefined;
return { value: value.points ?? 0, disabled: false };
case GradeKind.Absent:
return GradeInformation.Absent;
return { value: value.points ?? 0, disabled: true, information: GradeInformation.Absent };
case GradeKind.Exempted:
return GradeInformation.Exempted;
return { value: value.points ?? 0, disabled: true, information: GradeInformation.Exempted };
case GradeKind.NotGraded:
return GradeInformation.NotGraded;
return { value: value.points ?? 0, disabled: true, information: GradeInformation.NotGraded };
default:
return undefined;
return { value: value.points ?? 0, disabled: true };
}
};

const decodeGradeValue = (
value: ecoledirecte.GradeValue | undefined,
): GradeValue => {
if (!value)
return {
disabled: true,
information: GradeInformation.NotGraded,
value: 0,
};

return {
disabled: value.kind === GradeKind.Error,
information: decodeGradeKind(value.kind),
value: value?.points,
};
};

const getGradeValue = (value: number | string | undefined): GradeValue => {
return {
disabled: false,
Expand Down
6 changes: 5 additions & 1 deletion src/services/ecoledirecte/homework.ts
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,10 @@ import { log } from "@/utils/logger/logger";
import { AttachmentType } from "../shared/Attachment";
import { formatDate } from "@/services/ecoledirecte/format-date";

const cleanContent = (message: string) => {
return message.replace(/>\s+/g, '>').replace(/&nbsp;/g, ' ');
}

export const getHomeworkForWeek = async (
account: EcoleDirecteAccount,
weekNumber: number,
Expand Down Expand Up @@ -36,7 +40,7 @@ export const getHomeworkForWeek = async (
name: att.name,
})),
color: "#000000", // TODO
content: homework.content,
content: cleanContent(homework.content),
done: homework.done,
due: date.getTime(),
id: homework.id.toString(),
Expand Down
113 changes: 78 additions & 35 deletions src/services/ecoledirecte/time-interval.ts
Original file line number Diff line number Diff line change
Expand Up @@ -6,49 +6,76 @@ export type Timeinterval = {
const months = ["janvier", "février", "mars", "avril", "mai", "juin", "juillet", "août", "septembre", "octobre", "novembre", "décembre"];

/**
* @param str "A string containing a date formatted as "mercredi 21 février 2024 de 00:00", will return the date for the specified day and hour"
*/
export function dateAsISO860 (str: string): string {
* @param str "A string containing a date formatted as "mercredi 21 février 2024 de 08:10", will return the date for the specified day and hour"
*/
export function dateAsISO860(str: string): string {
const parts = str.split(" ");
const hour = parts[5].split(":");
return (new Date(Number(parts[3]), months.indexOf(parts[2]), Number(parts[1]), Number(hour[0]), Number(hour[1]))).toISOString();
const timeIndex = parts.findIndex(part => part.includes(":"));
const hour = parts[timeIndex].split(":");
const monthIndex = parts.findIndex(part => months.includes(part));

return (new Date(
Number(parts[monthIndex + 1]),
months.indexOf(parts[monthIndex]),
Number(parts[monthIndex - 1]),
Number(hour[0]),
Number(hour[1])
)).toISOString();
}

export function dateStringAsTimeInterval (
export function dateStringAsTimeInterval(
str: string,
): Timeinterval | undefined {
if (str.includes("du")) {
/**
* @example
* str is equal to "du mercredi 21 février 2024 au jeudi 22 février 2024"
*/
const parts = str.split("au");
const start = dateAsISO860(parts[0].replace("du", "").trim());
const end = dateAsISO860(parts[1].trim());
return { start: start, end: end } as Timeinterval;
/**
* @example
* str is equal to "du mercredi 21 février 2024 au jeudi 22 février 2024"
* or "du mercredi 27 novembre 2024 à 08:10 au vendredi 06 décembre 2024 à 08:10"
*/
const [startPart, endPart] = str.split("au").map(part => part.trim());
let start = startPart.replace("du", "").trim();
let end = endPart.trim();

if (!start.includes(":")) {
start += " de 00:00";
} else {
start = start.replace(" à ", " de ");
}

if (!end.includes(":")) {
end += " de 23:59";
} else {
end = end.replace(" à ", " de ");
}

return {
start: dateAsISO860(start),
end: dateAsISO860(end)
} as Timeinterval;
}

if (str.includes("le")) {
/**
* @example
* str is equal to "le mercredi 21 février 2024 de 08:55 à 09:45"
* or "le mercredi 21 février 2024"
*/
const parts = str.split("à");

let startDate: string;
let endDate: string;

// C'est une journée complète ("le mercredi 21 février 2024")
if (!str.includes(":")) {
startDate = `${parts[0].replace("le", "").trim()} de 00:00`;
endDate = `${parts[0].split("de")[0].replace("le", "").trim()} de 23:59`;
} else {
startDate = parts[0].replace("le", "").trim();
endDate = `${parts[0].split("de")[0].replace("le", "").trim()} de ${parts[1].trim()}`;
}
const start = dateAsISO860(startDate);
const end = dateAsISO860(endDate);
return { start: start, end: end } as Timeinterval;
/**
* @example
* str is equal to "le mercredi 21 février 2024 de 08:10 à 16:10"
* or "le jeudi 22 février 2024"
*/
const parts = str.split("à");
let startDate: string;
let endDate: string;

// It's a full day ("le mercredi 21 février 2024")
if (!str.includes(":")) {
startDate = `${parts[0].replace("le", "").trim()} de 00:00`;
endDate = `${parts[0].split("de")[0].replace("le", "").trim()} de 23:59`;
} else {
startDate = parts[0].replace("le", "").trim();
endDate = `${parts[0].split("de")[0].replace("le", "").trim()} de ${parts[1].trim()}`;
}

const start = dateAsISO860(startDate);
const end = dateAsISO860(endDate);
return { start: start, end: end } as Timeinterval;
}
return undefined;
}
Expand All @@ -60,3 +87,19 @@ export function dateStringAsTimeInterval (
export const getDuration = (interval: Timeinterval): Date => {
return new Date(new Date(interval.end).getTime() - new Date(interval.start).getTime());
};

/**
* Get the duration of interval in hours.
* @param interval
*/
/**
* Get the duration of interval in hours.
* @param interval
*/
export const getDurationInHours = (interval: Timeinterval): string => {
const diff = new Date(interval.end).getTime() - new Date(interval.start).getTime();
const hours = Math.floor(diff / (1000 * 60 * 60));
const minutes = Math.floor((diff % (1000 * 60 * 60)) / (1000 * 60));

return `${hours}h${minutes.toString().padStart(2, '0')}`;
};
6 changes: 5 additions & 1 deletion src/services/ecoledirecte/timetable.ts
Original file line number Diff line number Diff line change
Expand Up @@ -18,7 +18,7 @@ const decodeTimetableClass = (c: ecoledirecte.TimetableItem): TimetableClass =>
subject: c.subjectName,
title: c.subjectName,
room: c.room || void 0,
teacher: c.teacher ?? void 0,
teacher: c.teacher || void 0,
// TODO: add more states
status: c.updated ? TimetableClassStatus.MODIFIED : c.cancelled ? TimetableClassStatus.CANCELED : void 0,
statusText: c.updated ? TimetableClassStatus.MODIFIED : c.cancelled ? TimetableClassStatus.CANCELED : void 0,
Expand All @@ -30,6 +30,7 @@ const decodeTimetableClass = (c: ecoledirecte.TimetableItem): TimetableClass =>
subject: c.subjectName,
id: c.id,
title: c.subjectName ?? "Sans titre",
teacher: c.teacher || void 0,
room: c.room || void 0,
...base
};
Expand All @@ -39,6 +40,7 @@ const decodeTimetableClass = (c: ecoledirecte.TimetableItem): TimetableClass =>
subject: c.subjectName,
id: c.id,
title: c.subjectName ?? "Sans titre",
teacher: c.teacher || void 0,
room: c.room || void 0,
...base
};
Expand All @@ -48,6 +50,7 @@ const decodeTimetableClass = (c: ecoledirecte.TimetableItem): TimetableClass =>
subject: c.subjectName,
id: c.id,
title: "Congés",
teacher: c.teacher || void 0,
room: void 0,
...base
};
Expand All @@ -57,6 +60,7 @@ const decodeTimetableClass = (c: ecoledirecte.TimetableItem): TimetableClass =>
subject: "",
id: c.id,
title: "Sanction",
teacher: c.teacher || void 0,
room: "PERMANENCE",
...base
};
Expand Down
8 changes: 5 additions & 3 deletions src/views/account/Grades/Grades.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -118,9 +118,11 @@ const Grades: Screen<"Grades"> = ({ route, navigation }) => {
});
}

gradesPerSubject.sort((a, b) =>
a.average.subjectName.localeCompare(b.average.subjectName)
);
if (account.service !== AccountService.EcoleDirecte) {
gradesPerSubject.sort((a, b) =>
a.average.subjectName.localeCompare(b.average.subjectName)
);
}
setGradesPerSubject(gradesPerSubject);
}, 1);
}, [selectedPeriod, averages, grades]);
Expand Down
4 changes: 2 additions & 2 deletions src/views/account/Grades/Subject/SubjectList.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -135,9 +135,9 @@ const SubjectGradeItem: React.FC<SubjectGradeItemProps> = ({ subject, grade, ind
fontFamily: "medium",
}}
>
{typeof grade.student.value === "number"
{grade.student.disabled === true ? "N. not" : (typeof grade.student.value === "number"
? grade.student.value.toFixed(2)
: "N. not"}
: "N. not")}
</NativeText>
<NativeText
style={{
Expand Down
6 changes: 3 additions & 3 deletions src/views/account/Lessons/Atoms/Page.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -188,7 +188,7 @@ const SeparatorCourse: React.FC<{
}}
/>

{startHours > 11 &&
{startHours >= 11 &&
startHours < 14 ? (
<Utensils size={20} color={colors.text} />
) : (
Expand All @@ -203,8 +203,8 @@ const SeparatorCourse: React.FC<{
color: colors.text,
}}
>
{(startHours > 11 &&
startHours < 14)
{startHours >= 11 &&
startHours < 14
? "Pause méridienne"
: "Pas de cours"}
</Text>
Expand Down

0 comments on commit ef3de10

Please sign in to comment.