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

refactor: Resolve bugs related to teacher page #287

Merged
merged 3 commits into from
Dec 13, 2024
Merged
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
564 changes: 14 additions & 550 deletions apps/api/src/chapter/chapter.controller.ts

Large diffs are not rendered by default.

12 changes: 10 additions & 2 deletions apps/api/src/chapter/chapter.service.ts
Original file line number Diff line number Diff line change
Expand Up @@ -2,19 +2,27 @@ import { Inject, Injectable } from "@nestjs/common";
import { EventBus } from "@nestjs/cqrs";

import { DatabasePg } from "src/common";
import { FileService } from "src/file/file.service";

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

@Injectable()
export class ChapterService {
constructor(
@Inject("DB") private readonly db: DatabasePg,
private readonly fileService: FileService,
private readonly chapterRepository: ChapterRepository,
private readonly eventBus: EventBus,
) {}

// async getChapterWithDetails(id: UUIDType, userId: UUIDType, isStudent: boolean) {
// const hasCourseAccess = await this.chapterRepository.checkChapterAssignment(id, userId);
// if (!hasCourseAccess && isStudent) throw new Error("You don't have access to this chapter");

// const [chapter] = await this.chapterRepository.getChapterWithDetails(id, userId);
// if (!chapter) throw new Error("Chapter not found");

// return chapter;
// }

// async getChapter(
// id: UUIDType,
// userId: UUIDType,
Expand Down
Original file line number Diff line number Diff line change
@@ -1,7 +1,6 @@
import { Inject, Injectable } from "@nestjs/common";
import { eq, and, sql } from "drizzle-orm";


import { DatabasePg, type UUIDType } from "src/common";
import { chapters, lessons } from "src/storage/schema";

Expand Down
70 changes: 53 additions & 17 deletions apps/api/src/chapter/repositories/chapter.repository.ts
Original file line number Diff line number Diff line change
@@ -1,13 +1,64 @@
import { Inject, Injectable } from "@nestjs/common";
import { and, eq, sql } from "drizzle-orm";
import { and, eq, isNotNull, sql } from "drizzle-orm";

import { DatabasePg, type UUIDType } from "src/common";
import { chapters, studentChapterProgress, studentCourses } from "src/storage/schema";
import { chapters, lessons, studentChapterProgress, studentCourses } from "src/storage/schema";

@Injectable()
export class ChapterRepository {
constructor(@Inject("DB") private readonly db: DatabasePg) {}

async getChapterWithDetails(id: UUIDType, userId: UUIDType, isStudent: boolean) {
return await this.db
.select({
id: chapters.id,
title: chapters.title,
displayOrder: chapters.displayOrder,
lessonCount: chapters.lessonCount,
isFree: chapters.isFreemium,
lessons: sql<string>`
COALESCE(
(
SELECT json_agg(
json_build_object(
'id', ${lessons.id},
'type', ${lessons.type},
'displayOrder', ${lessons.displayOrder}
'title', ${lessons.title},
'description', ${lessons.description},
'fileS3Key', ${lessons.fileS3Key},
'fileType', ${lessons.fileType},
)
)
FROM ${lessons}
WHERE ${lessons.chapterId} = ${chapters.id}
ORDER BY ${lessons.displayOrder}
),
'[]'::json
)
`,
})
.from(chapters)
.where(and(eq(chapters.courseId, id), isNotNull(chapters.title)))
.orderBy(chapters.displayOrder);
}

async checkChapterAssignment(id: UUIDType, userId: UUIDType) {
return this.db
.select({
id: chapters.id,
isFreemium: chapters.isFreemium,
isAssigned: sql<boolean>`CASE WHEN ${studentCourses.id} IS NOT NULL THEN TRUE ELSE FALSE END`,
})
.from(chapters)
.leftJoin(
studentCourses,
and(eq(studentCourses.courseId, chapters.courseId), eq(studentCourses.studentId, userId)),
)
.where(and(eq(chapters.isPublished, true), eq(chapters.id, id)));
}

// TODO: check this functions \/
async getChapterForUser(id: UUIDType, userId: UUIDType) {
const [lesson] = await this.db
.select({
Expand Down Expand Up @@ -50,21 +101,6 @@ export class ChapterRepository {
return chapter;
}

async checkChapterAssignment(id: UUIDType, userId: UUIDType) {
return this.db
.select({
id: chapters.id,
isFreemium: chapters.isFreemium,
isAssigned: sql<boolean>`CASE WHEN ${studentCourses.id} IS NOT NULL THEN TRUE ELSE FALSE END`,
})
.from(chapters)
.leftJoin(
studentCourses,
and(eq(studentCourses.courseId, chapters.courseId), eq(studentCourses.studentId, userId)),
)
.where(and(eq(chapters.isPublished, true), eq(chapters.id, id)));
}

async getChapterProgressForStudent(chapterId: UUIDType, userId: UUIDType) {
const [chapterProgress] = await this.db
.select({})
Expand Down
16 changes: 8 additions & 8 deletions apps/api/src/courses/course.controller.ts
Original file line number Diff line number Diff line change
Expand Up @@ -100,7 +100,7 @@ export class CourseController {
@Query("page") page: number,
@Query("perPage") perPage: number,
@Query("sort") sort: SortCourseFieldsOptions,
@CurrentUser("userId") currentUserId: string,
@CurrentUser("userId") currentUserId: UUIDType,
): Promise<PaginatedResponse<AllCoursesResponse>> {
const filters: CoursesFilterSchema = {
title,
Expand Down Expand Up @@ -153,8 +153,8 @@ export class CourseController {
response: baseResponse(allCoursesSchema),
})
async getTeacherCourses(
@Query("authorId") authorId: string,
@CurrentUser("userId") currentUserId: string,
@Query("authorId") authorId: UUIDType,
@CurrentUser("userId") currentUserId: UUIDType,
): Promise<BaseResponse<AllCoursesForTeacherResponse>> {
return new BaseResponse(await this.courseService.getTeacherCourses(authorId));
}
Expand All @@ -165,8 +165,8 @@ export class CourseController {
response: baseResponse(commonShowCourseSchema),
})
async getCourse(
@Query("id") id: string,
@CurrentUser("userId") currentUserId: string,
@Query("id") id: UUIDType,
@CurrentUser("userId") currentUserId: UUIDType,
): Promise<BaseResponse<CommonShowCourse>> {
return new BaseResponse(await this.courseService.getCourse(id, currentUserId));
}
Expand All @@ -177,7 +177,7 @@ export class CourseController {
request: [{ type: "query", name: "id", schema: UUIDSchema, required: true }],
response: baseResponse(commonShowCourseSchema),
})
async getCourseById(@Query("id") id: string): Promise<BaseResponse<CommonShowCourse>> {
async getCourseById(@Query("id") id: UUIDType): Promise<BaseResponse<CommonShowCourse>> {
return new BaseResponse(await this.courseService.getCourseById(id));
}

Expand All @@ -199,7 +199,7 @@ export class CourseController {
})
async createCourse(
@Body() createCourseBody: CreateCourseBody,
@CurrentUser("userId") currentUserId: string,
@CurrentUser("userId") currentUserId: UUIDType,
): Promise<BaseResponse<{ id: UUIDType; message: string }>> {
const { id } = await this.courseService.createCourse(createCourseBody, currentUserId);

Expand All @@ -217,7 +217,7 @@ export class CourseController {
response: baseResponse(Type.Object({ message: Type.String() })),
})
async updateCourse(
@Param("id") id: string,
@Param("id") id: UUIDType,
@Body() updateCourseBody: UpdateCourseBody,
@UploadedFile() image: Express.Multer.File,
@CurrentUser("userId") currentUserId: UUIDType,
Expand Down
92 changes: 78 additions & 14 deletions apps/api/src/courses/course.service.ts
Original file line number Diff line number Diff line change
Expand Up @@ -20,7 +20,7 @@ import {
sql,
} from "drizzle-orm";

import { LESSON_ITEM_TYPE } from "src/chapter/chapter.type";
import { LESSON_ITEM_TYPE, LESSON_TYPE } from "src/chapter/chapter.type";
import { AdminChapterRepository } from "src/chapter/repositories/adminChapter.repository";
import { ChapterProgress } from "src/chapter/schemas/chapter.types";
import { DatabasePg } from "src/common";
Expand Down Expand Up @@ -284,7 +284,7 @@ export class CourseService {
.select({
id: courses.id,
title: courses.title,
imageUrl: sql<string>`${courses.thumbnailS3Key}`,
thumbnailS3Key: sql<string>`${courses.thumbnailS3Key}`,
category: categories.title,
description: sql<string>`${courses.description}`,
courseChapterCount: courses.chapterCount,
Expand All @@ -294,7 +294,9 @@ export class CourseService {
priceInCents: courses.priceInCents,
currency: courses.currency,
authorId: courses.authorId,
// is no needed on frontend *
author: sql<string>`${users.firstName} || ' ' || ${users.lastName}`,
// is no needed on frontend *
authorEmail: sql<string>`${users.email}`,
hasFreeChapter: sql<boolean>`
EXISTS (
Expand Down Expand Up @@ -327,9 +329,14 @@ export class CourseService {
WHERE ${studentChapterProgress.chapterId} = ${chapters.id}
AND ${studentChapterProgress.courseId} = ${course.id}
AND ${studentChapterProgress.studentId} = ${userId}
AND ${studentChapterProgress.completedAt}
AND ${studentChapterProgress.completedAt} IS NOT NULL
)::BOOLEAN`,
lessonCount: chapters.lessonCount,
quizCount: sql<number>`
(SELECT COUNT(*)
FROM ${lessons}
WHERE ${lessons.chapterId} = ${chapters.id}
AND ${lessons.type} = ${LESSON_TYPE.quiz.key})::INTEGER`,
completedLessonCount: sql<number>`COALESCE(${studentChapterProgress.completedLessonCount}, 0)`,
// TODO: add lessonProgressState to student lessons progress table
chapterProgress: sql<ChapterProgressType>`
Expand All @@ -342,13 +349,15 @@ export class CourseService {
) = (
SELECT COUNT(*)
FROM ${studentLessonProgress}
LEFT JOIN ${lessons} ON ${lessons.chapterId} = ${chapters.id}
WHERE ${studentLessonProgress.lessonId} = ${lessons.id}
AND ${studentLessonProgress.studentId} = ${userId}
)
THEN ${ChapterProgress.completed}
WHEN (
SELECT COUNT(*)
FROM ${studentLessonProgress}
LEFT JOIN ${lessons} ON ${lessons.id} = ${studentLessonProgress.lessonId}
WHERE ${studentLessonProgress.lessonId} = ${lessons.id}
AND ${studentLessonProgress.studentId} = ${userId}
) > 0
Expand All @@ -357,25 +366,59 @@ export class CourseService {
END)
`,
isFree: chapters.isFreemium,
lessons: sql<string>`
COALESCE(
(
SELECT json_agg(lesson_data)
FROM (
SELECT
${lessons.id} AS id,
${lessons.title} AS title,
${lessons.type} AS type,
${lessons.displayOrder} AS "displayOrder",
CASE
WHEN ${studentLessonProgress.completedAt} IS NOT NULL THEN 'completed'
WHEN ${studentLessonProgress.completedAt} IS NULL
AND ${studentLessonProgress.completedQuestionCount} > 0 THEN 'in_progress'
ELSE 'not_started'
END AS status,
CASE
WHEN ${lessons.type} = ${LESSON_TYPES.quiz} THEN COUNT(${questions.id})
ELSE NULL
END AS "quizQuestionCount"
FROM ${lessons}
LEFT JOIN ${studentLessonProgress} ON ${lessons.id} = ${studentLessonProgress.lessonId}
AND ${studentLessonProgress.studentId} = ${userId}
LEFT JOIN ${questions} ON ${lessons.id} = ${questions.lessonId}
WHERE ${lessons.chapterId} = ${chapters.id}
GROUP BY
${lessons.id},
${lessons.type},
${lessons.displayOrder},
${lessons.title},
${studentLessonProgress.completedAt},
${studentLessonProgress.completedQuestionCount}
ORDER BY ${lessons.displayOrder}
) AS lesson_data
),
'[]'::json
)
`,
})
.from(chapters)
.leftJoin(studentChapterProgress, eq(studentChapterProgress.chapterId, chapters.id))
.where(
and(
eq(chapters.courseId, id),
eq(chapters.isPublished, true),
isNotNull(chapters.id),
isNotNull(chapters.title),
),
and(eq(chapters.courseId, id), eq(chapters.isPublished, true), isNotNull(chapters.title)),
)
.orderBy(chapters.displayOrder);

// TODO: temporary fix
const getImageUrl = async (url: string) => {
if (!url || url.startsWith("https://")) return url;
if (!url || url.startsWith("https://")) return url ?? "";
return await this.fileService.getFileUrl(url);
};

const thumbnailUrl = await getImageUrl(course.imageUrl);
const thumbnailUrl = await getImageUrl(course.thumbnailS3Key);

return {
...course,
Expand Down Expand Up @@ -520,9 +563,32 @@ export class CourseService {
};
}

//TODO: Needs to be refactored
async getTeacherCourses(authorId: UUIDType): Promise<AllCoursesForTeacherResponse> {
return this.db
.select(this.getSelectField())
.select({
id: courses.id,
description: sql<string>`${courses.description}`,
title: courses.title,
thumbnailUrl: courses.thumbnailS3Key,
authorId: sql<string>`${courses.authorId}`,
author: sql<string>`CONCAT(${users.firstName} || ' ' || ${users.lastName})`,
authorEmail: sql<string>`${users.email}`,
category: sql<string>`${categories.title}`,
enrolled: sql<boolean>`CASE WHEN ${studentCourses.studentId} IS NOT NULL THEN true ELSE false END`,
enrolledParticipantCount: sql<number>`0`,
courseChapterCount: courses.chapterCount,
completedChapterCount: sql<number>`0`,
priceInCents: courses.priceInCents,
currency: courses.currency,
hasFreeChapters: sql<boolean>`
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))
Expand All @@ -545,8 +611,6 @@ export class CourseService {
users.email,
studentCourses.studentId,
categories.title,
coursesSummaryStats.freePurchasedCount,
coursesSummaryStats.paidPurchasedCount,
)
.orderBy(
sql<boolean>`CASE WHEN ${studentCourses.studentId} IS NULL THEN TRUE ELSE FALSE END`,
Expand Down
2 changes: 1 addition & 1 deletion apps/api/src/courses/schemas/course.schema.ts
Original file line number Diff line number Diff line change
Expand Up @@ -19,7 +19,7 @@ export const courseSchema = Type.Object({
currency: Type.String(),
isPublished: Type.Optional(Type.Boolean()),
createdAt: Type.Optional(Type.String()),
hasFreeLessons: Type.Optional(Type.Boolean()),
hasFreeChapters: Type.Optional(Type.Boolean()),
});

export const allCoursesSchema = Type.Array(courseSchema);
Expand Down
Loading
Loading