Files
xrlms/src/app/admin/id/mockData.ts

130 lines
5.0 KiB
TypeScript
Raw Normal View History

2025-11-19 02:17:39 +09:00
type RoleType = 'learner' | 'instructor' | 'admin';
type AccountStatus = 'active' | 'inactive';
export type UserRow = {
id: string;
joinDate: string;
name: string;
email: string;
role: RoleType;
status: AccountStatus;
};
2025-11-26 21:40:56 +09:00
// 더미 데이터 제거됨 - 이제 API에서 데이터를 가져옵니다
// 강사 목록 가져오기 함수 - API에서 가져오기
export async function getInstructors(): Promise<UserRow[]> {
try {
const token = typeof window !== 'undefined'
? (localStorage.getItem('token') || document.cookie
.split('; ')
.find(row => row.startsWith('token='))
?.split('=')[1])
: null;
const baseUrl = process.env.NEXT_PUBLIC_API_BASE_URL
2025-11-26 21:40:56 +09:00
? `${process.env.NEXT_PUBLIC_API_BASE_URL}/admin/users/compact`
: 'https://hrdi.coconutmeet.net/admin/users/compact';
// 쿼리 파라미터 추가: type=STUDENT, limit=10
const apiUrl = new URL(baseUrl);
apiUrl.searchParams.set('type', 'STUDENT');
apiUrl.searchParams.set('limit', '10');
2025-11-26 21:40:56 +09:00
console.log('🔍 [getInstructors] API 호출 정보:', {
url: apiUrl.toString(),
2025-11-26 21:40:56 +09:00
hasToken: !!token,
tokenLength: token?.length || 0
});
const response = await fetch(apiUrl.toString(), {
2025-11-26 21:40:56 +09:00
method: 'GET',
headers: {
'Content-Type': 'application/json',
...(token && { Authorization: `Bearer ${token}` }),
},
});
console.log('📡 [getInstructors] API 응답 상태:', {
status: response.status,
statusText: response.statusText,
ok: response.ok
});
if (!response.ok) {
const errorText = await response.text();
console.error('❌ [getInstructors] API 에러 응답:', errorText);
console.error('강사 목록 가져오기 실패:', response.status);
return [];
}
const data = await response.json();
console.log('📦 [getInstructors] 원본 API 응답 데이터:', {
type: typeof data,
isArray: Array.isArray(data),
length: Array.isArray(data) ? data.length : 'N/A',
data: data,
sampleItem: Array.isArray(data) && data.length > 0 ? data[0] : null
});
// API 응답 데이터를 UserRow 형식으로 변환하고 강사만 필터링 (null 값도 표시)
const users: UserRow[] = Array.isArray(data)
? data
.map((user: any, index: number) => {
// null 값을 명시적으로 처리 (null이면 "(없음)" 표시)
const getValue = (value: any, fallback: string = '(없음)') => {
if (value === null || value === undefined) return fallback;
if (typeof value === 'string' && value.trim() === '') return fallback;
return String(value);
};
const transformed = {
id: getValue(user.id || user.userId, `user-${index + 1}`),
joinDate: getValue(user.joinDate || user.createdAt || user.join_date, new Date().toISOString().split('T')[0]),
name: getValue(user.name || user.userName, '(없음)'),
email: getValue(user.email || user.userEmail, '(없음)'),
role: (user.role === 'instructor' || user.role === 'INSTRUCTOR' ? 'instructor' :
user.role === 'admin' || user.role === 'ADMIN' ? 'admin' : 'learner') as RoleType,
status: (user.status === 'inactive' || user.status === 'INACTIVE' ? 'inactive' : 'active') as AccountStatus,
};
// 모든 항목의 null 체크
console.log(`🔎 [getInstructors] [${index + 1}번째] 사용자 데이터 변환:`, {
원본: user,
변환됨: transformed,
null체크: {
id: user.id === null || user.id === undefined,
userId: user.userId === null || user.userId === undefined,
joinDate: user.joinDate === null || user.joinDate === undefined,
createdAt: user.createdAt === null || user.createdAt === undefined,
join_date: user.join_date === null || user.join_date === undefined,
name: user.name === null || user.name === undefined,
userName: user.userName === null || user.userName === undefined,
email: user.email === null || user.email === undefined,
userEmail: user.userEmail === null || user.userEmail === undefined,
role: user.role === null || user.role === undefined,
status: user.status === null || user.status === undefined,
}
});
return transformed;
})
.filter((user: UserRow) => user.role === 'instructor' && user.status === 'active')
: [];
console.log('✅ [getInstructors] 변환된 강사 데이터:', {
총개수: users.length,
필터링전개수: Array.isArray(data) ? data.length : 0,
데이터: users,
빈배열여부: users.length === 0
});
return users;
} catch (error) {
console.error('강사 목록 가져오기 오류:', error);
return [];
}
2025-11-19 02:17:39 +09:00
}