feat: Implement user profile management and avatar upload functionality for regular users.
This commit is contained in:
@@ -10,6 +10,105 @@ type UserWithProfiles = Prisma.UserGetPayload<{
|
||||
export class UsersService {
|
||||
constructor(private readonly prisma: PrismaService) {}
|
||||
|
||||
// =============================================
|
||||
// USER PROFILE METHODS (for regular users)
|
||||
// =============================================
|
||||
|
||||
async getMyUserProfile(userId: string) {
|
||||
const user = await this.prisma.user.findUnique({
|
||||
where: { id: userId },
|
||||
include: { userProfile: true },
|
||||
});
|
||||
|
||||
if (!user) {
|
||||
throw new NotFoundException('User not found');
|
||||
}
|
||||
|
||||
if (user.role !== UserRole.USER) {
|
||||
throw new BadRequestException('This endpoint is for regular users only');
|
||||
}
|
||||
|
||||
// Create user profile if it doesn't exist
|
||||
let profile = user.userProfile;
|
||||
if (!profile) {
|
||||
profile = await this.prisma.userProfile.create({
|
||||
data: {
|
||||
userId: user.id,
|
||||
},
|
||||
});
|
||||
}
|
||||
|
||||
return {
|
||||
id: profile.id,
|
||||
userId: user.id,
|
||||
email: user.email,
|
||||
firstName: profile.firstName,
|
||||
lastName: profile.lastName,
|
||||
phone: profile.phone,
|
||||
avatar: profile.avatar,
|
||||
city: profile.city,
|
||||
state: profile.state,
|
||||
country: profile.country,
|
||||
createdAt: profile.createdAt,
|
||||
updatedAt: profile.updatedAt,
|
||||
};
|
||||
}
|
||||
|
||||
async updateMyUserProfile(
|
||||
userId: string,
|
||||
data: {
|
||||
firstName?: string;
|
||||
lastName?: string;
|
||||
phone?: string;
|
||||
avatar?: string | null;
|
||||
city?: string;
|
||||
state?: string;
|
||||
country?: string;
|
||||
},
|
||||
) {
|
||||
const user = await this.prisma.user.findUnique({
|
||||
where: { id: userId },
|
||||
include: { userProfile: true },
|
||||
});
|
||||
|
||||
if (!user) {
|
||||
throw new NotFoundException('User not found');
|
||||
}
|
||||
|
||||
if (user.role !== UserRole.USER) {
|
||||
throw new BadRequestException('This endpoint is for regular users only');
|
||||
}
|
||||
|
||||
// Create or update user profile
|
||||
const profile = await this.prisma.userProfile.upsert({
|
||||
where: { userId },
|
||||
create: {
|
||||
userId,
|
||||
...data,
|
||||
},
|
||||
update: data,
|
||||
});
|
||||
|
||||
return {
|
||||
id: profile.id,
|
||||
userId: user.id,
|
||||
email: user.email,
|
||||
firstName: profile.firstName,
|
||||
lastName: profile.lastName,
|
||||
phone: profile.phone,
|
||||
avatar: profile.avatar,
|
||||
city: profile.city,
|
||||
state: profile.state,
|
||||
country: profile.country,
|
||||
createdAt: profile.createdAt,
|
||||
updatedAt: profile.updatedAt,
|
||||
};
|
||||
}
|
||||
|
||||
// =============================================
|
||||
// ADMIN METHODS
|
||||
// =============================================
|
||||
|
||||
async findAll(page = 1, limit = 10, role?: UserRole) {
|
||||
const skip = (page - 1) * limit;
|
||||
|
||||
|
||||
Reference in New Issue
Block a user