forked from hangshuo652/aurak
feat: 添加历史管理和用户评估记录API
- GET /assessment/history: 获取用户评估历史(保留最近100条) - cleanupOldSessions: 保持最多3条记录(在创建新session时自动清理) - 复查记录保留完整历史(reviewHistory)
This commit is contained in:
@@ -135,6 +135,15 @@ export class AssessmentController {
|
|||||||
return this.assessmentService.generateCertificate(sessionId, userId, tenantId);
|
return this.assessmentService.generateCertificate(sessionId, userId, tenantId);
|
||||||
}
|
}
|
||||||
|
|
||||||
|
@Get('history')
|
||||||
|
@ApiOperation({ summary: 'Get current user assessment history (keep latest 3)' })
|
||||||
|
async getHistory(
|
||||||
|
@Request() req: any,
|
||||||
|
) {
|
||||||
|
const { id: userId } = req.user;
|
||||||
|
return this.assessmentService.getUserHistory(userId);
|
||||||
|
}
|
||||||
|
|
||||||
@Get('stats')
|
@Get('stats')
|
||||||
@ApiOperation({ summary: 'Get assessment statistics for admin' })
|
@ApiOperation({ summary: 'Get assessment statistics for admin' })
|
||||||
async getStats(
|
async getStats(
|
||||||
|
|||||||
@@ -553,6 +553,9 @@ private async getModel(tenantId: string): Promise<ChatOpenAI> {
|
|||||||
this.logger.log(
|
this.logger.log(
|
||||||
`[startSession] Session ${savedSession.id} created and saved`,
|
`[startSession] Session ${savedSession.id} created and saved`,
|
||||||
);
|
);
|
||||||
|
|
||||||
|
this.cleanupOldSessions(userId);
|
||||||
|
|
||||||
return savedSession;
|
return savedSession;
|
||||||
}
|
}
|
||||||
|
|
||||||
@@ -1487,4 +1490,29 @@ const initialState: Partial<EvaluationState> = {
|
|||||||
|
|
||||||
return session;
|
return session;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
async getUserHistory(userId: string): Promise<AssessmentSession[]> {
|
||||||
|
const sessions = await this.sessionRepository.find({
|
||||||
|
where: { userId, status: AssessmentStatus.COMPLETED },
|
||||||
|
order: { createdAt: 'DESC' },
|
||||||
|
take: 100,
|
||||||
|
relations: ['template'],
|
||||||
|
});
|
||||||
|
return sessions;
|
||||||
|
}
|
||||||
|
|
||||||
|
private async cleanupOldSessions(userId: string): Promise<void> {
|
||||||
|
const sessions = await this.sessionRepository.find({
|
||||||
|
where: { userId },
|
||||||
|
order: { createdAt: 'DESC' },
|
||||||
|
});
|
||||||
|
|
||||||
|
if (sessions.length > 3) {
|
||||||
|
const toDelete = sessions.slice(3);
|
||||||
|
for (const session of toDelete) {
|
||||||
|
await this.sessionRepository.remove(session);
|
||||||
|
}
|
||||||
|
this.logger.log(`[cleanupOldSessions] Deleted ${toDelete.length} old sessions for user ${userId}`);
|
||||||
|
}
|
||||||
|
}
|
||||||
}
|
}
|
||||||
|
|||||||
Reference in New Issue
Block a user