From 0c805726cf12958ff104c9afe0f389fc95c6d7b7 Mon Sep 17 00:00:00 2001 From: ditiqi Date: Fri, 21 Feb 2025 08:14:04 +0800 Subject: [PATCH] add --- apps/server/src/models/post/post.service.ts | 3 ++- apps/server/src/models/post/utils.ts | 27 +++++++++++++++------ 2 files changed, 21 insertions(+), 9 deletions(-) diff --git a/apps/server/src/models/post/post.service.ts b/apps/server/src/models/post/post.service.ts index c8cd7ba..df99714 100755 --- a/apps/server/src/models/post/post.service.ts +++ b/apps/server/src/models/post/post.service.ts @@ -15,7 +15,7 @@ import { import { MessageService } from '../message/message.service'; import { BaseService } from '../base/base.service'; import { DepartmentService } from '../department/department.service'; -import { setPostRelation } from './utils'; +import { setCourseInfo, setPostRelation } from './utils'; import EventBus, { CrudOperation } from '@server/utils/event-bus'; import { BaseTreeService } from '../base/base.tree.service'; import { z } from 'zod'; @@ -180,6 +180,7 @@ export class PostService extends BaseTreeService { if (result) { await setPostRelation({ data: result, staff }); await this.setPerms(result, staff); + await setCourseInfo({ data: result }); } return result; diff --git a/apps/server/src/models/post/utils.ts b/apps/server/src/models/post/utils.ts index e2fb89b..40ea595 100644 --- a/apps/server/src/models/post/utils.ts +++ b/apps/server/src/models/post/utils.ts @@ -3,6 +3,7 @@ import { EnrollmentStatus, Post, PostType, + SectionDto, UserProfile, VisitType, } from '@nice/common'; @@ -123,7 +124,7 @@ export async function updateCourseEnrollmentStats(courseId: string) { }, }); } -export async function setCourseInfo(data: Post) { +export async function setCourseInfo({ data }: { data: Post }) { if (data?.type === PostType.COURSE) { const ancestries = await db.postAncestry.findMany({ where: { @@ -135,18 +136,28 @@ export async function setCourseInfo(data: Post) { }, }); const descendants = ancestries.map((ancestry) => ancestry.descendant); - const sections = descendants.filter((descendant) => { - return ( - descendant.type === PostType.SECTION && descendant.parentId === data.id - ); - }); + const sections: SectionDto[] = descendants + .filter((descendant) => { + return ( + descendant.type === PostType.SECTION && + descendant.parentId === data.id + ); + }) + .map((section) => ({ + ...section, + lectures: [], + })); const lectures = descendants.filter((descendant) => { return ( descendant.type === PostType.LECTURE && sections.map((section) => section.id).includes(descendant.parentId) ); }); + sections.forEach((section) => { + section.lectures = lectures.filter( + (lecture) => lecture.parentId === section.id, + ); + }); + Object.assign(data, { sections }); } - - Object.assign(data, {}); }