Skip to content

Commit

Permalink
feat: 🎸 getUser (kolplattformen#19)
Browse files Browse the repository at this point in the history
Returns currrenctly logged in user

✅ Closes: kolplattformen#9
  • Loading branch information
JohanObrink authored Dec 21, 2020
1 parent 02bf08a commit 39b62b7
Show file tree
Hide file tree
Showing 8 changed files with 75 additions and 8 deletions.
3 changes: 2 additions & 1 deletion .eslintrc.js
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,7 @@ module.exports = {
// e.g. "@typescript-eslint/explicit-function-return-type": "off",
// '@typescript-eslint/indent': ['error', 2],
'@typescript-eslint/semi': [2, 'never'],
'max-len': ['error', { code: 120, 'ignoreUrls': true }]
'max-len': ['error', { code: 120, 'ignoreUrls': true }],
'import/prefer-default-export': 0,
},
}
3 changes: 3 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -65,6 +65,9 @@ loginStatus.on("OK", () =>
### Loading data
```javascript
// Get current user
const user = await api.getUser();

// List children
const children = await api.getChildren();

Expand Down
6 changes: 6 additions & 0 deletions lib/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,7 @@ import {
calendar, classmates, list, menu, schedule,
} from './children'
import { news, News } from './news'
import { user } from './user'

interface AsyncishFunction { (): void | Promise<void> }

Expand Down Expand Up @@ -49,6 +50,11 @@ export class Api extends EventEmitter {
return loginStatus
}

async getUser(): Promise<any> {
const data = await user(this.fetch, this.session)()
return data
}

async getChildren(): Promise<Child[]> {
const data = await list(this.fetch, this.session)()
return data
Expand Down
25 changes: 24 additions & 1 deletion lib/parse.test.ts
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
import * as moment from 'moment'
import {
etjanst, newsItem, EtjanstResponse, child, calendarItem, classmate, scheduleItem, menuItem,
etjanst, newsItem, EtjanstResponse, child, calendarItem, classmate, scheduleItem, menuItem, user,
} from "./parse"
import { NewsItem } from "./types"

Expand Down Expand Up @@ -306,5 +306,28 @@ describe('parse', () => {
}])
})
})
describe('user', () => {
let userResponse: any
beforeEach(() => {
userResponse = {
socialSecurityNumber: '197106171635',
isAuthenticated: true,
userFirstName: 'Per-Ola',
userLastName: 'Assarsson',
userEmail: 'per-ola.assarsson@dodgit.com',
notificationId: 'B026594053D44299AB64ED81990B49C04D32F635C9A3454A84030439BFDDEF04'
}
})
it('parses user correctly', () => {
expect(user(userResponse)).toEqual({
personalNumber: '197106171635',
firstName: 'Per-Ola',
lastName: 'Assarsson',
email: 'per-ola.assarsson@dodgit.com',
isAuthenticated: true,
notificationId: 'B026594053D44299AB64ED81990B49C04D32F635C9A3454A84030439BFDDEF04',
})
})
})
})
})
13 changes: 12 additions & 1 deletion lib/parse.ts
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@ import * as moment from 'moment'
import * as h2m from 'h2m'
import { htmlDecode } from 'js-htmlencode'
import {
CalendarItem, Child, Classmate, Guardian, MenuItem, NewsItem, ScheduleItem,
CalendarItem, Child, Classmate, Guardian, MenuItem, NewsItem, ScheduleItem, User,
} from './types'

const camel = require('camelcase-keys')
Expand All @@ -20,6 +20,17 @@ export const etjanst = (response: EtjanstResponse): any | any[] => {
return camel(response.Data, { deep: true })
}

export const user = ({
socialSecurityNumber, isAuthenticated, userFirstName, userLastName, userEmail, notificationId,
}: any): User => ({
personalNumber: socialSecurityNumber,
firstName: userFirstName,
lastName: userLastName,
email: userEmail,
isAuthenticated,
notificationId,
})

export const child = ({
id, sdsId, name, status, schoolId,
}: any): Child => ({
Expand Down
9 changes: 9 additions & 0 deletions lib/types.ts
Original file line number Diff line number Diff line change
Expand Up @@ -142,3 +142,12 @@ export interface MenuItem {
title: string
description: string
}

export interface User {
personalNumber: string
isAuthenticated: boolean
firstName: string
lastName: string
email: string | null
notificationId: string
}
10 changes: 10 additions & 0 deletions lib/user.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,10 @@
import routes from './routes'
import { user as _user } from './parse'
import { Fetch, RequestInit } from './types'

export const user = (fetch: Fetch, init?: RequestInit) => async (): Promise<any> => {
const url = routes.user
const response = await fetch(url, init)
const data = await response.json()
return _user(data)
}
14 changes: 9 additions & 5 deletions run.js
Original file line number Diff line number Diff line change
Expand Up @@ -28,8 +28,12 @@ async function run() {
api.on('login', async () => {
console.log('Logged in')

console.log('children')
const children = await api.getChildren()
console.log('user')
const user = await api.getUser()
console.log(user)

// console.log('children')
// const children = await api.getChildren()
// console.log(children)

// console.log('calendar')
Expand All @@ -48,9 +52,9 @@ async function run() {
// const news = await api.getNews(children[0])
// console.log(news)

console.log('menu')
const menu = await api.getMenu(children[0])
console.log(menu)
// console.log('menu')
// const menu = await api.getMenu(children[0])
// console.log(menu)

await api.logout()
})
Expand Down

0 comments on commit 39b62b7

Please sign in to comment.