diff --git a/api/afdb/session.ts b/api/afdb/session.ts index 3478835..865bde1 100644 --- a/api/afdb/session.ts +++ b/api/afdb/session.ts @@ -5,11 +5,18 @@ import getFetchConfig from '../fetchConfig'; const url = process.env.AF_DB_SERVICE_URL; const bearerToken = process.env.AF_DB_SERVICE_BEARER_TOKEN || ''; -export const getSessionOccurrences = async (sessionId: string) => { +export const getSessionOccurrences = async (sessionIds: string[]) => { try { const queryParams = new URLSearchParams(); - if (sessionId) queryParams.append('session_id', sessionId.toString()); - const urlWithParams = `${url}/session-occurrence?${queryParams.toString()}&is_start_time=today`; + + if (sessionIds && sessionIds.length > 0) { + queryParams.append('session_ids', sessionIds.join(',')); + } + + queryParams.append('is_start_time', 'today'); + + const urlWithParams = `${url}/session-occurrence?${queryParams.toString()}`; + const response = await fetch(urlWithParams, getFetchConfig(bearerToken)); if (!response.ok) { diff --git a/app/page.tsx b/app/page.tsx index adf26c3..2acc3ba 100644 --- a/app/page.tsx +++ b/app/page.tsx @@ -87,33 +87,17 @@ export default function Home() { shouldFetchQuizzes ? fetchUserSession(userDbId!, true) : Promise.resolve([]) ]); - if (quizSessionData.length > 0) { - const quizData = await Promise.all(quizSessionData.map(async (quiz: Session) => { - const sessionOccurrenceData = await getSessionOccurrences(quiz.session_id); - if (!sessionOccurrenceData) return null; - return sessionOccurrenceData; - })); - - const flattenedQuizData = quizData.flat(); - const quizSessions = flattenedQuizData.filter(flattenedSessionsData => - flattenedSessionsData.session.platform === 'quiz' - ); - setQuizzes(quizSessions); - } + const sessionIds = [...liveSessionData, ...quizSessionData].map(session => session.session_id); - if (liveSessionData.length > 0) { - const sessionsData = await Promise.all(liveSessionData.map(async (liveSession: Session) => { - const sessionOccurrenceData = await getSessionOccurrences(liveSession.session_id); - if (!sessionOccurrenceData) return null; - return sessionOccurrenceData; - })); + const sessionOccurrences = await getSessionOccurrences(sessionIds); - const flattenedSessionsData = sessionsData.flat(); - const liveSessions = flattenedSessionsData.filter(flattenedSessionsData => - flattenedSessionsData.session.platform === 'meet' - ); - setLiveClasses(liveSessions); - } + const quizSessions = sessionOccurrences + .filter((sessionOccurence: SessionOccurrence) => sessionOccurence.session.platform === 'quiz'); + setQuizzes(quizSessions); + + const liveSessions = sessionOccurrences + .filter((sessionOccurence: SessionOccurrence) => sessionOccurence.session.platform === 'meet'); + setLiveClasses(liveSessions); MixpanelTracking.getInstance().identify(userId!); } catch (error) { @@ -121,6 +105,7 @@ export default function Home() { } }; + const renderLiveClasses = () => { if (liveClasses.length === 0) { return ;