Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 1 addition & 1 deletion src/db/course.ts
Original file line number Diff line number Diff line change
Expand Up @@ -166,7 +166,7 @@ export const getNextVideo = async (currentVideoId: number) => {
return latestContent;
};

async function getAllContent(): Promise<
export async function getAllContent(): Promise<
{
id: number;
type: string;
Expand Down
33 changes: 25 additions & 8 deletions src/utiles/appx.ts
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@ import {
getAllCoursesAndContentHierarchy,
getAllVideos,
getVideoProgressForUser,
getAllContent,
} from '@/db/course';
import { authOptions } from '@/lib/auth';
import { Course } from '@/store/atoms';
Expand Down Expand Up @@ -153,26 +154,42 @@ export async function getPurchases(email: string): Promise<CoursesResponse> {
session?.user?.id || '',
true,
);
const allVideos = await getAllVideos();
const allContents = await getAllContent();

const completedVideosLookup: { [key: string]: boolean } =
userVideoProgress?.reduce((acc: any, progress) => {
acc[progress.contentId] = true;
return acc;
}, {});

const childrenMap = new Map<number, number[]>();
allContents.forEach(c => {
if (c.parentId) {
if (!childrenMap.has(c.parentId)) childrenMap.set(c.parentId, []);
childrenMap.get(c.parentId)!.push(c.id);
}
});

const courses = _courses.map((course) => {
let totalVideos = 0;
let totalVideosWatched = 0;

course.content.forEach(({ contentId }) => {
allVideos.forEach(({ parentId, id }) => {
if (parentId === contentId) {
totalVideos++;
if (completedVideosLookup[id]) {
totalVideosWatched++;
}
const queue: number[] = [contentId];
while(queue.length > 0) {
const currentId = queue.shift()!;
const content = allContents.find(c => c.id === currentId);

if (content?.type === 'video') {
totalVideos++;
if (completedVideosLookup[currentId]) {
totalVideosWatched++;
}
}
});

const children = childrenMap.get(currentId) || [];
queue.push(...children);
}
});

return {
Expand Down