Skip to content
11 changes: 10 additions & 1 deletion devrun.js
Original file line number Diff line number Diff line change
Expand Up @@ -66,6 +66,15 @@ async function run() {
const classmates = await api.getClassmates(children[0])
console.log(classmates)
*/
console.log('teachers')
const teachers = await api.getTeachers(children[0])
console.log(teachers)

console.log('schoolContacts')
const schoolContacts = await api.getSchoolContacts(children[0])
console.log(schoolContacts)

/*
try {
console.log('schedule')
const schedule = await api.getSchedule(children[1], DateTime.local(), DateTime.local().plus({ week: 1 }))
Expand All @@ -89,7 +98,7 @@ async function run() {
} catch (error) {
console.error(error)
}

*/
/*
console.log('news')
const news = await api.getNews(children[0])
Expand Down
36 changes: 36 additions & 0 deletions lib/api.ts
Original file line number Diff line number Diff line change
Expand Up @@ -17,7 +17,9 @@ import {
RequestInit,
ScheduleItem,
User,
SchoolContact,
Skola24Child,
Teacher,
EtjanstChild,
SSOSystem,
TimetableEntry
Expand All @@ -27,6 +29,7 @@ import * as parse from './parse/index'
import wrap, { Fetcher, FetcherOptions } from './fetcher'
import * as fake from './fakeData'


const fakeResponse = <T>(data: T): Promise<T> =>
new Promise((res) => setTimeout(() => res(data), 200 + Math.random() * 800))

Expand Down Expand Up @@ -253,6 +256,39 @@ export class Api extends EventEmitter {
return parse.classmates(data)
}

public async getTeachers(child: EtjanstChild): Promise<Teacher[]> {
if (this.isFake) return fakeResponse(fake.teachers(child))

const session = this.getRequestInit()

const schoolForms = (child.status || '').split(';')
let teachers: Teacher[] = []

for(let i = 0; i< schoolForms.length; i+=1){
const url = routes.teachers(child.sdsId, schoolForms[i])
// eslint-disable-next-line no-await-in-loop
const response = await this.fetch(`teachers_${schoolForms[i]}`, url, session)
// eslint-disable-next-line no-await-in-loop
const data = await response.json()
teachers = [
...teachers,
...parse.teachers(data)
]
}

return teachers
}

public async getSchoolContacts(child: EtjanstChild): Promise<SchoolContact[]> {
if(this.isFake) return fakeResponse(fake.schoolContacts(child))

const url = routes.schoolContacts(child.sdsId, child.schoolId || '')
const session = this.getRequestInit()
const response = await this.fetch('schoolContacts', url, session)
const data = await response.json()
return parse.schoolContacts(data)
}

public async getSchedule(
child: EtjanstChild,
from: DateTime,
Expand Down
60 changes: 59 additions & 1 deletion lib/fakeData.ts
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,9 @@ import {
NewsItem,
Notification,
ScheduleItem,
SchoolContact,
Skola24Child,
Teacher,
TimetableEntry,
User,
} from './types'
Expand Down Expand Up @@ -233,6 +235,56 @@ const data: any = {
className: '2B',
},
],
teachers: [
{
id: 15662220,
firstname: "Cecilia",
lastname: "Test",
email: "cecilia.test@edu.stockholm.se",
phoneWork: null,
active: true,
status: " S",
timeTableAbbreviation: 'CTE',
},
{
id: 15662221,
firstname: "Anna",
lastname: "Test",
email: "anna.test@edu.stockholm.se",
phoneWork: '08000000',
active: true,
status: " GR",
timeTableAbbreviation: 'ATE',
},
{
id: 15662221,
firstname: "Greta",
lastname: "Test",
email: null,
phoneWork: '08000001',
active: true,
status: " F",
timeTableAbbreviation: 'GTE',
}
],
schoolContacts: [
{
title: "Expedition",
name: null,
phone: "508 000 00",
email: "",
schoolName: "Vallaskolan",
className: null,
},
{
title: "Rektor",
name: "Alvar Sträng",
phone: "08-50800001",
email: "alvar.strang@edu.stockholm.se",
schoolName: null,
className: null,
}
],
news: [
{
id: 'asdfasdfasdfw',
Expand Down Expand Up @@ -1126,6 +1178,12 @@ export const skola24Children = (): Skola24Child[] => [
export const classmates = (child: EtjanstChild): Classmate[] =>
data[child.id].classmates

export const teachers = (child:EtjanstChild): Teacher[] =>
data[child.id].teachers

export const schoolContacts = (child: EtjanstChild): SchoolContact[] =>
data[child.id].schoolContacts

export const news = (child: Child): NewsItem[] => data[child.id].news

export const calendar = (child: Child): CalendarItem[] =>
Expand Down Expand Up @@ -1599,5 +1657,5 @@ export const timetable = (child: Skola24Child): TimetableEntry[] => {
dateStart: '2021-04-16T10:40:00.000+02:00',
dateEnd: '2021-04-16T11:35:00.000+02:00',
}
]
]
}
50 changes: 50 additions & 0 deletions lib/parse/__tests__/schoolContacts.test.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,50 @@
import { EtjanstResponse } from '../'
import { schoolContacts } from '../schoolContacts'

let response: EtjanstResponse

beforeEach(() => {
response = {
"Success": true,
"Error": null,
"Data": [
{
"Title": "Expedition",
"Name": null,
"Phone": "508 000 00",
"Email": "",
"SchoolName": "Påhittade skolan",
"ClassName": null
},
{
"Title": "Rektor",
"Name": "Andersson, Anna Bella Cecilia",
"Phone": "08-508 000 00",
"Email": "anna.anderssonn@edu.stockholm.se",
"SchoolName": null,
"ClassName": null
}
]
}
})

it('parses teachers correctly', () => {
expect(schoolContacts(response)).toEqual([
{
title: 'Expedition',
name: null,
phone: '508 000 00',
email: '',
schoolName: 'Påhittade skolan',
className: null
},
{
title: 'Rektor',
name: 'Andersson, Anna Bella Cecilia',
phone: '08-508 000 00',
email: 'anna.anderssonn@edu.stockholm.se',
schoolName: null,
className: null
}
])
})
68 changes: 68 additions & 0 deletions lib/parse/__tests__/teachers.test.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,68 @@
import { EtjanstResponse } from '../'
import { teachers } from '../teachers'

let response: EtjanstResponse

beforeEach(() => {
response = {
"Success": true,
"Error": null,
"Data": [
{
"ID": 156735,
"BATCH": "GR",
"SIS_ID": "F154239A-EA4A-4C6C-A112-0B9581132E3D",
"USERNAME": "anna.andersson",
"SCHOOL_SIS_ID": "DE2E1293-0F40-4B91-9D91-1E99355DC257",
"EMAILADDRESS": null,
"STATUS": " GR",
"ERRORCODE": 0,
"FIRSTNAME": "Anna",
"LASTNAME": "Andersson",
"ACTIVE": true,
"TELWORK": "08 508 0000000"
},
{
"ID": 156690,
"BATCH": "GR",
"SIS_ID": "9EC59FCA-80AD-4774-AABD-427040207E33",
"USERNAME": "gunnar.grymm",
"SCHOOL_SIS_ID": "DE2E1293-0F40-4B91-9D91-1E99355DC257",
"EMAILADDRESS": "gunnar.grymm@edu.stockholm.se",
"STATUS": " F",
"ERRORCODE": 0,
"FIRSTNAME": "Gunnar",
"LASTNAME": "Grymm",
"ACTIVE": true,
"TELWORK": null
}
]
}
})

it('parses teachers correctly', () => {
expect(teachers(response)).toEqual([
{
id: 156735,
sisId: 'F154239A-EA4A-4C6C-A112-0B9581132E3D',
firstname: 'Anna',
lastname: 'Andersson',
email: null,
phoneWork: '08 508 0000000',
active: true,
status: ' GR',
timeTableAbbreviation: 'AAN'
},
{
id: 156690,
sisId: '9EC59FCA-80AD-4774-AABD-427040207E33',
firstname: 'Gunnar',
lastname: 'Grymm',
email: 'gunnar.grymm@edu.stockholm.se',
phoneWork: null,
active: true,
status: ' F',
timeTableAbbreviation: 'GGR'
},
])
})
2 changes: 2 additions & 0 deletions lib/parse/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -6,5 +6,7 @@ export * from './menu'
export * from './news'
export * from './notifications'
export * from './schedule'
export * from './schoolContacts'
export * from './teachers'
export * from './timetable'
export * from './user'
22 changes: 22 additions & 0 deletions lib/parse/schoolContacts.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,22 @@
import { etjanst } from './etjanst'
import { SchoolContact } from '../types'

export const schoolContact = ({
title,
name,
phone,
email,
schoolName,
className,
}: any): SchoolContact => ({
title,
name,
phone,
email,
schoolName,
className,
})


export const schoolContacts = (data: any): SchoolContact[] =>
etjanst(data).map(schoolContact)
29 changes: 29 additions & 0 deletions lib/parse/teachers.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,29 @@
import { etjanst } from './etjanst'
import { Teacher } from '../types'

const abbreviate = (firstname?: string, lastname?: string): string =>
`${firstname?.substr(0,1)}${lastname?.substr(0,2)}`.toUpperCase()

export const teacher = ({
id,
sisId,
firstname,
lastname,
emailaddress,
telwork,
active,
status,
}: any): Teacher => ({
id,
sisId,
firstname,
lastname,
email: emailaddress,
phoneWork: telwork,
active,
status,
timeTableAbbreviation: abbreviate(firstname, lastname)
})

export const teachers = (data: any): Teacher[] =>
etjanst(data).map(teacher)
6 changes: 6 additions & 0 deletions lib/routes.ts
Original file line number Diff line number Diff line change
Expand Up @@ -20,6 +20,12 @@ export const calendar = (childId: string) =>
export const classmates = (childId: string) =>
`${urlLoggedIn}/contacts/GetStudentsByClass?studentId=${childId}`

export const teachers = (childId: string, schoolForm: string) =>
`${urlLoggedIn}/contacts/GetTeachersByStudent?studentId=${childId}&schoolForm=${schoolForm}`

export const schoolContacts = (childId: string, schoolId: string) =>
`${urlLoggedIn}/contacts/GetSchoolContacts?schoolId=${schoolId}&studentId=${childId}&schoolForm=Klasslista`

export const user =
'https://etjanst.stockholm.se/vardnadshavare/base/getuserdata'

Expand Down
Loading