mirror of
https://github.com/dd060606/WebAurion-API.git
synced 2026-01-18 16:47:26 +01:00
feat: can get schedule using a week number
This commit is contained in:
@@ -1,5 +1,8 @@
|
||||
import { getJSFFormParams, getViewState } from "../utils/AurionUtils";
|
||||
import { planningResponseToEvents } from "../utils/PlanningUtils";
|
||||
import {
|
||||
getScheduleDates,
|
||||
planningResponseToEvents,
|
||||
} from "../utils/PlanningUtils";
|
||||
import { PlanningEvent } from "../utils/types";
|
||||
import Session from "./Session";
|
||||
|
||||
@@ -10,7 +13,7 @@ class PlanningApi {
|
||||
}
|
||||
|
||||
// Récupération de l'emploi du temps en fonction de la date de début et de fin (timestamps en millisecondes)
|
||||
public fetchPlanning(startDate?: string): Promise<PlanningEvent[]> {
|
||||
public fetchPlanning(weeksFromNow?: number): Promise<PlanningEvent[]> {
|
||||
return new Promise<PlanningEvent[]>(async (resolve, reject) => {
|
||||
try {
|
||||
// On récupère le ViewState pour effectuer la requête
|
||||
@@ -22,34 +25,12 @@ class PlanningApi {
|
||||
"j_idt118",
|
||||
viewState,
|
||||
);
|
||||
if (startDate) {
|
||||
params.append("form:j_idt118_start", startDate);
|
||||
// La date de fin est fixée à une semaine après la date de début
|
||||
const endDate = startDate + 6 * 24 * 60 * 60 * 1000;
|
||||
params.append("form:j_idt118_end", endDate);
|
||||
} else {
|
||||
const now = new Date();
|
||||
// Obtenir le jour actuel (0 = dimanche, 1 = lundi, ..., 6 = samedi)
|
||||
let day = now.getDay();
|
||||
// Calculer la différence pour atteindre lundi (0 = dimanche => 1 jour pour atteindre lundi)
|
||||
const daysToMonday = day === 0 ? 1 : 1 - day;
|
||||
// Créer la date de début (lundi 6h00)
|
||||
const startDate = new Date(now);
|
||||
startDate.setDate(now.getDate() + daysToMonday); // Passer au lundi
|
||||
startDate.setHours(6, 0, 0, 0); // Fixer à 6h00
|
||||
// Date de fin (6 jours après la date de début)
|
||||
const endDate = new Date(startDate);
|
||||
endDate.setDate(startDate.getDate() + 6); // Ajouter 6 jours
|
||||
// Convertir les dates en timestamp
|
||||
const startTimestamp = startDate.getTime();
|
||||
const endTimestamp = endDate.getTime();
|
||||
|
||||
params.append(
|
||||
"form:j_idt118_start",
|
||||
startTimestamp.toString(),
|
||||
);
|
||||
params.append("form:j_idt118_end", endTimestamp.toString());
|
||||
}
|
||||
//On récupère les dates de début et de fin de l'emploi du temps
|
||||
let { startTimestamp, endTimestamp } =
|
||||
getScheduleDates(weeksFromNow);
|
||||
params.append("form:j_idt118_start", startTimestamp.toString());
|
||||
params.append("form:j_idt118_end", endTimestamp.toString());
|
||||
|
||||
const response = await this.session.sendPOST<string>(
|
||||
"faces/Planning.xhtml",
|
||||
|
||||
@@ -46,3 +46,26 @@ export function planningResponseToEvents(response: string): PlanningEvent[] {
|
||||
};
|
||||
});
|
||||
}
|
||||
|
||||
// On récupère les dates de début et de fin de l'emploi du temps (par défaut, la semaine actuelle: 0)
|
||||
export function getScheduleDates(weeksFromNow: number = 0): {
|
||||
startTimestamp: number;
|
||||
endTimestamp: number;
|
||||
} {
|
||||
const now = new Date();
|
||||
// Obtenir le jour actuel (0 = dimanche, 1 = lundi, ..., 6 = samedi)
|
||||
let day = now.getDay();
|
||||
// Calculer la différence pour atteindre lundi
|
||||
const daysToMonday = day === 0 ? 1 : 1 - day;
|
||||
// Créer la date de début (lundi 6h00)
|
||||
const startDate = new Date(now);
|
||||
startDate.setDate(now.getDate() + daysToMonday + weeksFromNow * 7); // Passer au lundi de la semaine correspondante
|
||||
startDate.setHours(6, 0, 0, 0); // Fixer à 6h00
|
||||
// Date de fin (dimanche de la même semaine, 6 jours après le début)
|
||||
const endDate = new Date(startDate);
|
||||
endDate.setDate(startDate.getDate() + 6); // Ajouter 6 jours
|
||||
// Convertir les dates en timestamp
|
||||
const startTimestamp = startDate.getTime();
|
||||
const endTimestamp = endDate.getTime();
|
||||
return { startTimestamp, endTimestamp };
|
||||
}
|
||||
|
||||
@@ -1,4 +1,5 @@
|
||||
import { login } from "../src/api/Session";
|
||||
import { getScheduleDates } from "../src/utils/PlanningUtils";
|
||||
describe("PlanningApi", () => {
|
||||
it("should receive a planning", async () => {
|
||||
const username = process.env.TEST_USERNAME;
|
||||
@@ -14,4 +15,12 @@ describe("PlanningApi", () => {
|
||||
console.log(planning);
|
||||
expect(planning).toBeInstanceOf(Array);
|
||||
});
|
||||
it("should get schedule dates from week number", async () => {
|
||||
const { startTimestamp, endTimestamp } = getScheduleDates(3);
|
||||
console.log(
|
||||
new Date(startTimestamp).toLocaleString(),
|
||||
new Date(endTimestamp).toLocaleString(),
|
||||
);
|
||||
expect(startTimestamp).toBeLessThan(endTimestamp);
|
||||
});
|
||||
});
|
||||
|
||||
Reference in New Issue
Block a user