api-server/src/modules/users/users.repository.ts

64 lines
1.6 KiB
TypeScript
Raw Normal View History

import { Injectable, NotFoundException } from '@nestjs/common';
import { PrismaService } from '../../infrastructure/database/prisma.service';
@Injectable()
export class UsersRepository {
constructor(private readonly prisma: PrismaService) {}
async findProfileByUserId(userId: string) {
const user = await this.prisma.user.findUnique({
where: { id: 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: user.id,
email: user.email,
nickname: user.nickname,
avatarUrl: user.avatarUrl,
role: user.role,
status: user.status,
onboardingCompleted: user.onboardingCompleted,
createdAt: user.createdAt,
};
}
async updateProfile(userId: string, dto: any) {
return this.prisma.user.update({
where: { id: userId },
data: {
nickname: dto.nickname,
avatarUrl: dto.avatarUrl,
},
});
}
async updatePreferences(userId: string, dto: any) {
return this.prisma.userPreference.upsert({
where: { userId },
create: {
userId,
defaultFocusMinutes: dto.defaultFocusMinutes ?? 25,
aiSuggestionLevel: dto.aiSuggestionLevel ?? 'normal',
language: dto.language ?? 'zh-CN',
appearance: dto.appearance ?? 'system',
notificationEnabled: dto.notificationEnabled ?? true,
},
update: dto,
});
}
}