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 { MessageService } from '../message/message.service';
import { BaseService } from '../base/base.service'; import { BaseService } from '../base/base.service';
import { DepartmentService } from '../department/department.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 EventBus, { CrudOperation } from '@server/utils/event-bus';
import { BaseTreeService } from '../base/base.tree.service'; import { BaseTreeService } from '../base/base.tree.service';
import { z } from 'zod'; import { z } from 'zod';
@ -180,6 +180,7 @@ export class PostService extends BaseTreeService<Prisma.PostDelegate> {
if (result) { if (result) {
await setPostRelation({ data: result, staff }); await setPostRelation({ data: result, staff });
await this.setPerms(result, staff); await this.setPerms(result, staff);
await setCourseInfo({ data: result });
} }
return result; return result;

View File

@ -3,6 +3,7 @@ import {
EnrollmentStatus, EnrollmentStatus,
Post, Post,
PostType, PostType,
SectionDto,
UserProfile, UserProfile,
VisitType, VisitType,
} from '@nice/common'; } 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) { if (data?.type === PostType.COURSE) {
const ancestries = await db.postAncestry.findMany({ const ancestries = await db.postAncestry.findMany({
where: { where: {
@ -135,18 +136,28 @@ export async function setCourseInfo(data: Post) {
}, },
}); });
const descendants = ancestries.map((ancestry) => ancestry.descendant); const descendants = ancestries.map((ancestry) => ancestry.descendant);
const sections = descendants.filter((descendant) => { const sections: SectionDto[] = descendants
return ( .filter((descendant) => {
descendant.type === PostType.SECTION && descendant.parentId === data.id return (
); descendant.type === PostType.SECTION &&
}); descendant.parentId === data.id
);
})
.map((section) => ({
...section,
lectures: [],
}));
const lectures = descendants.filter((descendant) => { const lectures = descendants.filter((descendant) => {
return ( return (
descendant.type === PostType.LECTURE && descendant.type === PostType.LECTURE &&
sections.map((section) => section.id).includes(descendant.parentId) 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, {});
} }