diff --git a/src/services/admin-credit-service.ts b/src/services/admin-credit-service.ts index 73b9bf4..67a79e6 100644 --- a/src/services/admin-credit-service.ts +++ b/src/services/admin-credit-service.ts @@ -27,11 +27,18 @@ export async function getAllUsersWithCredits(): Promise { // Check if current user is admin const { data: { user } } = await supabase.auth.getUser(); - if (!user) return []; + if (!user) { + console.log('[getAllUsersWithCredits] No user found'); + return []; + } const isAdmin = await checkIsAdmin(user.id); - if (!isAdmin) return []; + if (!isAdmin) { + console.log('[getAllUsersWithCredits] User is not admin'); + return []; + } + console.log('[getAllUsersWithCredits] Fetching profiles...'); // Get all users with their profiles const { data: profiles, error: profilesError } = await supabase .from('profiles') @@ -42,6 +49,8 @@ export async function getAllUsersWithCredits(): Promise { return []; } + console.log('[getAllUsersWithCredits] Found profiles:', profiles?.length); + // Get all user credits const { data: credits, error: creditsError } = await supabase .from('user_credits') @@ -49,25 +58,17 @@ export async function getAllUsersWithCredits(): Promise { if (creditsError) { console.error('Error fetching credits:', creditsError); - return []; } - // Get user emails from auth.users - const { data: { users }, error: usersError } = await supabase.auth.admin.listUsers(); + console.log('[getAllUsersWithCredits] Found credits:', credits?.length); - if (usersError) { - console.error('Error fetching users:', usersError); - return []; - } - - // Combine data + // Combine data - we'll use profile id as email fallback const usersWithCredits: UserWithCredits[] = profiles?.map(profile => { - const userAuth = users.find(u => u.id === profile.id); const userCredits = credits?.find(c => c.user_id === profile.id); return { id: profile.id, - email: userAuth?.email || 'Unknown', + email: profile.id.substring(0, 8) + '...', // Show partial ID as placeholder username: profile.username || 'Unknown', balance: userCredits?.balance || 0, total_purchased: userCredits?.total_purchased || 0, @@ -75,10 +76,11 @@ export async function getAllUsersWithCredits(): Promise { daily_limit: userCredits?.daily_limit || null, google_search_cost: userCredits?.google_search_cost || 1, gemini_ai_cost: userCredits?.gemini_ai_cost || 1, - last_active: userAuth?.last_sign_in_at || undefined + last_active: undefined }; }) || []; + console.log('[getAllUsersWithCredits] Returning users:', usersWithCredits.length); return usersWithCredits; } catch (err) { console.error('Error in getAllUsersWithCredits:', err);