api-server/src/modules/learning-session/learning-session.repository.ts
WangDL 08f31dd5b6 feat: P0 后端补全 — BullMQ Workers 注册 + 用户 Profile API + 角色权限
- AppModule 注册 3 个 BullMQ Workers (AiAnalysis/DocumentImport/Notification)
- Users 模块新增 GET/PATCH /users/me/profile 端点:
  - GET 读取 UserProfile (learningIdentity, learningDirection, bio, currentGoal)
  - PATCH upsert UserProfile
  - GET /users/me 返回 profile + preferences (include join)
- 新增 RolesGuard + @Roles() 装饰器 (UserRole enum)
- QueueModule/QueueService 改进
- 各模块 controller/repository/service 完善

Co-Authored-By: Claude Opus 4.7 <noreply@anthropic.com>
2026-05-17 19:08:07 +08:00

52 lines
1.4 KiB
TypeScript

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, pagination?: { page?: number; limit?: number }) {
const page = pagination?.page ?? 1;
const limit = pagination?.limit ?? 20;
return this.prisma.learningSession.findMany({
where: { userId },
orderBy: { startedAt: 'desc' },
skip: (page - 1) * limit,
take: limit,
});
}
}