Fix new services implementation 7
This commit is contained in:
@@ -95,14 +95,39 @@ export class TenantService {
|
||||
*/
|
||||
async getUserTenants(): Promise<TenantInfo[]> {
|
||||
try {
|
||||
// First get current user info to get user ID
|
||||
const currentUser = await apiClient.get(`/users/me`);
|
||||
const userId = currentUser.id;
|
||||
// Extract user ID from the JWT token in localStorage
|
||||
const token = localStorage.getItem('auth_token');
|
||||
console.log('🔑 TenantService: Auth token present:', !!token);
|
||||
|
||||
// Then get tenants owned by this user
|
||||
return apiClient.get(`${this.baseEndpoint}/user/${userId}`);
|
||||
if (!token) {
|
||||
throw new Error('No auth token found');
|
||||
}
|
||||
|
||||
// Decode JWT to get user ID (simple base64 decode)
|
||||
const payload = JSON.parse(atob(token.split('.')[1]));
|
||||
const userId = payload.user_id || payload.sub;
|
||||
console.log('👤 TenantService: Extracted user ID:', userId);
|
||||
|
||||
if (!userId) {
|
||||
throw new Error('No user ID found in token');
|
||||
}
|
||||
|
||||
// Get tenants owned by this user
|
||||
const url = `${this.baseEndpoint}/user/${userId}/owned`;
|
||||
console.log('🌐 TenantService: Making request to:', url);
|
||||
|
||||
const result = await apiClient.get(url);
|
||||
console.log('📦 TenantService: API response:', result);
|
||||
console.log('📏 TenantService: Response length:', Array.isArray(result) ? result.length : 'Not an array');
|
||||
|
||||
if (Array.isArray(result) && result.length > 0) {
|
||||
console.log('✅ TenantService: First tenant:', result[0]);
|
||||
console.log('🆔 TenantService: First tenant ID:', result[0]?.id);
|
||||
}
|
||||
|
||||
return result;
|
||||
} catch (error) {
|
||||
console.error('Failed to get user tenants:', error);
|
||||
console.error('❌ TenantService: Failed to get user tenants:', error);
|
||||
// Return empty array if API call fails
|
||||
return [];
|
||||
}
|
||||
|
||||
@@ -104,17 +104,36 @@ export const useTenantId = () => {
|
||||
const fetchTenantIdFromAPI = useCallback(async (): Promise<string | null> => {
|
||||
try {
|
||||
setIsLoading(true);
|
||||
console.log('🔍 useTenantId: Fetching tenants from API...');
|
||||
const tenants = await tenantService.getUserTenants();
|
||||
console.log('📋 useTenantId: Received tenants:', tenants);
|
||||
console.log('📊 useTenantId: Tenants array length:', tenants?.length);
|
||||
|
||||
if (tenants.length > 0) {
|
||||
const firstTenantId = tenants[0].id;
|
||||
// Handle both array and object responses
|
||||
let tenantsArray;
|
||||
if (Array.isArray(tenants)) {
|
||||
tenantsArray = tenants;
|
||||
} else if (tenants && typeof tenants === 'object') {
|
||||
// Convert object with numeric keys to array
|
||||
tenantsArray = Object.values(tenants);
|
||||
} else {
|
||||
tenantsArray = [];
|
||||
}
|
||||
|
||||
console.log('🔄 useTenantId: Converted to array:', tenantsArray);
|
||||
console.log('📏 useTenantId: Array length:', tenantsArray.length);
|
||||
|
||||
if (tenantsArray && tenantsArray.length > 0) {
|
||||
const firstTenantId = tenantsArray[0].id;
|
||||
console.log('🎯 useTenantId: First tenant ID:', firstTenantId);
|
||||
storeTenantId(firstTenantId);
|
||||
return firstTenantId;
|
||||
}
|
||||
|
||||
console.log('❌ useTenantId: No tenants found or empty array');
|
||||
return null;
|
||||
} catch (error) {
|
||||
console.error('Failed to fetch tenant ID from API:', error);
|
||||
console.error('❌ useTenantId: Failed to fetch tenant ID from API:', error);
|
||||
setError('Failed to fetch tenant information');
|
||||
return null;
|
||||
} finally {
|
||||
|
||||
Reference in New Issue
Block a user