Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Prod deployment #66

Merged
merged 8 commits into from
Nov 30, 2024
Merged
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
4 changes: 3 additions & 1 deletion app/library/page.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -11,11 +11,13 @@ import BlueprintIcon from '../../assets/blueprint.png';
import { MixpanelTracking } from '@/services/mixpanel';
import { MIXPANEL_EVENT } from '@/constants/config';
import { useAuth } from '@/services/AuthContext';
import { getGroupConfig } from '@/config/groupConfig';

const Page: React.FC = () => {
const [selectedLibrary, setSelectedLibrary] = useState<string | null>('Content');
const { push } = useRouter();
const { group } = useAuth();
const groupConfig = getGroupConfig(group || 'defaultGroup');

const handleLibraryChange = (library: string) => {
setSelectedLibrary(library);
Expand Down Expand Up @@ -47,7 +49,7 @@ const Page: React.FC = () => {
>
Content Library
</PrimaryButton>
{group !== 'EnableStudents' && group !== 'AllIndiaStudents' && (
{groupConfig.showClassLibrary && (
<PrimaryButton
onClick={() => handleLibraryChange('Class')}
className={`${buttonStyle} ${selectedLibrary === 'Class' ? selectedButtonStyle : unselectedButtonStyle}`}
Expand Down
59 changes: 36 additions & 23 deletions app/page.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -12,9 +12,11 @@ import Loading from "./loading";
import { formatCurrentTime, formatSessionTime, formatQuizSessionTime, formatTime, isSessionActive, format12HrSessionTime } from "@/utils/dateUtils";
import { api } from "@/services/url";
import { MixpanelTracking } from "@/services/mixpanel";
import { getGroupConfig } from "@/config/groupConfig";

export default function Home() {
const { loggedIn, userId, userDbId, group } = useAuth();
const groupConfig = getGroupConfig(group || 'defaultGroup');
const [liveClasses, setLiveClasses] = useState<SessionOccurrence[]>([]);
const [isLoading, setIsLoading] = useState(true);
const [quizzes, setQuizzes] = useState<QuizSession[]>([]);
Expand Down Expand Up @@ -77,37 +79,33 @@ export default function Home() {

const fetchUserSessions = async () => {
try {
const groupConfig = getGroupConfig(group || 'defaultGroup');
const shouldFetchQuizzes = groupConfig.showTests || groupConfig.showPracticeTests || groupConfig.showHomework;

const [liveSessionData, quizSessionData] = await Promise.all([
group !== 'AllIndiaStudents' ? fetchUserSession(userDbId!) : Promise.resolve([]),
fetchUserSession(userDbId!, true)
groupConfig.showLiveClasses ? fetchUserSession(userDbId!) : Promise.resolve([]),
shouldFetchQuizzes ? fetchUserSession(userDbId!, true) : Promise.resolve([])
]);

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');
const sessionIds = [...liveSessionData, ...quizSessionData].map(session => session.session_id);

const sessionOccurrences = await getSessionOccurrences(sessionIds);

const quizSessions = sessionOccurrences
.filter((sessionOccurence: SessionOccurrence) => sessionOccurence.session.platform === 'quiz');
setQuizzes(quizSessions);

if (group !== 'AllIndiaStudents') {
const sessionsData = await Promise.all(liveSessionData.map(async (liveSession: Session) => {
const sessionOccurrenceData = await getSessionOccurrences(liveSession.session_id);
if (!sessionOccurrenceData) return null;
return sessionOccurrenceData;
}));
const flattenedSessionsData = sessionsData.flat();
const liveSessions = flattenedSessionsData.filter(flattenedSessionsData => flattenedSessionsData.session.platform === 'meet');
setLiveClasses(liveSessions);
}
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 Expand Up @@ -147,6 +145,21 @@ export default function Home() {
};

const renderTestSection = (title: string, tests: QuizSession[]) => {
const shouldShow = (() => {
switch (title.toLowerCase()) {
case 'tests':
return groupConfig.showTests;
case 'practice tests':
return groupConfig.showPracticeTests;
case 'homework':
return groupConfig.showHomework;
default:
return true;
}
})();

if (!shouldShow) return null;

if (tests.length === 0) {
return (
<div>
Expand Down Expand Up @@ -280,17 +293,17 @@ export default function Home() {
) : (
<main className="min-h-screen max-w-xl mx-auto md:mx-auto bg-heading">
<TopBar />
{group !== 'AllIndiaStudents' && (
{groupConfig.showLiveClasses && (
<div>
<h1 className="text-primary ml-4 font-semibold text-xl pt-6">Live Classes</h1>
{renderLiveClasses()}
</div>
)}

<div className="pb-40">
{renderTestSection("Tests", [...nonChapterTests, ...chapterTests])}
{renderTestSection("Practice Tests", practiceTests)}
{renderTestSection("Homework", homework)}
{groupConfig.showTests && renderTestSection("Tests", [...nonChapterTests, ...chapterTests])}
{groupConfig.showPracticeTests && renderTestSection("Practice Tests", practiceTests)}
{groupConfig.showHomework && renderTestSection("Homework", homework)}
</div>
<BottomNavigationBar />
</main>
Expand Down
15 changes: 14 additions & 1 deletion app/types.ts
Original file line number Diff line number Diff line change
Expand Up @@ -155,4 +155,17 @@ export interface MessageDisplayProps {

export interface QuizCompletionStatus {
[key: string]: boolean;
}
}

export interface GroupConfig {
showLiveClasses: boolean;
showTests: boolean;
showPracticeTests: boolean;
showHomework: boolean;
showContentLibrary: boolean;
showClassLibrary: boolean;
}

export type GroupConfigurations = {
[key: string]: GroupConfig;
};
42 changes: 42 additions & 0 deletions config/groupConfig.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,42 @@
import { GroupConfig, GroupConfigurations } from "@/app/types";

const groupConfig: GroupConfigurations = {
AllIndiaStudents: {
showLiveClasses: false,
showTests: true,
showPracticeTests: true,
showHomework: false,
showContentLibrary: true,
showClassLibrary: false,
},
EnableStudents: {
showLiveClasses: true,
showTests: true,
showPracticeTests: true,
showHomework: true,
showContentLibrary: true,
showClassLibrary: false,
},
DelhiStudents: {
showLiveClasses: true,
showTests: true,
showPracticeTests: true,
showHomework: true,
showContentLibrary: true,
showClassLibrary: true,
},
defaultGroup: {
showLiveClasses: true,
showTests: true,
showPracticeTests: true,
showHomework: true,
showContentLibrary: true,
showClassLibrary: true,
}
};

export const getGroupConfig = (group: string): GroupConfig => {
return groupConfig[group] || groupConfig.defaultGroup;
};

export default groupConfig;