add update user api

This commit is contained in:
JakkrapartXD 2026-01-14 16:29:18 +07:00
parent c411f2a8a4
commit 5d508c4731
3 changed files with 64 additions and 2 deletions

View file

@ -108,6 +108,51 @@ export class UserService {
}
}
/**
* Update user profile
*/
async updateProfile(token: string, profile: ProfileUpdate): Promise<ProfileUpdateResponse> {
try {
// Decode JWT token to get user ID
const decoded = jwt.verify(token, config.jwt.secret) as { id: number; username: string; email: string; roleCode: string };
const user = await prisma.user.findUnique({ where: { id: decoded.id } });
if (!user) throw new UnauthorizedError('User not found');
// Update profile
const updatedProfile = await prisma.userProfile.update({
where: { user_id: user.id },
data: profile
});
logger.info('Profile updated successfully', { userId: user.id });
return {
code: 200,
message: 'Profile updated successfully',
data: {
id: updatedProfile.id,
prefix: updatedProfile.prefix as { th?: string; en?: string } | undefined,
first_name: updatedProfile.first_name,
last_name: updatedProfile.last_name,
avatar_url: updatedProfile.avatar_url,
phone: updatedProfile.phone,
birth_date: updatedProfile.birth_date
}
};
} catch (error) {
if (error instanceof jwt.JsonWebTokenError) {
logger.error('Invalid JWT token:', error);
throw new UnauthorizedError('Invalid token');
}
if (error instanceof jwt.TokenExpiredError) {
logger.error('JWT token expired:', error);
throw new UnauthorizedError('Token expired');
}
logger.error('Failed to update profile', { error });
throw error;
}
}
/**
* Format user response
*/