Skip to content

Commit

Permalink
Merge pull request #65 from avantifellows/feat/fetch-session-occurren…
Browse files Browse the repository at this point in the history
…ce-in-one-call

Feat: fetch session occurrences in single call
  • Loading branch information
Bahugunajii authored Nov 26, 2024
2 parents f9ae918 + 388d65f commit d275620
Show file tree
Hide file tree
Showing 2 changed files with 20 additions and 28 deletions.
13 changes: 10 additions & 3 deletions api/afdb/session.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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) {
Expand Down
35 changes: 10 additions & 25 deletions app/page.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -87,40 +87,25 @@ 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) {
console.error("Error fetching user sessions:", error);
}
};


const renderLiveClasses = () => {
if (liveClasses.length === 0) {
return <MessageDisplay message="No more live classes are scheduled for today!" />;
Expand Down

0 comments on commit d275620

Please sign in to comment.