feat: Implement user profile management and avatar upload functionality for regular users.
This commit is contained in:
@@ -27,6 +27,62 @@ import { UserRole, UserStatus, VerificationStatus } from '@prisma/client';
|
||||
export class UsersController {
|
||||
constructor(private readonly usersService: UsersService) {}
|
||||
|
||||
// =============================================
|
||||
// USER PROFILE ENDPOINTS (for regular users)
|
||||
// =============================================
|
||||
|
||||
@Get('profile/me')
|
||||
@Roles(UserRole.USER)
|
||||
@ApiOperation({ summary: 'Get current user profile (Regular users only)' })
|
||||
@ApiResponse({
|
||||
status: 200,
|
||||
description: 'User profile retrieved',
|
||||
schema: {
|
||||
example: {
|
||||
id: 'uuid',
|
||||
userId: 'uuid',
|
||||
email: 'user@example.com',
|
||||
firstName: 'John',
|
||||
lastName: 'Doe',
|
||||
phone: '+1234567890',
|
||||
avatar: 'users/uuid/avatar/image.jpg',
|
||||
city: 'New York',
|
||||
state: 'NY',
|
||||
country: 'USA',
|
||||
},
|
||||
},
|
||||
})
|
||||
async getMyProfile(@CurrentUser() user: { id: string }) {
|
||||
return this.usersService.getMyUserProfile(user.id);
|
||||
}
|
||||
|
||||
@Patch('profile/me')
|
||||
@Roles(UserRole.USER)
|
||||
@ApiOperation({ summary: 'Update current user profile (Regular users only)' })
|
||||
@ApiResponse({
|
||||
status: 200,
|
||||
description: 'User profile updated',
|
||||
})
|
||||
async updateMyProfile(
|
||||
@CurrentUser() user: { id: string },
|
||||
@Body()
|
||||
data: {
|
||||
firstName?: string;
|
||||
lastName?: string;
|
||||
phone?: string;
|
||||
avatar?: string | null;
|
||||
city?: string;
|
||||
state?: string;
|
||||
country?: string;
|
||||
},
|
||||
) {
|
||||
return this.usersService.updateMyUserProfile(user.id, data);
|
||||
}
|
||||
|
||||
// =============================================
|
||||
// ADMIN ENDPOINTS
|
||||
// =============================================
|
||||
|
||||
@Get()
|
||||
@Roles(UserRole.ADMIN)
|
||||
@ApiOperation({ summary: 'Get all users (Admin only)' })
|
||||
|
||||
@@ -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