All files / src/hooks useBusiness.ts

100% Statements 61/61
100% Branches 56/56
100% Functions 12/12
100% Lines 53/53

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 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172                        2x   29x   29x       12x 12x 3x     9x     8x                                                                                                 2x 26x   26x   9x     9x 9x 9x 9x 9x 9x 9x 9x 9x 2x   9x 2x   9x 2x   9x 2x   9x 2x   9x 2x   9x 1x   9x 1x     9x 8x     8x               2x 11x     6x 5x                 2x 10x   10x   5x 4x     4x               2x 9x     4x 3x          
/**
 * Business Management Hooks
 */
 
import { useQuery, useMutation, useQueryClient } from '@tanstack/react-query';
import apiClient from '../api/client';
import { Business } from '../types';
import { getCookie } from '../utils/cookies';
 
/**
 * Hook to get current business
 */
export const useCurrentBusiness = () => {
  // Check token outside the query to use as dependency
  const token = getCookie('access_token');
 
  return useQuery<Business | null>({
    queryKey: ['currentBusiness', !!token], // Include token presence in query key to refetch when token changes
    queryFn: async () => {
      // Check if token exists before making request (from cookie)
      const currentToken = getCookie('access_token');
      if (!currentToken) {
        return null; // No token, return null instead of making request
      }
 
      const { data } = await apiClient.get('/business/current/');
 
      // Transform backend format to frontend format
      return {
        id: String(data.id),
        name: data.name,
        subdomain: data.subdomain,
        primaryColor: data.primary_color || '#3B82F6',  // Blue-500 default
        secondaryColor: data.secondary_color || '#1E40AF',  // Blue-800 default
        logoUrl: data.logo_url,
        emailLogoUrl: data.email_logo_url,
        logoDisplayMode: data.logo_display_mode || 'text-only',
        timezone: data.timezone || 'America/New_York',
        timezoneDisplayMode: data.timezone_display_mode || 'business',
        whitelabelEnabled: data.whitelabel_enabled,
        plan: data.tier, // Map tier to plan
        status: data.status,
        joinedAt: data.created_at ? new Date(data.created_at) : undefined,
        resourcesCanReschedule: data.resources_can_reschedule,
        requirePaymentMethodToBook: data.require_payment_method_to_book,
        cancellationWindowHours: data.cancellation_window_hours,
        lateCancellationFeePercent: data.late_cancellation_fee_percent,
        initialSetupComplete: data.initial_setup_complete,
        websitePages: data.website_pages || {},
        customerDashboardContent: data.customer_dashboard_content || [],
        paymentsEnabled: data.payments_enabled ?? false,
        // Platform-controlled permissions
        canManageOAuthCredentials: data.can_manage_oauth_credentials || false,
        // Plan permissions (what features are available based on subscription)
        planPermissions: data.plan_permissions || {
          sms_reminders: false,
          webhooks: false,
          api_access: false,
          custom_domain: false,
          white_label: false,
          custom_oauth: false,
          plugins: false,
          export_data: false,
          video_conferencing: false,
          two_factor_auth: false,
          masked_calling: false,
          pos_system: false,
          mobile_app: false,
        },
      };
    },
  });
};
 
/**
 * Hook to update business settings
 */
export const useUpdateBusiness = () => {
  const queryClient = useQueryClient();
 
  return useMutation({
    mutationFn: async (updates: Partial<Business>) => {
      const backendData: any = {};
 
      // Map frontend fields to backend fields
      if (updates.name) backendData.name = updates.name;
      if (updates.primaryColor !== undefined) backendData.primary_color = updates.primaryColor;
      if (updates.secondaryColor !== undefined) backendData.secondary_color = updates.secondaryColor;
      if (updates.logoUrl !== undefined) backendData.logo_url = updates.logoUrl;
      if (updates.emailLogoUrl !== undefined) backendData.email_logo_url = updates.emailLogoUrl;
      if (updates.logoDisplayMode !== undefined) backendData.logo_display_mode = updates.logoDisplayMode;
      if (updates.timezone !== undefined) backendData.timezone = updates.timezone;
      if (updates.timezoneDisplayMode !== undefined) backendData.timezone_display_mode = updates.timezoneDisplayMode;
      if (updates.whitelabelEnabled !== undefined) {
        backendData.whitelabel_enabled = updates.whitelabelEnabled;
      }
      if (updates.resourcesCanReschedule !== undefined) {
        backendData.resources_can_reschedule = updates.resourcesCanReschedule;
      }
      if (updates.requirePaymentMethodToBook !== undefined) {
        backendData.require_payment_method_to_book = updates.requirePaymentMethodToBook;
      }
      if (updates.cancellationWindowHours !== undefined) {
        backendData.cancellation_window_hours = updates.cancellationWindowHours;
      }
      if (updates.lateCancellationFeePercent !== undefined) {
        backendData.late_cancellation_fee_percent = updates.lateCancellationFeePercent;
      }
      if (updates.initialSetupComplete !== undefined) {
        backendData.initial_setup_complete = updates.initialSetupComplete;
      }
      if (updates.websitePages !== undefined) {
        backendData.website_pages = updates.websitePages;
      }
      if (updates.customerDashboardContent !== undefined) {
        backendData.customer_dashboard_content = updates.customerDashboardContent;
      }
 
      const { data } = await apiClient.patch('/business/current/update/', backendData);
      return data;
    },
    onSuccess: () => {
      queryClient.invalidateQueries({ queryKey: ['currentBusiness'] });
    },
  });
};
 
/**
 * Hook to get all resources for the current business
 */
export const useResources = () => {
  return useQuery({
    queryKey: ['resources'],
    queryFn: async () => {
      const { data } = await apiClient.get('/resources/');
      return data;
    },
    staleTime: 5 * 60 * 1000, // 5 minutes
  });
};
 
/**
 * Hook to create a new resource
 */
export const useCreateResource = () => {
  const queryClient = useQueryClient();
 
  return useMutation({
    mutationFn: async (resourceData: { name: string; type: string; user_id?: string }) => {
      const { data } = await apiClient.post('/resources/', resourceData);
      return data;
    },
    onSuccess: () => {
      queryClient.invalidateQueries({ queryKey: ['resources'] });
    },
  });
};
 
/**
 * Hook to get all users for the current business
 */
export const useBusinessUsers = () => {
  return useQuery({
    queryKey: ['businessUsers'],
    queryFn: async () => {
      const { data } = await apiClient.get('/staff/');
      return data;
    },
    staleTime: 5 * 60 * 1000, // 5 minutes
  });
};