@@ -6,57 +6,41 @@ import { NewsAttributes } from "../types/News";
66import { BaseDataResponse , BaseResponse , fileIncluded } from "../types/RequestHandler" ;
77import { schoolIncluded } from "../types/School" ;
88import { getSingleRelation } from "../util/Relations" ;
9-
10- const manager = new RestManager ( BASE_URL ( ) ) ;
11-
12- export const GetSchoolNews = async ( accessToken : string ) : Promise < Array < News > > => {
13- const response = await manager . get < BaseResponse > ( SCHOOL_NEWS ( ) , {
14- "include" : "illustration,school,author,author.person,author.technicalUser" ,
15- "fields[schoolInfo]" : "illustration,school,author,publicationDateTime,title,shortContent,content,linkedWebSiteUrl" ,
16- "fields[announcement]" : "level" ,
17- "fields[schoolInfoFile]" : "url,alternativeText" ,
18- "fields[school]" : "name" ,
19- "fields[schoolInfoAuthor]" : "person,technicalUser,additionalInfo" ,
20- "fields[person]" : "firstName,lastName,title,photoUrl" ,
21- "fields[schoolInfoTechnicalUser]" : "label,logoUrl" ,
22- "page[limit]" : 50
23- } , {
24- Authorization : `Bearer ${ accessToken } `
25- } ) ;
26-
27- const includedMap = new Map < string , unknown > ( ) ;
28- for ( const item of response . included ?? [ ] ) {
29- includedMap . set ( `${ item . type } :${ item . id } ` , item ) ;
30- }
31-
32- return ( Array . isArray ( response . data ) ? response . data : [ ] )
33- . filter ( ( item ) : item is BaseDataResponse < "news" , NewsAttributes > => item . type === "news" )
9+ import { extractBaseUrl } from "../util/URL" ;
10+
11+
12+ export const GetSchoolNews = async ( url : string , accessToken : string , mobileId : string ) : Promise < Array < News > > => {
13+ const [ base ] = extractBaseUrl ( url ) ;
14+ const manager = new RestManager ( base ) ;
15+
16+ const responsetext = await manager . post < any > ( SCHOOL_NEWS ( ) ,
17+ undefined ,
18+ undefined ,
19+ {
20+ headers : {
21+ "Authorization" : `Bearer ${ accessToken } ` ,
22+ "Accept-Language" : "fr" ,
23+ "SmscMobileId" : mobileId
24+ }
25+ } ,
26+ true
27+ ) ;
28+ const response = JSON . parse ( responsetext ) ;
29+
30+ return ( Array . isArray ( response ) ? response : [ ] )
3431 . map ( news => {
35- const { relationships, attributes } = news ;
36-
37- const illustrationId = getSingleRelation ( relationships . illustration ) ?. id ;
38- const authorData = getSingleRelation ( relationships . school ) ;
39-
40- const illustration = illustrationId
41- ? includedMap . get ( `schoolInfoFile:${ illustrationId } ` ) as fileIncluded
42- : null ;
43-
44- const author = authorData
45- ? includedMap . get ( `${ authorData . type } :${ authorData . id } ` ) as schoolIncluded
46- : null ;
47-
4832 return new News (
49- news . id ,
50- new Date ( attributes ?. publicationDateTime ?? "" ) ,
51- attributes ? .title ?? "" ,
52- attributes ?. shortContent ?? "" ,
53- attributes ?. content ?? "" ,
33+ news . newsItem . newsID ,
34+ new Date ( news . newsItem . date_published ?? "" ) ,
35+ news . newsItem . title ?? "" ,
36+ news . newsItem . message ?? "" ,
37+ news . newsItem . message ?? "" ,
5438 {
55- id : authorData ?. id ?? "" ,
56- name : author ?. attributes ? .name ?? ""
39+ id : news . newsItem . author ?? "" ,
40+ name : news . newsItem . name ?? ""
5741 } ,
58- attributes ?. linkedWebSiteUrl ?? "" ,
59- new Attachment ( accessToken , illustration ?. id ?? "" , illustration ?. attributes ?. url ?? "" )
42+ "" ,
43+ new Attachment ( accessToken , news . icon ?? "" , ` ${ base } /smsc/svg/ ${ news . icon } / ${ news . icon } _24x24.svg` )
6044 ) ;
6145 } ) ;
6246} ;
0 commit comments