49 lines
1.3 KiB
TypeScript
49 lines
1.3 KiB
TypeScript
|
|
import { Injectable } from '@nestjs/common';
|
||
|
|
import { generateShortId } from '../../common/utils/id.util';
|
||
|
|
|
||
|
|
export interface LearningSession {
|
||
|
|
id: string;
|
||
|
|
userId: string;
|
||
|
|
knowledgeItemId: string;
|
||
|
|
mode: string;
|
||
|
|
status: 'active' | 'completed';
|
||
|
|
startedAt: Date;
|
||
|
|
endedAt: Date | null;
|
||
|
|
durationSeconds: number;
|
||
|
|
}
|
||
|
|
|
||
|
|
@Injectable()
|
||
|
|
export class LearningSessionRepository {
|
||
|
|
private sessions: Map<string, LearningSession> = new Map();
|
||
|
|
|
||
|
|
async create(userId: string, dto: any): Promise<LearningSession> {
|
||
|
|
const session: LearningSession = {
|
||
|
|
id: generateShortId(),
|
||
|
|
userId,
|
||
|
|
knowledgeItemId: dto.knowledgeItemId || '',
|
||
|
|
mode: dto.mode || 'reading',
|
||
|
|
status: 'active',
|
||
|
|
startedAt: new Date(),
|
||
|
|
endedAt: null,
|
||
|
|
durationSeconds: 0,
|
||
|
|
};
|
||
|
|
this.sessions.set(session.id, session);
|
||
|
|
return session;
|
||
|
|
}
|
||
|
|
|
||
|
|
async end(id: string): Promise<LearningSession | undefined> {
|
||
|
|
const session = this.sessions.get(id);
|
||
|
|
if (!session) return undefined;
|
||
|
|
session.status = 'completed';
|
||
|
|
session.endedAt = new Date();
|
||
|
|
session.durationSeconds = Math.floor(
|
||
|
|
(session.endedAt.getTime() - session.startedAt.getTime()) / 1000,
|
||
|
|
);
|
||
|
|
return session;
|
||
|
|
}
|
||
|
|
|
||
|
|
async findByUserId(userId: string): Promise<LearningSession[]> {
|
||
|
|
return Array.from(this.sessions.values()).filter((s) => s.userId === userId);
|
||
|
|
}
|
||
|
|
}
|