@@ -5,41 +5,73 @@ import { absenceFileStateIncluded, absenceReasonIncluded } from "../types/Attend
55import { BaseDataResponse , BaseResponse } from "../types/RequestHandler" ;
66import { AttendanceItemState , AttendanceItemType } from "../util/Constants" ;
77import { getSingleRelation } from "../util/Relations" ;
8+ import { extractBaseUrl } from "../util/URL" ;
89
9- const manager = new RestManager ( BASE_URL ( ) ) ;
1010
11- export const GetAttendanceItems = async ( userId : string , accessToken : string ) : Promise < Array < AttendanceItem > > => {
12- const response = await manager . get < BaseResponse > ( ATTENDANCE_FILES ( ) , {
13- "filter[student.id]" : userId ,
14- "filter[currentState.absenceType]" : "ABSENCE,CLATENESS" ,
15- "filter[absenceFile]" : "currentState" ,
16- "include" : "currentState,currentState.absenceReason,currentState.absenceRecurrence" ,
17- "fields[absenceFileState]" : "creationDateTime,absenceStartDateTime,absenceEndDateTime,absenceType,absenceFileStatus,absenceReason,absenceRecurrence" ,
18- "fields[absenceReason]" : "code,longLabel"
19- } , {
20- Authorization : `Bearer ${ accessToken } `
21- } ) ;
22-
23- const includedMap = new Map < string , unknown > ( ) ;
24- for ( const item of response . included ?? [ ] ) {
25- includedMap . set ( `${ item . type } :${ item . id } ` , item ) ;
11+ function parseDate ( date : string ) : Date {
12+ const clean = date . replace ( / ^ \w + \. \s * / , "" )
13+ const months : Record < string , number > = {
14+ "janvier" : 0 , "février" : 1 , "mars" : 2 ,
15+ "avril" : 3 , "mai" : 4 , "juin" : 5 ,
16+ "juillet" : 6 , "août" : 7 , "septembre" : 8 ,
17+ "octobre" : 9 , "novembre" : 10 , "décembre" : 11
18+ } ;
19+ const match = clean . match ( / ^ ( \d { 1 , 2 } ) ( \w + ) ( \d { 4 } ) $ / ) ;
20+ if ( ! match ) {
21+ throw new Error ( `Invalid date format: ${ date } ` ) ;
22+ }
23+ const [ , day , month , year ] = match ;
24+ const monthIndex = months [ month . toLowerCase ( ) ] ;
25+ if ( monthIndex === undefined ) {
26+ throw new Error ( `Invalid month name: ${ month } ` ) ;
2627 }
28+ return new Date ( Number ( year ) , monthIndex , Number ( day ) ) ;
29+ }
30+
31+ export const GetAttendanceItems = async ( url :string , userId : string , accessToken : string , mobileId : string ) : Promise < Array < AttendanceItem > > => {
32+ const [ base ] = extractBaseUrl ( url ) ;
33+ const manager = new RestManager ( base ) ;
34+
35+ const body = new URLSearchParams (
36+ { pupilID : userId . split ( "_" ) [ 1 ] }
37+ ) ;
38+ const responsetext = await manager . post < any > (
39+ ATTENDANCE_FILES ( ) ,
40+ body ,
41+ undefined ,
42+ {
43+ headers : {
44+ "Content-Type" : "application/x-www-form-urlencoded" ,
45+ "Authorization" : `Bearer ${ accessToken } ` ,
46+ "Accept-Language" : "fr" ,
47+ "SmscMobileId" : mobileId
48+ }
49+ } ,
50+ true
51+ ) ;
2752
28- return ( Array . isArray ( response . data ) ? response . data : [ ] )
29- . filter ( ( item ) : item is BaseDataResponse < "absenceFile" > => item . type === "absenceFile" )
30- . map ( absenceItem => {
31- const fileId = getSingleRelation ( absenceItem . relationships . currentState ) ?. id ;
32- const file = fileId ? includedMap . get ( "absenceFileState:" + fileId ) as absenceFileStateIncluded : null ;
33- const reasonId = getSingleRelation ( file ?. relationships ?. absenceReason ) ?. id ;
34- const reason = reasonId ? includedMap . get ( "absenceReason:" + reasonId ) as absenceReasonIncluded : null ;
35- return new AttendanceItem (
36- absenceItem . id ,
37- new Date ( file ?. attributes ?. creationDateTime ?? "" ) ,
38- new Date ( file ?. attributes ?. absenceStartDateTime ?? "" ) ,
39- new Date ( file ?. attributes ?. absenceEndDateTime ?? "" ) ,
40- file ?. attributes ?. absenceType ?? AttendanceItemType . ABSENCE ,
41- file ?. attributes ?. absenceFileStatus ?? AttendanceItemState . OPEN ,
42- reason ?. attributes ?. longLabel ?? ""
43- ) ;
44- } ) ;
53+ const response = JSON . parse ( responsetext ) ;
54+ const attendanceItems : Array < AttendanceItem > = [ ] ;
55+
56+ const currentYear = response . year_label ;
57+ const currentYearAttendance = response . presences ?. [ currentYear ] || [ ] ;
58+
59+ for ( const attendance of currentYearAttendance ) {
60+ const attendanceData = attendance . am || attendance . pm || attendance . full ;
61+
62+ if ( attendanceData ) {
63+ const date = parseDate ( attendanceData . formattedDate ) ;
64+
65+ attendanceItems . push ( new AttendanceItem (
66+ attendanceData . codeKey || "" ,
67+ date ,
68+ new Date ( date ) , // Start date
69+ new Date ( date ) , // End date
70+ attendanceData . codeDescr || AttendanceItemType . ABSENCE ,
71+ AttendanceItemState . OPEN ,
72+ attendanceData . motivation || attendanceData . codeDescr || ""
73+ ) ) ;
74+ }
75+ }
76+ return attendanceItems ;
4577} ;
0 commit comments