2026-05-13 17:31:50 +08:00
|
|
|
import { Injectable, NotFoundException } from '@nestjs/common';
|
|
|
|
|
import { PrismaService } from '../../infrastructure/database/prisma.service';
|
2026-05-09 18:25:04 +08:00
|
|
|
|
|
|
|
|
@Injectable()
|
|
|
|
|
export class UsersRepository {
|
2026-05-13 17:31:50 +08:00
|
|
|
constructor(private readonly prisma: PrismaService) {}
|
2026-05-09 18:25:04 +08:00
|
|
|
|
|
|
|
|
async findProfileByUserId(userId: string) {
|
2026-05-13 17:31:50 +08:00
|
|
|
const user = await this.prisma.user.findUnique({
|
|
|
|
|
where: { id: BigInt(userId) },
|
|
|
|
|
select: {
|
|
|
|
|
id: true,
|
|
|
|
|
email: true,
|
|
|
|
|
nickname: true,
|
|
|
|
|
avatarUrl: true,
|
|
|
|
|
role: true,
|
|
|
|
|
status: true,
|
|
|
|
|
onboardingCompleted: true,
|
|
|
|
|
createdAt: true,
|
|
|
|
|
},
|
|
|
|
|
});
|
|
|
|
|
|
|
|
|
|
if (!user) {
|
|
|
|
|
throw new NotFoundException('用户不存在');
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
return {
|
|
|
|
|
id: String(user.id),
|
|
|
|
|
email: user.email,
|
|
|
|
|
nickname: user.nickname,
|
|
|
|
|
avatarUrl: user.avatarUrl,
|
|
|
|
|
role: user.role,
|
|
|
|
|
status: user.status,
|
|
|
|
|
onboardingCompleted: user.onboardingCompleted,
|
|
|
|
|
createdAt: user.createdAt,
|
2026-05-09 18:25:04 +08:00
|
|
|
};
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
async updateProfile(userId: string, dto: any) {
|
2026-05-13 17:31:50 +08:00
|
|
|
return this.prisma.user.update({
|
|
|
|
|
where: { id: BigInt(userId) },
|
|
|
|
|
data: {
|
|
|
|
|
nickname: dto.nickname,
|
|
|
|
|
avatarUrl: dto.avatarUrl,
|
|
|
|
|
},
|
|
|
|
|
});
|
2026-05-09 18:25:04 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
async updatePreferences(userId: string, dto: any) {
|
2026-05-13 17:31:50 +08:00
|
|
|
return this.prisma.userPreference.upsert({
|
|
|
|
|
where: { userId: BigInt(userId) },
|
|
|
|
|
create: {
|
|
|
|
|
userId: BigInt(userId),
|
|
|
|
|
defaultFocusMinutes: dto.defaultFocusMinutes ?? 25,
|
|
|
|
|
aiSuggestionLevel: dto.aiSuggestionLevel ?? 'normal',
|
|
|
|
|
language: dto.language ?? 'zh-CN',
|
|
|
|
|
appearance: dto.appearance ?? 'system',
|
|
|
|
|
notificationEnabled: dto.notificationEnabled ?? true,
|
|
|
|
|
},
|
|
|
|
|
update: dto,
|
|
|
|
|
});
|
2026-05-09 18:25:04 +08:00
|
|
|
}
|
|
|
|
|
}
|