From 348e43778df7e7336148b327a0ce2363bca05fa4 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Johan=20=C3=96brink?= Date: Mon, 21 Dec 2020 18:55:38 +0100 Subject: [PATCH] =?UTF-8?q?feat:=20=F0=9F=8E=B8=20Notifications=20(#20)?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit ✅ Closes: #11 --- README.md | 3 +++ lib/children.ts | 10 +++++-- lib/index.ts | 7 ++++- lib/parse.test.ts | 66 ++++++++++++++++++++++++++++++++++++++++++++++- lib/parse.ts | 32 ++++++++++++++++++++++- lib/types.ts | 16 +++++------- run.js | 14 ++++++---- 7 files changed, 129 insertions(+), 19 deletions(-) diff --git a/README.md b/README.md index dd4046b2..29a5adb7 100644 --- a/README.md +++ b/README.md @@ -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 diff --git a/lib/children.ts b/lib/children.ts index 7cd25808..66968b4b 100644 --- a/lib/children.ts +++ b/lib/children.ts @@ -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 => { @@ -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 => { + const url = routes.notifications(childId) + const response = await fetch(url, init) + const data = await response.json() + return etjanst(data).map(notification) +} diff --git a/lib/index.ts b/lib/index.ts index db438296..7985a618 100644 --- a/lib/index.ts +++ b/lib/index.ts @@ -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' @@ -85,6 +85,11 @@ export class Api extends EventEmitter { return data } + async getNotifications(child: Child): Promise { + const data = notifications(this.fetch, this.session)(child.sdsId) + return data + } + async logout() { this.session = undefined await this.clearCookies() diff --git a/lib/parse.test.ts b/lib/parse.test.ts index 29e93c5f..bcb948f5 100644 --- a/lib/parse.test.ts +++ b/lib/parse.test.ts @@ -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" @@ -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', + }]) + }) + }) }) }) diff --git a/lib/parse.ts b/lib/parse.ts index d445cb6d..ffa1cd79 100644 --- a/lib/parse.ts +++ b/lib/parse.ts @@ -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') @@ -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, +}) diff --git a/lib/types.ts b/lib/types.ts index e6b06db3..1e50bbff 100644 --- a/lib/types.ts +++ b/lib/types.ts @@ -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 /** *

* URL with the actual message as a webpage. Needs separate login. @@ -119,9 +117,9 @@ export interface Notification { * @type {string} * @memberof Notification */ - url?: string; - category?: string; - messageType?: string; + url: string + category: string | null + type: string } /** diff --git a/run.js b/run.js index 4880c9e7..a9707899 100644 --- a/run.js +++ b/run.js @@ -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') @@ -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() })