-
Notifications
You must be signed in to change notification settings - Fork 5
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
finish APIs for getCurrentActivity and findAllFriends - #842
- Loading branch information
Chanel
committed
Jul 24, 2024
1 parent
543dbec
commit bd7e624
Showing
2 changed files
with
105 additions
and
0 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,89 @@ | ||
import { CurrentActivity, Friend, FriendsList } from '../interfaces/Friends'; | ||
import NetworkError from '../interfaces/NetworkError'; | ||
import { CourseCode, CourseData, EventPeriod } from '../interfaces/Periods'; | ||
import timeoutPromise from '../utils/timeoutPromise'; | ||
import { API_URL } from './config'; | ||
import getCourseInfo from './getCourseInfo'; | ||
|
||
const getCurrentActivity = async (userId: string): Promise<CurrentActivity | null> => { | ||
// get the user's timetable data | ||
const baseURL = `${API_URL.server}/user/timetable/${userId}`; | ||
try { | ||
const data = await timeoutPromise(1000, fetch(baseURL)); | ||
const json = await data.json(); | ||
if (data.status === 400) { | ||
throw new NetworkError('Internal server error'); | ||
} | ||
|
||
const selectedCourses = json.selectedCourses; | ||
|
||
// TODO: find helpers for these - for now hardcoded | ||
const year = '2024'; | ||
const term = 'T2'; | ||
const isConvertToLocalTimezone = true; | ||
|
||
const courseInfos = await Promise.all( | ||
selectedCourses.map((courseCode: string) => | ||
getCourseInfo(year, term, courseCode as CourseCode, isConvertToLocalTimezone) | ||
) | ||
); | ||
|
||
const now = new Date(); | ||
return findCurrentActivity(courseInfos, json.createdEvents, now); | ||
} catch (error) { | ||
throw new NetworkError('Could not connect to server'); | ||
} | ||
}; | ||
|
||
|
||
const findCurrentActivity = (courseInfos: CourseData[], events: EventPeriod[], now: Date): CurrentActivity | null => { | ||
for (const courseInfo of courseInfos) { | ||
for (const courseClass of Object.values(courseInfo.activities)) { | ||
for (const classData of courseClass) { | ||
for (const period of classData.periods) { | ||
const classStart = new Date(period.time.start); | ||
const classEnd = new Date(period.time.end); | ||
|
||
if (classStart <= now && now <= classEnd) { | ||
return { ...courseClass, type: 'class', start: classStart, end: classEnd } as CurrentActivity; | ||
} | ||
} | ||
} | ||
} | ||
} | ||
|
||
for (const event of events) { | ||
const eventStart = new Date(event.time.start); | ||
const eventEnd = new Date(event.time.end); | ||
|
||
if (eventStart <= now && now <= eventEnd) { | ||
return { ...event, type: 'event', start: eventStart, end: eventEnd } as CurrentActivity; | ||
} | ||
} | ||
|
||
return null; | ||
} | ||
|
||
|
||
const getAllFriends = async (userId: string): Promise<FriendsList> => { | ||
const baseURL = `${API_URL.server}/friend/findAllFriends/${userId}`; | ||
try { | ||
const data = await timeoutPromise(1000, fetch(baseURL)); | ||
const json = await data.json(); | ||
if (data.status === 400) { | ||
throw new NetworkError('Internal server error'); | ||
} | ||
|
||
const friends: FriendsList = await Promise.all(json.data.map(async (friend: any) => ({ | ||
name: friend.name, | ||
userId: friend.userId, | ||
currentActivity: await getCurrentActivity(friend.userId) | ||
}))); | ||
|
||
return friends; | ||
} catch (error) { | ||
throw new NetworkError('Could not connect to server'); | ||
} | ||
}; | ||
|
||
export default getCurrentActivity; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,16 @@ | ||
import { SelectedClasses, CreatedEvents } from "./Periods"; | ||
|
||
export interface Friend { | ||
name: string; | ||
userId: string; | ||
// show next event within the hour | ||
currentActivity: CurrentActivity | null; | ||
} | ||
|
||
export type FriendsList = Friend[]; | ||
|
||
export type CurrentActivity = { | ||
type: 'class' | 'event'; | ||
start: Date; | ||
end: Date; | ||
} |