From 23c9d5709e12b7462df608a022108570bfaf621f Mon Sep 17 00:00:00 2001 From: LarryKwon <65128957+LarryKwon@users.noreply.github.com> Date: Mon, 26 Aug 2024 13:08:20 +0900 Subject: [PATCH] reduce the taken-lecture api response time --- src/common/entities/ECourse.ts | 12 +++++++ src/common/entities/ELecture.ts | 11 ++++++ src/modules/user/user.service.ts | 34 +++++++++++-------- src/prisma/repositories/course.repository.ts | 4 +-- src/prisma/repositories/lecture.repository.ts | 12 ++----- 5 files changed, 46 insertions(+), 27 deletions(-) diff --git a/src/common/entities/ECourse.ts b/src/common/entities/ECourse.ts index 811ca754..9e5787eb 100644 --- a/src/common/entities/ECourse.ts +++ b/src/common/entities/ECourse.ts @@ -19,4 +19,16 @@ export namespace ECourse { }, }); export type Details = Prisma.subject_courseGetPayload; + + export const DetailWithIsRead = Prisma.validator()( + { + include: { + ...Details.include, + subject_courseuser: true, + }, + }, + ); + export type DetailWithIsRead = Prisma.subject_courseGetPayload< + typeof DetailWithIsRead + >; } diff --git a/src/common/entities/ELecture.ts b/src/common/entities/ELecture.ts index b537c7c7..170746a3 100644 --- a/src/common/entities/ELecture.ts +++ b/src/common/entities/ELecture.ts @@ -40,6 +40,17 @@ export namespace ELecture { }); export type Details = Prisma.subject_lectureGetPayload; + export const DetailsWithCourse = + Prisma.validator()({ + include: { + ...Details.include, + course: true, + }, + }); + export type DetailsWithCourse = Prisma.subject_lectureGetPayload< + typeof DetailsWithCourse + >; + export function isDetails( lecture: ELecture.Extended | ELecture.Details, ): lecture is ELecture.Details { diff --git a/src/modules/user/user.service.ts b/src/modules/user/user.service.ts index 5efd53c0..95bd46f5 100644 --- a/src/modules/user/user.service.ts +++ b/src/modules/user/user.service.ts @@ -101,24 +101,28 @@ export class UserService { takenLecturesId, query.order ?? DEFAULT_ORDER, ); - return Promise.all( - courses.map(async (course) => { - const representativeLecture = getRepresentativeLecture(course.lecture); - const professorRaw = course.subject_course_professors.map( - (x) => x.professor, - ); - const result = toJsonCourseDetail( - course, - representativeLecture, - professorRaw, - ); - + return courses.map((course) => { + const representativeLecture = getRepresentativeLecture(course.lecture); + const professorRaw = course.subject_course_professors.map( + (x) => x.professor, + ); + const result = toJsonCourseDetail( + course, + representativeLecture, + professorRaw, + ); + const subjectCourseUser = course.subject_courseuser.filter( + (e) => e.user_profile_id === user.id && e.course_id === course.id, + )[0]; + if (!subjectCourseUser || !course.latest_written_datetime) + return addIsRead(result, false); + else return addIsRead( result, - await this.courseRepository.isUserSpecificRead(course.id, user.id), + course.latest_written_datetime > + subjectCourseUser?.latest_read_datetime, ); - }), - ); + }); } async getUserLikedReviews( diff --git a/src/prisma/repositories/course.repository.ts b/src/prisma/repositories/course.repository.ts index 8704d675..29020a48 100644 --- a/src/prisma/repositories/course.repository.ts +++ b/src/prisma/repositories/course.repository.ts @@ -369,7 +369,7 @@ export class CourseRepository { async getUserTakenCourses( takenLecturesId: number[], order: string[], - ): Promise { + ): Promise { const orderFilter: { [key: string]: string }[] = []; order.forEach((orderList) => { const orderDict: { [key: string]: string } = {}; @@ -381,7 +381,7 @@ export class CourseRepository { orderDict[orderBy[orderBy.length - 1]] = order; orderFilter.push(orderDict); }); - return await this.prisma.subject_course.findMany({ + return this.prisma.subject_course.findMany({ where: { lecture: { some: { diff --git a/src/prisma/repositories/lecture.repository.ts b/src/prisma/repositories/lecture.repository.ts index 77b4b4ca..628be66e 100644 --- a/src/prisma/repositories/lecture.repository.ts +++ b/src/prisma/repositories/lecture.repository.ts @@ -8,6 +8,7 @@ import { PrismaService } from '../prisma.service'; import { CourseRepository } from './course.repository'; import { FilterType } from '@src/common/types/types'; import SubjectClasstimeFilter = FilterType.SubjectClasstimeFilter; +import Details = ELecture.Details; @Injectable() export class LectureRepository { @@ -194,16 +195,7 @@ export class LectureRepository { }, include: { lecture: { - include: { - subject_lecture_professors: { - include: { - professor: true, - }, - }, - subject_department: true, - subject_examtime: true, - subject_classtime: true, - }, + include: Details.include, }, }, })