feat: 🎸 Notifications (#20)

 Closes: #11
This commit is contained in:
Johan Öbrink 2020-12-21 18:55:38 +01:00 committed by GitHub
parent 17db6cc152
commit 348e43778d
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
7 changed files with 129 additions and 19 deletions

View File

@ -87,6 +87,9 @@ const news = await api.getNews(children[0])
// Get menu // Get menu
const menu = await api.getMenu(children[0]) const menu = await api.getMenu(children[0])
// Get notifications
const notifications = await api.getNotifications(children[0])
``` ```
### Setting session cookie ### Setting session cookie

View File

@ -4,7 +4,7 @@ import {
CalendarItem, Child, Classmate, Fetch, RequestInit, CalendarItem, Child, Classmate, Fetch, RequestInit,
} from './types' } from './types'
import { import {
etjanst, child, calendarItem, classmate, etjanst, child, calendarItem, classmate, notification,
} from './parse' } from './parse'
export const list = (fetch: Fetch, init?: RequestInit) => async (): Promise<Child[]> => { export const list = (fetch: Fetch, init?: RequestInit) => async (): Promise<Child[]> => {
@ -41,6 +41,12 @@ export const menu = (fetch: Fetch, init?: RequestInit) => async (childId: string
const url = routes.menu(childId) const url = routes.menu(childId)
const response = await fetch(url, init) const response = await fetch(url, init)
const data = await response.json() const data = await response.json()
console.log(data)
return etjanst(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)
}

View File

@ -7,7 +7,7 @@ import {
CalendarItem, Child, Classmate, Fetch, MenuItem, RequestInit, CalendarItem, Child, Classmate, Fetch, MenuItem, RequestInit,
} from './types' } from './types'
import { import {
calendar, classmates, list, menu, schedule, calendar, classmates, list, menu, notifications, schedule,
} from './children' } from './children'
import { news, News } from './news' import { news, News } from './news'
import { user } from './user' import { user } from './user'
@ -85,6 +85,11 @@ export class Api extends EventEmitter {
return data return data
} }
async getNotifications(child: Child): Promise<Notification[]> {
const data = notifications(this.fetch, this.session)(child.sdsId)
return data
}
async logout() { async logout() {
this.session = undefined this.session = undefined
await this.clearCookies() await this.clearCookies()

View File

@ -1,6 +1,15 @@
import * as moment from 'moment' import * as moment from 'moment'
import { import {
etjanst, newsItem, EtjanstResponse, child, calendarItem, classmate, scheduleItem, menuItem, user, etjanst,
newsItem,
EtjanstResponse,
child,
calendarItem,
classmate,
scheduleItem,
menuItem,
user,
notification,
} from "./parse" } from "./parse"
import { NewsItem } from "./types" import { NewsItem } from "./types"
@ -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',
}])
})
})
}) })
}) })

View File

@ -2,7 +2,7 @@ import * as moment from 'moment'
import * as h2m from 'h2m' import * as h2m from 'h2m'
import { htmlDecode } from 'js-htmlencode' import { htmlDecode } from 'js-htmlencode'
import { import {
CalendarItem, Child, Classmate, Guardian, MenuItem, NewsItem, ScheduleItem, User, CalendarItem, Child, Classmate, Guardian, MenuItem, NewsItem, ScheduleItem, User, Notification,
} from './types' } from './types'
const camel = require('camelcase-keys') const camel = require('camelcase-keys')
@ -99,3 +99,33 @@ export const menuItem = ({
title, title,
description: htmlDecode(h2m(description)), 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,
})

View File

@ -105,12 +105,10 @@ export interface NewsItem {
* @interface Notification * @interface Notification
*/ */
export interface Notification { export interface Notification {
id?: string; id: string
sender?: { sender: string
name?: string; dateCreated: Moment
}; message: string
dateCreated?: string;
message?: string;
/** /**
* <p> * <p>
* URL with the actual message as a webpage. Needs separate login. * URL with the actual message as a webpage. Needs separate login.
@ -119,9 +117,9 @@ export interface Notification {
* @type {string} * @type {string}
* @memberof Notification * @memberof Notification
*/ */
url?: string; url: string
category?: string; category: string | null
messageType?: string; type: string
} }
/** /**

14
run.js
View File

@ -28,12 +28,12 @@ async function run() {
api.on('login', async () => { api.on('login', async () => {
console.log('Logged in') console.log('Logged in')
console.log('user') // console.log('user')
const user = await api.getUser() // const user = await api.getUser()
console.log(user) // console.log(user)
// console.log('children') console.log('children')
// const children = await api.getChildren() const children = await api.getChildren()
// console.log(children) // console.log(children)
// console.log('calendar') // console.log('calendar')
@ -56,6 +56,10 @@ async function run() {
// const menu = await api.getMenu(children[0]) // const menu = await api.getMenu(children[0])
// console.log(menu) // console.log(menu)
console.log('notifications')
const notifications = await api.getNotifications(children[0])
console.log(notifications)
await api.logout() await api.logout()
}) })