get me api

This commit is contained in:
JakkrapartXD 2026-01-13 17:55:00 +07:00
parent 815e8aeaf0
commit d8d3dff2e7
8 changed files with 1719 additions and 575 deletions

View file

@ -10,11 +10,11 @@ import {
LoginResponse,
RegisterResponse,
RefreshTokenResponse,
UserResponse,
ResetPasswordResponse,
ChangePasswordResponse,
ResetRequestResponse
} from '../types/auth.types';
import { UserResponse } from '../types/user.types';
import { UnauthorizedError, ValidationError, ForbiddenError } from '../middleware/errorHandler';
import nodemailer from 'nodemailer';
@ -323,6 +323,8 @@ export class AuthService {
id: user.id,
username: user.username,
email: user.email,
updated_at: user.updated_at,
created_at: user.created_at,
role: {
code: user.role.code,
name: user.role.name
@ -331,7 +333,9 @@ export class AuthService {
prefix: user.profile.prefix,
first_name: user.profile.first_name,
last_name: user.profile.last_name,
avatar_url: user.profile.avatar_url
phone: user.profile.phone,
avatar_url: user.profile.avatar_url,
birth_date: user.profile.birth_date
} : undefined
};
}

View file

@ -0,0 +1,91 @@
import { prisma } from '../config/database';
import { Prisma } from '@prisma/client';
import { config } from '../config';
import { logger } from '../config/logger';
import jwt from 'jsonwebtoken';
import {
UserResponse,
ProfileResponse,
ProfileUpdate,
ProfileUpdateResponse,
ChangePasswordRequest,
ChangePasswordResponse
} from '../types/user.types';
import { UnauthorizedError, ValidationError, ForbiddenError } from '../middleware/errorHandler';
export class UserService {
async getUserProfile(token: string): Promise<UserResponse> {
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
},
include: {
profile: true,
role: true
}
});
if (!user) throw new UnauthorizedError("User not found");
return {
id: user.id,
username: user.username,
email: user.email,
updated_at: user.updated_at,
created_at: user.created_at,
role: {
code: user.role.code,
name: user.role.name as { th: string; en: string }
},
profile: user.profile ? {
prefix: user.profile.prefix as { th?: string; en?: string } | undefined,
first_name: user.profile.first_name,
last_name: user.profile.last_name,
avatar_url: user.profile.avatar_url,
birth_date: user.profile.birth_date,
phone: user.profile.phone
} : undefined
};
} 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('Error fetching user profile:', error);
throw error;
}
};
/**
* Format user response
*/
private formatUserResponse(user: any): UserResponse {
return {
id: user.id,
username: user.username,
email: user.email,
updated_at: user.updated_at,
created_at: user.created_at,
role: {
code: user.role.code,
name: user.role.name
},
profile: user.profile ? {
prefix: user.profile.prefix,
first_name: user.profile.first_name,
last_name: user.profile.last_name,
avatar_url: user.profile.avatar_url,
phone: user.profile.phone,
birth_date: user.profile.birth_date
} : undefined
};
}
}