api-server/src/modules/learning-session/learning-session.repository.ts

48 lines
1.2 KiB
TypeScript
Raw Normal View History

import { Injectable } from '@nestjs/common';
import { PrismaService } from '../../infrastructure/database/prisma.service';
@Injectable()
export class LearningSessionRepository {
constructor(private readonly prisma: PrismaService) {}
async create(userId: string, dto: {
knowledgeItemId?: string;
knowledgeBaseId?: string;
mode?: string;
}) {
return this.prisma.learningSession.create({
data: {
userId,
knowledgeItemId: dto.knowledgeItemId ?? null,
knowledgeBaseId: dto.knowledgeBaseId ?? null,
mode: dto.mode ?? 'reading',
status: 'active',
startedAt: new Date(),
},
});
}
async end(id: string) {
const session = await this.prisma.learningSession.findUnique({ where: { id } });
if (!session) return undefined;
return this.prisma.learningSession.update({
where: { id },
data: {
status: 'completed',
endedAt: new Date(),
durationSeconds: Math.floor(
(Date.now() - session.startedAt.getTime()) / 1000,
),
},
});
}
async findByUserId(userId: string) {
return this.prisma.learningSession.findMany({
where: { userId },
orderBy: { startedAt: 'desc' },
});
}
}