mirror of
https://github.com/margrop/wrong-notebook.git
synced 2026-12-06 12:49:59 +08:00
fix: 练习统计排除重复导入产生的记录
重复导入时写入的 duplicate_import 记录表示同一道题再次出错,不是一次练习,不再计入练习总数、正确率和分布图。 Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
This commit is contained in:
@@ -71,6 +71,36 @@ describe('/api/stats', () => {
|
||||
vi.useRealTimers();
|
||||
}
|
||||
});
|
||||
it('应该排除 duplicate_import 来源的记录', async () => {
|
||||
mocks.mockPrismaPracticeRecord.groupBy
|
||||
.mockResolvedValueOnce([
|
||||
{ subject: '数学', _count: { id: 8 } },
|
||||
])
|
||||
.mockResolvedValueOnce([
|
||||
{ difficulty: 'medium', _count: { id: 5 } },
|
||||
]);
|
||||
mocks.mockPrismaPracticeRecord.findMany.mockResolvedValue([
|
||||
{ createdAt: new Date(), isCorrect: true, difficulty: 'medium' },
|
||||
]);
|
||||
mocks.mockPrismaPracticeRecord.count
|
||||
.mockResolvedValueOnce(8)
|
||||
.mockResolvedValueOnce(5);
|
||||
|
||||
const response = await GET_PRACTICE_STATS();
|
||||
expect(response.status).toBe(200);
|
||||
|
||||
// 所有查询都应包含排除 duplicate_import 的条件
|
||||
expect(mocks.mockPrismaPracticeRecord.groupBy).toHaveBeenCalledTimes(2);
|
||||
for (const call of mocks.mockPrismaPracticeRecord.groupBy.mock.calls) {
|
||||
expect(call[0].where.source).toEqual({ not: 'duplicate_import' });
|
||||
}
|
||||
expect(mocks.mockPrismaPracticeRecord.findMany.mock.calls[0][0].where.source).toEqual({ not: 'duplicate_import' });
|
||||
expect(mocks.mockPrismaPracticeRecord.count).toHaveBeenCalledTimes(2);
|
||||
for (const call of mocks.mockPrismaPracticeRecord.count.mock.calls) {
|
||||
expect(call[0].where.source).toEqual({ not: 'duplicate_import' });
|
||||
}
|
||||
});
|
||||
|
||||
it('应该返回练习统计数据', async () => {
|
||||
// groupBy 被调用两次(subject + difficulty),按调用顺序 mock
|
||||
mocks.mockPrismaPracticeRecord.groupBy
|
||||
|
||||
@@ -1,4 +1,5 @@
|
||||
import { NextResponse } from "next/server";
|
||||
import type { Prisma } from "@prisma/client";
|
||||
import { prisma } from "@/lib/prisma";
|
||||
import { authOptions } from "@/lib/auth";
|
||||
import { getServerSession } from "next-auth";
|
||||
@@ -9,6 +10,9 @@ import { createLogger } from "@/lib/logger";
|
||||
|
||||
const logger = createLogger('api:stats:practice');
|
||||
|
||||
/** 重复导入产生的记录不计入练习统计 */
|
||||
const EXCLUDE_DUPLICATE_IMPORT = { source: { not: 'duplicate_import' } } satisfies Prisma.PracticeRecordWhereInput;
|
||||
|
||||
export async function GET() {
|
||||
const session = await getServerSession(authOptions);
|
||||
|
||||
@@ -25,12 +29,13 @@ export async function GET() {
|
||||
const [subjectGroups, activityStats, difficultyGroups, totalRecords, correctRecords] = await Promise.all([
|
||||
prisma.practiceRecord.groupBy({
|
||||
by: ['subject'],
|
||||
where: { userId },
|
||||
where: { userId, ...EXCLUDE_DUPLICATE_IMPORT },
|
||||
_count: { id: true }
|
||||
}),
|
||||
prisma.practiceRecord.findMany({
|
||||
where: {
|
||||
userId,
|
||||
...EXCLUDE_DUPLICATE_IMPORT,
|
||||
createdAt: {
|
||||
gte: getMonthStart(now, timeZone, -5),
|
||||
}
|
||||
@@ -43,11 +48,11 @@ export async function GET() {
|
||||
}),
|
||||
prisma.practiceRecord.groupBy({
|
||||
by: ['difficulty'],
|
||||
where: { userId },
|
||||
where: { userId, ...EXCLUDE_DUPLICATE_IMPORT },
|
||||
_count: { id: true }
|
||||
}),
|
||||
prisma.practiceRecord.count({ where: { userId } }),
|
||||
prisma.practiceRecord.count({ where: { userId, isCorrect: true } }),
|
||||
prisma.practiceRecord.count({ where: { userId, ...EXCLUDE_DUPLICATE_IMPORT } }),
|
||||
prisma.practiceRecord.count({ where: { userId, isCorrect: true, ...EXCLUDE_DUPLICATE_IMPORT } }),
|
||||
]);
|
||||
|
||||
// Process activity stats into monthly counts
|
||||
|
||||
Reference in New Issue
Block a user