Fix new services implementation 7

This commit is contained in:
Urtzi Alfaro
2025-08-15 22:40:19 +02:00
parent 277b1332cb
commit 399ba80067
15 changed files with 433 additions and 534 deletions

View File

@@ -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 [];
}