This commit is contained in:
ditiqi 2025-02-21 08:14:04 +08:00
parent f9a9890a74
commit 0c805726cf
2 changed files with 21 additions and 9 deletions

View File

@ -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<Prisma.PostDelegate> {
if (result) {
await setPostRelation({ data: result, staff });
await this.setPerms(result, staff);
await setCourseInfo({ data: result });
}
return result;

View File

@ -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, {});
}