From 2a064ca70943d04781eb5d2656b263700fe8c6cb Mon Sep 17 00:00:00 2001 From: Christian Landgren Date: Tue, 15 Dec 2020 00:34:18 +0100 Subject: [PATCH] bugfix events - needs proper user agent --- packages/api/lib/backend.js | 21 ++++++++++++--------- packages/api/lib/urls.js | 2 +- packages/api/test.js | 6 ++++++ 3 files changed, 19 insertions(+), 10 deletions(-) diff --git a/packages/api/lib/backend.js b/packages/api/lib/backend.js index f5f02101..f7709953 100644 --- a/packages/api/lib/backend.js +++ b/packages/api/lib/backend.js @@ -6,10 +6,11 @@ const h2m = require('h2m') const {htmlDecode} = require('js-htmlencode') const urls = require('./urls') -const download = (url, cookie) => fetch(url, {headers: {cookie}}) +const download = (url, cookie) => fetch(url, {headers: {cookie, 'user-agent': 'Mozilla/5.0 (Macintosh; Intel Mac OS X 10_15_6) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/87.0.4280.87 Safari/537.36'}}) const fetchJson = (url, cookie) => { return fetch(url, {headers: {cookie}}) + .then(res => console.log('fetching', res.url) || res) .then(res => res.ok ? res : Promise.reject(res.statusText)) .then(res => res.json()) // convert to camelCase @@ -39,7 +40,7 @@ const getNews = (childId, cookie) => fetchJson(urls.news(childId), cookie) .catch(err => ({err})) const getCalendar = (childId, cookie) => fetchJson(urls.calendar(childId), cookie) - .then(({title, id, description, location, longEventDateTime: startDate, longEndDateTime: endDate, allDayEvent: allDay}) => ({title, id, description, location, startDate, endDate, allDay})) + .then(calendar => calendar.map(({title, id, description, location, longEventDateTime: startDate, longEndDateTime: endDate, allDayEvent: allDay}) => ({title, id, description, location, startDate, endDate, allDay}))) .catch(err => ({err})) const getNotifications = (childId, cookie) => fetchJson(urls.notifications(childId), cookie) @@ -47,17 +48,19 @@ const getNotifications = (childId, cookie) => fetchJson(urls.notifications(child .catch(err => console.error(err) || {err}) const getMenu = (childId, cookie) => fetchJson(urls.menu(childId), cookie).catch(err => ({err})) -const getSchedule = (childId, cookie) => fetchJson(urls.schedule(childId, moment().startOf('day').toISOString(), moment().endOf('day').toISOString()), cookie).catch(err => ({err})) +const getSchedule = (childId, cookie) => fetchJson(urls.schedule(childId, moment().format('YYYY-MM-DD'), moment().add(7, 'days').format('YYYY-MM-DD')), cookie) + .then(schedule => schedule.map(({title, id, description, location, longEventDateTime: startDate, longEndDateTime: endDate, allDayEvent: allDay, mentor}) => ({title, id, description, location, startDate, endDate, allDay, mentor}))) + .catch(err => ({err})) const getChildById = async (childId, cookie) => { const children = await getChildren() const child = children.find(c => c.id == childId) - const [news, calendar, notifications, menu, schedule] = await Promise.all([ - getNews(childId, cookie), - getCalendar(childId, cookie), - getNotifications(child.sdsId, cookie), - getMenu(child.id, cookie), - getSchedule(child.sdsId, cookie)]) + const [news, calendar, notifications, menu, schedule] = [ + await getNews(childId, cookie), + await getCalendar(childId, cookie), + await getNotifications(child.sdsId, cookie), + await getMenu(child.id, cookie), + await getSchedule(child.id, cookie)] return {child, news, calendar, notifications, menu, schedule} } diff --git a/packages/api/lib/urls.js b/packages/api/lib/urls.js index 968ad92d..ff880029 100644 --- a/packages/api/lib/urls.js +++ b/packages/api/lib/urls.js @@ -10,7 +10,7 @@ const urls = { news: childId => `${baseUrl}/vardnadshavare/inloggad2/News/GetNewsOverview?childId=${childId}`, image: url => `${baseUrl}/vardnadshavare/inloggad2/NewsBanner?url=${url}`, notifications: childId => `${baseUrl}/vardnadshavare/inloggad2/Overview/GetNotification?childId=${childId}`, - menu: childId => `${baseUrl}/vardnadshavare/inloggad2/Matsedel/GetMatsedelChoice?childId=${childId}`, + menu: childId => `${baseUrl}/vardnadshavare/inloggad2/Matsedel/GetMatsedelRSS?childId=${childId}`, schedule: (childId, fromDate, endDate) => `${baseUrl}/vardnadshavare/inloggad2/Calender/GetSchema?childId=${childId}&startDate=${fromDate}&endDate=${endDate}` } diff --git a/packages/api/test.js b/packages/api/test.js index 9b35a3e5..ece04892 100644 --- a/packages/api/test.js +++ b/packages/api/test.js @@ -29,6 +29,12 @@ const test = async () => { ...child, ...await fetch(`http://localhost:9000/children/${child.id}`, {headers}).then(res => res.json()) }))) + + data.map(async child => ({ + ...child, + messages: await Promise.all(child.notifications.map((notification) => fetch(notification.url, {headers}).then(res => res.text()))) + })) + console.log(JSON.stringify(data, null, 2)) }