fix: 标签建议接口复用统一年级学段规则以支持大学与课标高中

This commit is contained in:
margrop
2026-12-03 20:19:47 +08:00
parent 0295bc7206
commit 7dfdcdfa85
2 changed files with 39 additions and 28 deletions

View File

@@ -354,6 +354,29 @@ describe('/api/tags', () => {
expect(data.suggestions).toContain('二元一次方程');
expect(data.suggestions).not.toContain('函数');
});
it('应该按学段过滤系统标签(含大学与课标高中根节点)并保留用户自定义标签', async () => {
const mockTags = [
{ ...createMockTag('大一', 'math', true, true), id: 'root-uni' },
{ ...createMockTag('极限与连续', 'math', true, false), parentId: 'root-uni' },
{ ...createMockTag('高中 选必一', 'math', true, true), id: 'root-sh' },
{ ...createMockTag('空间向量', 'math', true, false), parentId: 'root-sh' },
{ ...createMockTag('七年级上', 'math', true, true), id: 'root-jh' },
{ ...createMockTag('有理数', 'math', true, false), parentId: 'root-jh' },
{ ...createMockTag('我的自定义标签', 'math', false, false), userId: 'user-1' },
];
mocks.mockPrismaKnowledgeTag.findMany.mockResolvedValue(mockTags);
const uniRes = await GET_SUGGESTIONS(new Request('http://localhost/api/tags/suggestions?stage=university'));
const uniData = await uniRes.json();
expect(uniRes.status).toBe(200);
expect(uniData.suggestions).toEqual(['极限与连续', '我的自定义标签']);
const shRes = await GET_SUGGESTIONS(new Request('http://localhost/api/tags/suggestions?stage=senior_high'));
const shData = await shRes.json();
expect(shRes.status).toBe(200);
expect(shData.suggestions).toEqual(['空间向量', '我的自定义标签']);
});
});
describe('POST /api/tags (创建标签)', () => {

View File

@@ -1,8 +1,9 @@
import { NextResponse } from "next/server";
import { prisma } from "@/lib/prisma";
import { authOptions } from "@/lib/auth";
import { getServerSession } from "next-auth";
import { authOptions } from "@/lib/auth";
import { createLogger } from "@/lib/logger";
import { isGradeInStage, isNotebookStage } from "@/lib/notebook-scope";
import { prisma } from "@/lib/prisma";
import type { Prisma } from "@prisma/client";
const logger = createLogger('api:tags:suggestions');
@@ -69,33 +70,20 @@ export async function GET(req: Request) {
let suggestions = allTags.filter(t => t.children.length === 0);
// Filter by stage if provided
if (stage) {
const allowedGradePatterns: Record<string, string[]> = {
'primary': ['一年级', '二年级', '三年级', '四年级', '五年级', '六年级'],
'junior_high': ['七年级', '八年级', '九年级'],
'senior_high': ['高一', '高二', '高三'],
};
// 按学段过滤(复用 notebook-scope.ts 的统一年级正则,支持小学/初中/高中/大学)
if (stage && isNotebookStage(stage)) {
suggestions = suggestions.filter(tag => {
// 用户自定义标签不受系统学段树限制
if (!tag.isSystem) {
return true;
}
const filters = allowedGradePatterns[stage];
if (filters) {
suggestions = suggestions.filter(tag => {
// Always show user custom tags (non-system tags) regardless of stage filter
// unless we want to enforce structure on them too? Usually custom tags have flattened structure or no parent
if (!tag.isSystem) {
return true;
}
let current = tag;
// Traverse up to find root
while (current.parentId && tagMap.get(current.parentId)) {
current = tagMap.get(current.parentId)!;
}
// Current is now the root (or top-most loaded ancestor)
const isMatch = filters.some(f => current.name.includes(f));
return isMatch;
});
}
let current = tag;
while (current.parentId && tagMap.get(current.parentId)) {
current = tagMap.get(current.parentId)!;
}
return isGradeInStage(current.name, stage);
});
}
// Filter by query