Skip to content

Commit

Permalink
✨ add events api
Browse files Browse the repository at this point in the history
  • Loading branch information
ISNIT0 committed Jan 27, 2020
1 parent 206face commit 2002680
Show file tree
Hide file tree
Showing 4 changed files with 147 additions and 3 deletions.
100 changes: 97 additions & 3 deletions package-lock.json

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

5 changes: 5 additions & 0 deletions package.json
Original file line number Diff line number Diff line change
Expand Up @@ -29,9 +29,11 @@
"@types/cors": "^2.8.6",
"@types/express": "^4.17.2",
"@types/express-session": "^1.15.16",
"@types/ical": "^0.6.1",
"@types/lodash": "^4.14.149",
"@types/morgan": "^1.7.37",
"@types/passport": "^1.0.2",
"@types/request-promise-native": "^1.0.17",
"body-parser": "^1.19.0",
"connect-redis": "^4.0.3",
"convict": "^5.2.0",
Expand All @@ -42,11 +44,14 @@
"express-async-handler": "^1.1.4",
"express-session": "^1.17.0",
"express-sms-auth": "^1.0.1",
"ical": "^0.6.0",
"lodash": "^4.17.15",
"morgan": "^1.9.1",
"passport": "^0.4.1",
"pg": "^7.17.1",
"redis": "^2.8.0",
"request": "^2.88.0",
"request-promise-native": "^1.0.8",
"ts-loader": "^6.2.1",
"ts-node": "^8.6.1",
"ts-node-dev": "^1.0.0-pre.44",
Expand Down
44 changes: 44 additions & 0 deletions src/routes/publicApi.ts
Original file line number Diff line number Diff line change
@@ -1,9 +1,50 @@
import { Router, Request, Response } from 'express'
import expressAsyncHandler = require('express-async-handler');
import Campus from 'src/models/Campus.model';
import ical from "ical";
import requestPromise = require('request-promise-native');

const router = Router()

async function getCalEvents(url: string) {
const ics = await requestPromise.get(url.replace('webcal://', 'http://'));
const events = ical.parseICS(ics);
return events;
}

const eventsCache = {
updatedAt: 0,
events: [] as any[],
};

const FIVE_MINUTES = 1000 * 60 * 5;
async function getEvents(campuses: Campus[]) {
let allEvents;
if (eventsCache.updatedAt < (Date.now() - FIVE_MINUTES)) {
console.log(`Events cache invalidated, re-fetching`);
const calUrls = campuses.map(campus => campus.calendarUrl).filter(a => a);
const calendars = await Promise.all(
calUrls.map(getCalEvents)
);
allEvents = calendars
.flatMap(cal => Object.values(cal))
.sort((a, b) => a.start < b.start ? 1 : -1);
} else {
console.log(`Reading events from cache`);
allEvents = eventsCache.events;
}

const now = new Date();
const pastIndex = allEvents.findIndex(ev => ev.end < now);
const futureEvents = allEvents.slice(0, pastIndex);
const pastEvents = allEvents.slice(pastIndex);

return {
futureEvents,
pastEvents,
}
}

router.get(
'/college.json',
expressAsyncHandler(async (req: Request, res: Response) => {
Expand All @@ -13,8 +54,11 @@ router.get(
Campus.find({ relations: ['dean'] })
]);

const events = await getEvents(campuses);

res.send({
campuses,
events,
});
})
);
Expand Down
1 change: 1 addition & 0 deletions tsconfig.json
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,7 @@
"baseUrl": ".",
"lib": [
"es7",
"ESNext"
],
"paths": {
"src/*": [
Expand Down

0 comments on commit 2002680

Please sign in to comment.