feat: Implement user profile management and avatar upload functionality for regular users.

This commit is contained in:
pradeepkumar
2026-02-01 00:50:46 +05:30
parent eece0f7401
commit 6624b35e59
4 changed files with 218 additions and 4 deletions

View File

@@ -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;