Make registerAbscence work with Hjärntorget

This changes teh registerAbscence api to use DateTime instead of Date,
mainly to be consistent with getSchedule that already used DateTime.

The main fixes that was missing is properly formatting the start and end
dates sent to the server and ensuring a `Referer` header is included
similar to what is sent in the browser.
This commit is contained in:
Emil Lindqvist 2021-12-15 09:43:04 +01:00
parent 411053c5d0
commit de6aef638e
4 changed files with 22 additions and 18 deletions

View File

@ -5,6 +5,7 @@ import {
Classmate,
CookieManager,
EtjanstChild,
Fetch,
Fetcher,
FetcherOptions,
LoginStatusChecker,
@ -85,7 +86,7 @@ export class ApiHjarntorget extends EventEmitter implements Api {
}
constructor(
fetch: typeof global.fetch,
fetch: Fetch,
cookieManager: CookieManager,
options?: FetcherOptions
) {
@ -497,7 +498,7 @@ export class ApiHjarntorget extends EventEmitter implements Api {
emitter.emit('OK')
this.emit('login')
}, 50)
return emitter;
return emitter as unknown as LoginStatusChecker;
}
console.log('prepping??? shibboleth')
@ -572,20 +573,23 @@ export class ApiHjarntorget extends EventEmitter implements Api {
return statusChecker
}
public async registerAbscense(child: EtjanstChild, startDate: Date, endDate: Date): Promise<void> {
public async registerAbscense(child: EtjanstChild, startDate: DateTime, endDate: DateTime): Promise<void> {
const body = {
attendeeId: child.id,
startDate: startDate.toFormat("yyyy-MM-dd HH:mm"),
endDate: endDate.toFormat("yyyy-MM-dd HH:mm"),
statusId: 27433608,
_submit: 'Save'
}
const body = {
attendeeId: child.id,
startDate: startDate,
endDate: endDate,
statusId: 27433608,
_submit: 'Register+the+whole+day'
}
this.fetch('register-abscense', abscenseRegistrationUrl, {
method: 'POST',
body: new URLSearchParams(body).toString(),
})
await this.fetch('register-abscense', abscenseRegistrationUrl, {
method: 'POST',
headers: {
'Content-Type': 'application/x-www-form-urlencoded',
'Referer': "https://hjarntorget.goteborg.se/attendanceParentRegisterAbsence.do?attendeeId=" + child.id,
},
body: new URLSearchParams(body).toString(),
})
}
private async fakeMode(): Promise<LoginStatusChecker> {

View File

@ -19,7 +19,7 @@ const init = (
const cookieManager = ((cookieManagerImpl as RNCookieManager).get)
? wrapReactNativeCookieManager(cookieManagerImpl as RNCookieManager)
: wrapToughCookie(cookieManagerImpl as ToughCookieJar)
return new ApiHjarntorget(fetchImpl as any, cookieManager, options)
return new ApiHjarntorget(fetchImpl, cookieManager, options)
}
export default init

View File

@ -79,7 +79,7 @@ export class ApiSkolplattformen extends EventEmitter implements Api {
this.cookieManager = cookieManager
this.headers = {}
}
registerAbscense(child: EtjanstChild, startDate: Date, endDate: Date): Promise<boolean> {
registerAbscense(child: EtjanstChild, startDate: DateTime, endDate: DateTime): Promise<void> {
throw new Error('Method not implemented.')
}

View File

@ -32,6 +32,6 @@ export interface Api extends EventEmitter {
getSchedule(child: EtjanstChild, from: DateTime, to: DateTime): Promise<ScheduleItem[]>
getSkola24Children(): Promise<Skola24Child[]>
getTimetable(child: Skola24Child, week: number, year: number, lang: Language): Promise<TimetableEntry[]>
registerAbscense(child: EtjanstChild, startDate: Date, endDate: Date): Promise<void>
registerAbscense(child: EtjanstChild, startDate: DateTime, endDate: DateTime): Promise<void>
logout(): Promise<void>
}