Skip to content

Commit

Permalink
wip: fetch more data
Browse files Browse the repository at this point in the history
  • Loading branch information
schweinryder committed Oct 6, 2023
1 parent 6130fdb commit 9276db3
Show file tree
Hide file tree
Showing 4 changed files with 152 additions and 20 deletions.
4 changes: 2 additions & 2 deletions apps/api-test-app/src/main.js
Original file line number Diff line number Diff line change
Expand Up @@ -68,11 +68,11 @@ async function run() {
const menu = await api.getMenu(children[0])
console.log(menu)

/*

console.log('calendar')
const calendar = await api.getCalendar(children[0])
console.log(calendar)
/*
/*console.log('classmates')
const classmates = await api.getClassmates(children[0])
console.log(classmates)
Expand Down
165 changes: 148 additions & 17 deletions libs/api-admentum/lib/apiAdmentum.ts
Original file line number Diff line number Diff line change
Expand Up @@ -191,11 +191,100 @@ export class ApiAdmentum extends EventEmitter implements Api {
}) as Skola24Child & EtjanstChild);
}



async getCalendar(child: EtjanstChild): Promise<CalendarItem[]> {
try {
if (!this.isLoggedIn) {
throw new Error('Not logged in...')
}
const now = DateTime.local()
const [year, week] = now.toISOWeekDate().split('-')
const isoWeek = week.replace('W','')

const fetchUrl = apiUrls.overview(year.toString(), isoWeek.toString())
console.log('fetching calendar', fetchUrl)
//const calendarResponse = await this.fetch('get-calendar', fetchUrl)
//const calendarResponseJson = await calendarResponse.json()
const overviewResponse = await this.fetch('get-overview', fetchUrl)
console.log('overview response', overviewResponse)
const overviewJson = await overviewResponse.json()
console.log('overview response', overviewJson)
const schedule_events = (await overviewJson)?.data?.schedule_events // .breaks: [] | .assignments: []
if (!schedule_events) {
return Promise.resolve([])
}
/*
"url": "https://skola.admentum.se/api/v1/schedule_event_instances/2990834/",
"id": 2990834,
"school_id": 824,
"start_date": "2023-08-07",
"end_date": "2023-08-07",
"schedule_event": {
"url": "https://skola.admentum.se/api/v1/schedule_events/148722/",
"id": 148722,
"eid": null,
"schedule_id": 4385,
"name": "Engelska",
"start_time": "08:00:00",
"end_time": "09:30:00",
"rooms": [
{
"url": "https://skola.admentum.se/api/v1/rooms/7200/",
"id": 7200
}
],
"teachers": [
{
"url": "https://skola.admentum.se/api/v1/users/437302/",
"id": 437302
}
],
"schedule_groups": [],
"primary_groups": [
{
"url": "https://skola.admentum.se/api/v1/primary_groups/36874/",
"id": 36874
}
],
"weekly_interval": ""
}
*/
return Promise.resolve(schedule_events.map(({ menu, date } : any) => ({
title: date,
description: menu
})))
} catch (e) {
console.error('Error fetching menu', e)
return Promise.resolve([])
}
}

async getScheduledEvents(child: EtjanstChild): Promise<CalendarItem[]> {
if (!this.isLoggedIn) {
throw new Error('Not logged in...')
}
console.log('get calendar')
const fetchUrl = apiUrls.schedule_events;
console.log('fetching calendar', fetchUrl)
const eventsResponse = await this.fetch('scheduled-events', fetchUrl, {
method: 'GET',
headers: {
'Accept': 'application/json, text/plain, */*',
},
})

if (eventsResponse.status === 403) {
console.error('Not allwed. Error headers', eventsResponse.headers)
return []
}
if (eventsResponse.status !== 200) {
console.error('Error headers', eventsResponse.headers)
throw new Error('Could not fetch children. Response code: ' + eventsResponse.status)
}

const eventsResponseJson = await eventsResponse.json();
console.log('eventsResponseJson', eventsResponseJson)
return []
// const fetchUrl = apiUrls.schedule_events
// const events = await this.fetch('scheduled-events', fetchUrl, {
Expand All @@ -214,6 +303,38 @@ export class ApiAdmentum extends EventEmitter implements Api {
// eslint-disable-next-line @typescript-eslint/no-unused-vars
getClassmates(_child: EtjanstChild): Promise<Classmate[]> {
// TODO: We could get this from the events a child is associated with...
/*
GET /api/v1/schedule_groups/423145/
{
"url": "https://skola.admentum.se/api/v1/schedule_groups/423145/",
"id": 423145,
"eid": null,
"schedule": {
"url": "https://skola.admentum.se/api/v1/schedules/4385/",
"id": 4385,
"school_year": "23/24"
},
"name": "1 A SV",
"guid": null,
"users": [
{
"url": "https://skola.admentum.se/api/v1/users/436741/",
"id": 436741,
"email": null,
"first_name": "Arvid",
"last_name": "Forslin",
"role": 1
},
{
"url": "https://skola.admentum.se/api/v1/users/436747/",
"id": 436747,
"email": null,
"first_name": "Emmy",
"last_name": "Granström",
"role": 1
}
...
*/
if (!this.isLoggedIn) {
throw new Error('Not logged in...')
}
Expand Down Expand Up @@ -268,24 +389,34 @@ export class ApiAdmentum extends EventEmitter implements Api {
*/
// eslint-disable-next-line @typescript-eslint/no-unused-vars
async getMenu(_child: EtjanstChild): Promise<MenuItem[]> {
if (!this.isLoggedIn) {
throw new Error('Not logged in...')
try {
if (!this.isLoggedIn) {
throw new Error('Not logged in...')
}
const now = DateTime.local()
const [year, week] = now.toISOWeekDate().split('-')
const isoWeek = week.replace('W', '')

const fetchUrl = apiUrls.menu(year.toString(), isoWeek.toString())
console.log('fetching menu', fetchUrl)
const menuResponse = (await this.fetch('get-menu', fetchUrl))
console.log('menu response', menuResponse)
const menuResponseJson = await menuResponse.text()
console.log('menu response', menuResponseJson)
const days = (await menuResponseJson)
//if (!days) {
return Promise.resolve([])
//}
/*return Promise.resolve(
days.map(({ menu, date }: any) => ({
title: date,
description: menu,
}))
)*/
} catch (e) {
console.error('Error fetching menu', e)
return Promise.resolve([])
}
const now = DateTime.local()
const [year, week] = now.toISOWeekDate().split('-')
const isoWeek = week.replace('W','')

const fetchUrl = apiUrls.menu(year.toString(), isoWeek.toString())

const menuResponse = (await this.fetch('get-menu', fetchUrl))
const menuResponseJson = await menuResponse.json()
console.log('menu response', menuResponseJson)
const days = (await menuResponseJson)?.data?.food_week?.food_days

return Promise.resolve(days.map(({ menu, date } : any) => ({
title: date,
description: menu
})))
}

async getChildEventsWithAssociatedMembers(child: EtjanstChild) {
Expand Down
2 changes: 1 addition & 1 deletion libs/api-admentum/lib/parse/parsers.ts
Original file line number Diff line number Diff line change
Expand Up @@ -94,7 +94,7 @@ export const parseCalendarItem = (jsonData: any): any => {
timeStart: lesson.time.substring(0, 5),
timeEnd: lesson.time.substring(9),
dayOfWeek,
blockName: lesson.title || lesson.subject_name
blockName: lesson.title || lesson.subject_name
} as TimetableEntry)
});
})
Expand Down
1 change: 1 addition & 0 deletions libs/api-admentum/lib/routes.ts
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,7 @@ export const apiUrls = {
lessons: api + 'lessons',
organisations: api + 'organisations',
orientations: api + 'orientations',
overview: (year: string, week: string) => baseUrl + `overview?week=${week}&year=${year}`,
permission_groups: api + 'permission_groups',
primary_group_enrollments: api + 'primary_group_enrollments',
primary_group_municipality_statistics:
Expand Down

0 comments on commit 9276db3

Please sign in to comment.