All files / src/hooks useTenantExists.ts

90% Statements 9/10
83.33% Branches 5/6
100% Functions 2/2
100% Lines 9/9

Press n or j to go to the next uncovered block, b, p or k for the previous block.

1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51                              35x     13x   13x     13x       9x     4x 1x     3x               35x                
/**
 * Hook to check if a tenant (business) exists for the current subdomain
 * Returns loading state and whether the tenant exists
 */
 
import { useQuery } from '@tanstack/react-query';
import apiClient from '../api/client';
 
interface TenantExistsResult {
  exists: boolean;
  isLoading: boolean;
  error: Error | null;
}
 
export function useTenantExists(subdomain: string | null): TenantExistsResult {
  const { data, isLoading, error } = useQuery({
    queryKey: ['tenant-exists', subdomain],
    queryFn: async () => {
      Iif (!subdomain) return { exists: false };
 
      try {
        // Check if business exists by subdomain using the public lookup endpoint
        // Pass subdomain as query param to explicitly request that business
        const response = await apiClient.get('/business/public-info/', {
          params: { subdomain },
          headers: { 'X-Business-Subdomain': subdomain },
        });
        return { exists: true, business: response.data };
      } catch (err: any) {
        // 404 means the business doesn't exist
        if (err.response?.status === 404) {
          return { exists: false };
        }
        // Other errors - treat as doesn't exist for security
        return { exists: false };
      }
    },
    enabled: !!subdomain,
    retry: false, // Don't retry on 404s
    staleTime: 5 * 60 * 1000, // Cache for 5 minutes
  });
 
  return {
    exists: data?.exists ?? false,
    isLoading,
    error: error as Error | null,
  };
}
 
export default useTenantExists;