Skip to content

Commit

Permalink
feat: 🎸 Notifications (kolplattformen#20)
Browse files Browse the repository at this point in the history
✅ Closes: kolplattformen#11
  • Loading branch information
JohanObrink authored Dec 21, 2020
1 parent 17db6cc commit 348e437
Show file tree
Hide file tree
Showing 7 changed files with 129 additions and 19 deletions.
3 changes: 3 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -87,6 +87,9 @@ const news = await api.getNews(children[0])

// Get menu
const menu = await api.getMenu(children[0])

// Get notifications
const notifications = await api.getNotifications(children[0])
```
### Setting session cookie
Expand Down
10 changes: 8 additions & 2 deletions lib/children.ts
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@ import {
CalendarItem, Child, Classmate, Fetch, RequestInit,
} from './types'
import {
etjanst, child, calendarItem, classmate,
etjanst, child, calendarItem, classmate, notification,
} from './parse'

export const list = (fetch: Fetch, init?: RequestInit) => async (): Promise<Child[]> => {
Expand Down Expand Up @@ -41,6 +41,12 @@ export const menu = (fetch: Fetch, init?: RequestInit) => async (childId: string
const url = routes.menu(childId)
const response = await fetch(url, init)
const data = await response.json()
console.log(data)
return etjanst(data)
}

export const notifications = (fetch: Fetch, init?: RequestInit) => async (childId: string): Promise<Notification[]> => {
const url = routes.notifications(childId)
const response = await fetch(url, init)
const data = await response.json()
return etjanst(data).map(notification)
}
7 changes: 6 additions & 1 deletion lib/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,7 @@ import {
CalendarItem, Child, Classmate, Fetch, MenuItem, RequestInit,
} from './types'
import {
calendar, classmates, list, menu, schedule,
calendar, classmates, list, menu, notifications, schedule,
} from './children'
import { news, News } from './news'
import { user } from './user'
Expand Down Expand Up @@ -85,6 +85,11 @@ export class Api extends EventEmitter {
return data
}

async getNotifications(child: Child): Promise<Notification[]> {
const data = notifications(this.fetch, this.session)(child.sdsId)
return data
}

async logout() {
this.session = undefined
await this.clearCookies()
Expand Down
66 changes: 65 additions & 1 deletion lib/parse.test.ts
Original file line number Diff line number Diff line change
@@ -1,6 +1,15 @@
import * as moment from 'moment'
import {
etjanst, newsItem, EtjanstResponse, child, calendarItem, classmate, scheduleItem, menuItem, user,
etjanst,
newsItem,
EtjanstResponse,
child,
calendarItem,
classmate,
scheduleItem,
menuItem,
user,
notification,
} from "./parse"
import { NewsItem } from "./types"

Expand Down Expand Up @@ -329,5 +338,60 @@ describe('parse', () => {
})
})
})
describe('notifications', () => {
beforeEach(() => {
response = {
Success: true,
Error: null,
Data: [
{
Notification: {
Messageid: 'E2E3A567-307F-4859-91BA-31B1F4522A7B',
Messagecorrelationid: 'BB54DC8E-BB02-49A5-9806-4A2433031AA7',
Message: '{"messages":{"message":{"messageid":"E2E3A567-307F-4859-91BA-31B1F4522A7B","messagecorrelationid":"BB54DC8E-BB02-49A5-9806-4A2433031AA7","messagetext":"Betygen är publicerade.","messagesubject":"Betyg klara","messagetime":"2020-12-18T15:59:43.195","linkbackurl":"https://elevdokumentation.stockholm.se/loa3/gradesStudent.do","sender":{"name":"Elevdokumentation"},"recipient":{"recipient":"195709227283","role":"Guardian"},"messagetype":{"type":"webnotify"},"system":"Elevdokumentation","participant":"BB7DE89D-D714-4EB2-85CD-36F9991E7C34"}}}',
Readreceipt: false,
Recipient: '195709227283',
Id: 5880387,
DateCreated: '2020-12-18T15:59:46.34',
DateModified: '/Date(1608307186340)/',
Role: 'Guardian',
Participant: 'BB7DE89D-D714-4EB2-85CD-36F9991E7C34'
},
NotificationMessage: {
Messages: {
Message: {
Messageid: 'E2E3A567-307F-4859-91BA-31B1F4522A7B',
Messagecorrelationid: 'BB54DC8E-BB02-49A5-9806-4A2433031AA7',
Messagetext: 'Betygen är publicerade.',
Messagetime: '/Date(1608303583195)/',
Linkbackurl: 'https://elevdokumentation.stockholm.se/loa3/gradesStudent.do',
Category: null,
Sender: { Name: 'Elevdokumentation' },
Recipient: {
RecipientRecipient: '195709227283',
Role: 'Guardian',
Schooltype: null
},
Messagetype: { Type: 'webnotify' },
System: 'Elevdokumentation'
}
}
}
},
],
}
})
it('parses notifications correctly', () => {
expect(etjanst(response).map(notification)).toEqual([{
id: 'E2E3A567-307F-4859-91BA-31B1F4522A7B',
message: 'Betygen är publicerade.',
sender: 'Elevdokumentation',
url: 'https://elevdokumentation.stockholm.se/loa3/gradesStudent.do',
dateCreated: moment(new Date('2020-12-18T15:59:46.34')),
category: null,
type: 'webnotify',
}])
})
})
})
})
32 changes: 31 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, User,
CalendarItem, Child, Classmate, Guardian, MenuItem, NewsItem, ScheduleItem, User, Notification,
} from './types'

const camel = require('camelcase-keys')
Expand Down Expand Up @@ -99,3 +99,33 @@ export const menuItem = ({
title,
description: htmlDecode(h2m(description)),
})

export const notification = ({
notification: {
messageid,
dateCreated,
},
notificationMessage: {
messages: {
message: {
category,
messagetext,
linkbackurl,
messagetype: {
type,
},
sender: {
name,
},
},
},
},
}: any): Notification => ({
id: messageid,
message: messagetext,
sender: name,
url: linkbackurl,
dateCreated: moment(new Date(dateCreated)),
category,
type,
})
16 changes: 7 additions & 9 deletions lib/types.ts
Original file line number Diff line number Diff line change
Expand Up @@ -105,12 +105,10 @@ export interface NewsItem {
* @interface Notification
*/
export interface Notification {
id?: string;
sender?: {
name?: string;
};
dateCreated?: string;
message?: string;
id: string
sender: string
dateCreated: Moment
message: string
/**
* <p>
* URL with the actual message as a webpage. Needs separate login.
Expand All @@ -119,9 +117,9 @@ export interface Notification {
* @type {string}
* @memberof Notification
*/
url?: string;
category?: string;
messageType?: string;
url: string
category: string | null
type: string
}

/**
Expand Down
14 changes: 9 additions & 5 deletions run.js
Original file line number Diff line number Diff line change
Expand Up @@ -28,12 +28,12 @@ async function run() {
api.on('login', async () => {
console.log('Logged in')

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

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

// console.log('calendar')
Expand All @@ -56,6 +56,10 @@ async function run() {
// const menu = await api.getMenu(children[0])
// console.log(menu)

console.log('notifications')
const notifications = await api.getNotifications(children[0])
console.log(notifications)

await api.logout()
})

Expand Down

0 comments on commit 348e437

Please sign in to comment.