@@ -22,11 +22,17 @@ const CALENDAR_FILE = new File(Paths.document, 'calendar.json');
2222const ONGOING_THRESHOLD_MS = 15 * 60 * 1000 ;
2323const ADE_PROJECT_OVERRIDE_KEY = 'adeProjectOverride' ;
2424
25- function getAcademicYearDateRange ( ) : string {
26- const now = new Date ( ) ;
27- const month = now . getMonth ( ) + 1 ;
28- const year = now . getFullYear ( ) ;
29- const startYear = month >= 9 ? year : year - 1 ;
25+ function getAcademicYearDateRange ( projectName ?: string ) : string {
26+ const match = projectName ?. match ( / ( \d { 4 } ) - ( \d { 4 } ) / ) ;
27+ let startYear : number ;
28+ if ( match ) {
29+ startYear = Number ( match [ 1 ] ) ;
30+ } else {
31+ const now = new Date ( ) ;
32+ const month = now . getMonth ( ) + 1 ;
33+ const year = now . getFullYear ( ) ;
34+ startYear = month >= 9 ? year : year - 1 ;
35+ }
3036 const endYear = startYear + 1 ;
3137 return `&firstDate=${ startYear } -09-01&lastDate=${ endYear } -08-31` ;
3238}
@@ -81,6 +87,14 @@ function pickAutoProjectId(projects: AdeProject[]): string | null {
8187 return match ?. id ?? null ;
8288}
8389
90+ async function disconnectSession ( sessionId : string ) : Promise < void > {
91+ try {
92+ await fetch ( `${ ADE_BASE } /jsp/webapi?function=disconnect&sessionId=${ sessionId } ` ) ;
93+ } catch {
94+ // la session expirera d'elle-même côté serveur
95+ }
96+ }
97+
8498async function getCalendarFromCache ( ) : Promise < CalendarEvent [ ] > {
8599 try {
86100 const json = await CALENDAR_FILE . text ( ) ;
@@ -94,18 +108,17 @@ export class EDT {
94108 private ADE_PROJECT : string | null = null ;
95109 private isOverridden = false ;
96110 private projects : AdeProject [ ] = [ ] ;
97- private readonly ADE_DATE : string ;
98111 private readonly _ready : Promise < void > ;
99112
100113 constructor ( ) {
101- this . ADE_DATE = getAcademicYearDateRange ( ) ;
102114 this . _ready = this . resolveProject ( ) ;
103115 }
104116
105117 private async resolveProject ( ) : Promise < void > {
106118 const sessionId = await fetchSessionId ( ) ;
107119 if ( ! sessionId ) return ;
108120 this . projects = await fetchProjects ( sessionId ) ;
121+ await disconnectSession ( sessionId ) ;
109122
110123 const override = await getAsync ( ADE_PROJECT_OVERRIDE_KEY ) ;
111124 if ( override ) {
@@ -154,9 +167,11 @@ export class EDT {
154167 await this . waitUntilReady ( ) ;
155168 if ( ! this . ADE_PROJECT ) return null ;
156169 try {
170+ const projectName = this . projects . find ( ( p ) => p . id === this . ADE_PROJECT ) ?. name ;
171+ const dateRange = getAcademicYearDateRange ( projectName ) ;
157172 const url =
158173 `${ ADE_BASE } /jsp/custom/modules/plannings/anonymous_cal.jsp` +
159- `?code=${ adeid } &projectId=${ this . ADE_PROJECT } &calType=ical${ this . ADE_DATE } ` ;
174+ `?code=${ adeid } &projectId=${ this . ADE_PROJECT } &calType=ical${ dateRange } ` ;
160175 const res = await fetch ( url ) ;
161176 if ( ! res . ok ) return null ;
162177 return res . text ( ) ;
0 commit comments