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

feat: lesson details and lesson progress #310

Merged
merged 4 commits into from
Dec 23, 2024
Merged
Show file tree
Hide file tree
Changes from 3 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 apps/api/src/chapter/chapter.service.ts
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@ import { Inject, Injectable, NotFoundException, UnauthorizedException } from "@n
import { EventBus } from "@nestjs/cqrs";

import { DatabasePg } from "src/common";
import { LessonRepository } from "src/lesson/lesson.repository";
import { LessonRepository } from "src/lesson/repositories/lesson.repository";

import { ChapterRepository } from "./repositories/chapter.repository";

Expand Down
6 changes: 1 addition & 5 deletions apps/api/src/chapter/chapter.type.ts
Original file line number Diff line number Diff line change
@@ -1,8 +1,4 @@
export const LESSON_TYPE = {
quiz: { key: "quiz", value: "Quiz" },
multimedia: { key: "multimedia", value: "Multimedia" },
} as const;

// TODO: remove unused types
export const LESSON_ITEM_TYPE = {
text_block: { key: "text_block", value: "Text Block" },
file: { key: "file", value: "File" },
Expand Down
1 change: 1 addition & 0 deletions apps/api/src/courses/course.controller.ts
Original file line number Diff line number Diff line change
Expand Up @@ -171,6 +171,7 @@ export class CourseController {
})
async getTeacherCourses(
@Query("authorId") authorId: UUIDType,
// TODO: extract to const
@Query("scope") scope: "all" | "enrolled" | "available" = "all",
@Query("excludeCourseId") excludeCourseId: UUIDType,
@CurrentUser("userId") currentUserId: UUIDType,
Expand Down
38 changes: 19 additions & 19 deletions apps/api/src/courses/course.service.ts
Original file line number Diff line number Diff line change
Expand Up @@ -20,7 +20,6 @@ import {
sql,
} from "drizzle-orm";

import { LESSON_TYPE } from "src/chapter/chapter.type";
import { AdminChapterRepository } from "src/chapter/repositories/adminChapter.repository";
import { DatabasePg } from "src/common";
import { addPagination, DEFAULT_PAGE_SIZE } from "src/common/pagination";
Expand Down Expand Up @@ -84,6 +83,7 @@ export class CourseService {
currentUserRole,
} = query;

// TODO: repair it
// const { sortOrder, sortedField } = getSortOptions(sort);
const sortOrder = asc;
const sortedField = CourseSortFields.title;
Expand Down Expand Up @@ -261,15 +261,15 @@ export class CourseService {
priceInCents: courses.priceInCents,
currency: courses.currency,
hasFreeChapters: sql<boolean>`
EXISTS (
SELECT 1
FROM ${chapters}
WHERE ${chapters.courseId} = ${courses.id}
AND ${chapters.isFreemium} = TRUE
)`,
EXISTS (
SELECT 1
FROM ${chapters}
WHERE ${chapters.courseId} = ${courses.id}
AND ${chapters.isFreemium} = TRUE
)
`,
})
.from(courses)
.leftJoin(studentCourses, eq(studentCourses.courseId, courses.id))
.leftJoin(categories, eq(courses.categoryId, categories.id))
.leftJoin(users, eq(courses.authorId, users.id))
.leftJoin(coursesSummaryStats, eq(courses.id, coursesSummaryStats.courseId))
Expand All @@ -283,11 +283,9 @@ export class CourseService {
users.firstName,
users.lastName,
users.email,
studentCourses.studentId,
categories.title,
coursesSummaryStats.freePurchasedCount,
coursesSummaryStats.paidPurchasedCount,
studentCourses.finishedChapterCount,
)
.orderBy(sortOrder(this.getColumnToSortBy(sortedField as CourseSortField)));

Expand All @@ -297,7 +295,6 @@ export class CourseService {
const [{ totalItems }] = await trx
.select({ totalItems: countDistinct(courses.id) })
.from(courses)
.leftJoin(studentCourses, eq(studentCourses.courseId, courses.id))
.leftJoin(categories, eq(courses.categoryId, categories.id))
.leftJoin(users, eq(courses.authorId, users.id))
.where(and(...conditions));
Expand Down Expand Up @@ -369,7 +366,7 @@ export class CourseService {
(SELECT COUNT(*)
FROM ${lessons}
WHERE ${lessons.chapterId} = ${chapters.id}
AND ${lessons.type} = ${LESSON_TYPE.quiz.key})::INTEGER`,
AND ${lessons.type} = ${LESSON_TYPES.QUIZ})::INTEGER`,
completedLessonCount: sql<number>`COALESCE(${studentChapterProgress.completedLessonCount}, 0)`,
chapterProgress: sql<ProgressStatus>`
CASE
Expand Down Expand Up @@ -398,7 +395,7 @@ export class CourseService {
ELSE 'not_started'
END AS status,
CASE
WHEN ${lessons.type} = ${LESSON_TYPES.quiz} THEN COUNT(${questions.id})
WHEN ${lessons.type} = ${LESSON_TYPES.QUIZ} THEN COUNT(${questions.id})
ELSE NULL
END AS "quizQuestionCount"
FROM ${lessons}
Expand Down Expand Up @@ -590,9 +587,9 @@ export class CourseService {
excludeCourseId,
);

if (availableCourseIds.length) {
conditions.push(inArray(courses.id, availableCourseIds));
}
if (!availableCourseIds.length) return [];

conditions.push(inArray(courses.id, availableCourseIds));
}

return this.db
Expand Down Expand Up @@ -620,7 +617,10 @@ export class CourseService {
)`,
})
.from(courses)
.leftJoin(studentCourses, eq(studentCourses.courseId, courses.id))
.leftJoin(
studentCourses,
and(eq(studentCourses.courseId, courses.id), eq(studentCourses.studentId, currentUserId)),
)
.leftJoin(categories, eq(courses.categoryId, categories.id))
.leftJoin(users, eq(courses.authorId, users.id))
.where(and(...conditions))
Expand Down Expand Up @@ -823,7 +823,7 @@ export class CourseService {
studentId,
lessonId: lesson.id,
completedQuestionCount: 0,
quizScore: lesson.type === LESSON_TYPES.quiz ? 0 : null,
quizScore: lesson.type === LESSON_TYPES.QUIZ ? 0 : null,
completedAt: null,
})),
);
Expand Down Expand Up @@ -934,7 +934,7 @@ export class CourseService {
const updatedLesson = { ...lesson };
if (
lesson.fileS3Key &&
(lesson.type === LESSON_TYPES.video || lesson.type === LESSON_TYPES.presentation)
(lesson.type === LESSON_TYPES.VIDEO || lesson.type === LESSON_TYPES.PRESENTATION)
) {
if (!lesson.fileS3Key.startsWith("https://")) {
try {
Expand Down
Loading
Loading