2026-05-17 00:39:46 +08:00
|
|
|
import { Injectable, NotFoundException } from '@nestjs/common';
|
|
|
|
|
import { ReviewRepository } from './review.repository';
|
2026-05-18 10:07:57 +08:00
|
|
|
import { ReviewCardGenerationWorkflow } from '../ai/workflows/review-card-generation.workflow';
|
2026-05-09 18:25:04 +08:00
|
|
|
import { SubmitReviewDto } from './dto/submit-review.dto';
|
|
|
|
|
|
|
|
|
|
@Injectable()
|
2026-05-17 00:39:46 +08:00
|
|
|
export class ReviewService {
|
2026-05-18 10:07:57 +08:00
|
|
|
constructor(
|
|
|
|
|
private readonly reviewRepository: ReviewRepository,
|
|
|
|
|
private readonly cardGenerationWorkflow: ReviewCardGenerationWorkflow,
|
|
|
|
|
) {}
|
2026-05-09 18:25:04 +08:00
|
|
|
|
2026-05-17 00:39:46 +08:00
|
|
|
async getDueCards(userId: string) {
|
|
|
|
|
return this.reviewRepository.findDueCards(userId);
|
2026-05-09 18:25:04 +08:00
|
|
|
}
|
|
|
|
|
|
2026-05-17 00:39:46 +08:00
|
|
|
async submitReview(userId: string, id: string, dto: SubmitReviewDto) {
|
2026-05-09 18:25:04 +08:00
|
|
|
const card = await this.reviewRepository.findById(id);
|
|
|
|
|
if (!card) throw new NotFoundException(`Review card ${id} not found`);
|
|
|
|
|
const log = await this.reviewRepository.insertLog({
|
2026-05-17 00:39:46 +08:00
|
|
|
userId,
|
|
|
|
|
reviewCardId: id,
|
|
|
|
|
rating: dto.rating,
|
|
|
|
|
responseText: dto.responseText,
|
|
|
|
|
});
|
|
|
|
|
await this.reviewRepository.updateCard(id, {
|
|
|
|
|
status: 'reviewed',
|
|
|
|
|
nextReviewAt: new Date(Date.now() + 86400000),
|
2026-05-09 18:25:04 +08:00
|
|
|
});
|
|
|
|
|
return log;
|
|
|
|
|
}
|
2026-05-18 10:07:57 +08:00
|
|
|
|
|
|
|
|
async generateCards(userId: string, input: {
|
|
|
|
|
knowledgeItemTitle: string;
|
|
|
|
|
knowledgeItemContent: string;
|
|
|
|
|
cardCount?: number;
|
|
|
|
|
}) {
|
|
|
|
|
const result = await this.cardGenerationWorkflow.execute({
|
|
|
|
|
userId,
|
|
|
|
|
knowledgeItemTitle: input.knowledgeItemTitle,
|
|
|
|
|
knowledgeItemContent: input.knowledgeItemContent,
|
|
|
|
|
cardCount: input.cardCount,
|
|
|
|
|
});
|
|
|
|
|
|
|
|
|
|
const savedCards: any[] = [];
|
|
|
|
|
for (const card of result.cards) {
|
|
|
|
|
const saved = await this.reviewRepository.insertCard({
|
|
|
|
|
userId,
|
|
|
|
|
frontText: card.frontText,
|
|
|
|
|
backText: card.backText,
|
|
|
|
|
difficulty: card.difficulty,
|
|
|
|
|
status: 'active',
|
|
|
|
|
intervalDays: 1,
|
|
|
|
|
easeFactor: 2.5,
|
|
|
|
|
repetitionCount: 0,
|
|
|
|
|
lapseCount: 0,
|
|
|
|
|
nextReviewAt: new Date(),
|
|
|
|
|
});
|
|
|
|
|
savedCards.push(saved);
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
return { cards: savedCards, totalCount: result.totalCount };
|
|
|
|
|
}
|
2026-05-09 18:25:04 +08:00
|
|
|
}
|