add update user api
This commit is contained in:
parent
c411f2a8a4
commit
5d508c4731
3 changed files with 64 additions and 2 deletions
|
|
@ -1,4 +1,4 @@
|
||||||
import { Get, Body, Post, Route, Tags, SuccessResponse, Response, Example, Controller, Security, Request } from 'tsoa';
|
import { Get, Body, Post, Route, Tags, SuccessResponse, Response, Example, Controller, Security, Request, Put } from 'tsoa';
|
||||||
import { UserService } from '../services/user.service';
|
import { UserService } from '../services/user.service';
|
||||||
import {
|
import {
|
||||||
UserResponse,
|
UserResponse,
|
||||||
|
|
@ -36,6 +36,23 @@ export class UserController {
|
||||||
return await this.userService.getUserProfile(token);
|
return await this.userService.getUserProfile(token);
|
||||||
}
|
}
|
||||||
|
|
||||||
|
@Put('me')
|
||||||
|
@Security('jwt')
|
||||||
|
@SuccessResponse('200', 'Profile updated successfully')
|
||||||
|
@Response('401', 'Invalid or expired token')
|
||||||
|
@Response('400', 'Validation error')
|
||||||
|
public async updateProfile(@Request() request: any, @Body() body: ProfileUpdate): Promise<ProfileUpdateResponse> {
|
||||||
|
const { error } = profileUpdateSchema.validate(body);
|
||||||
|
if (error) {
|
||||||
|
throw new ValidationError(error.details[0].message);
|
||||||
|
}
|
||||||
|
const token = request.headers.authorization?.replace('Bearer ', '');
|
||||||
|
if (!token) {
|
||||||
|
throw new ValidationError('No token provided');
|
||||||
|
}
|
||||||
|
return await this.userService.updateProfile(token, body);
|
||||||
|
}
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* Change password
|
* Change password
|
||||||
* @summary Change user password using old password
|
* @summary Change user password using old password
|
||||||
|
|
|
||||||
|
|
@ -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
|
* Format user response
|
||||||
*/
|
*/
|
||||||
|
|
|
||||||
|
|
@ -18,7 +18,7 @@ export const profileUpdateSchema = Joi.object({
|
||||||
.max(15)
|
.max(15)
|
||||||
.optional(),
|
.optional(),
|
||||||
avatar_url: Joi.string().optional(),
|
avatar_url: Joi.string().optional(),
|
||||||
birthday: Joi.date().optional()
|
birth_date: Joi.date().optional()
|
||||||
});
|
});
|
||||||
|
|
||||||
export const changePasswordSchema = Joi.object({
|
export const changePasswordSchema = Joi.object({
|
||||||
|
|
|
||||||
Loading…
Add table
Add a link
Reference in a new issue