All files / src/hooks useUsers.ts

100% Statements 22/22
100% Branches 4/4
100% Functions 12/12
100% Lines 21/21

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                                      2x 12x     7x 6x                 2x 16x     8x 18x                           2x 20x     10x   9x 9x 27x 19x                         2x 21x   21x   10x 7x     7x        
import { useQuery, useMutation, useQueryClient } from '@tanstack/react-query';
import apiClient from '../api/client';
import { User } from '../types';
 
interface StaffUser {
  id: number | string;
  email: string;
  name: string; // This is the full_name from the serializer
  username?: string;
  role: string;
  is_active: boolean;
  permissions: Record<string, boolean>;
  can_invite_staff?: boolean;
}
 
/**
 * Hook to fetch all staff members (owners, managers, staff) for the current business.
 * Used for assignee dropdowns in tickets and other features.
 */
export const useUsers = () => {
  return useQuery<StaffUser[]>({
    queryKey: ['staff'],
    queryFn: async () => {
      const response = await apiClient.get('/staff/');
      return response.data;
    },
  });
};
 
/**
 * Hook to fetch staff members for assignee selection.
 * Returns users formatted for dropdown use.
 */
export const useStaffForAssignment = () => {
  return useQuery<{ id: string; name: string; email: string; role: string }[]>({
    queryKey: ['staffForAssignment'],
    queryFn: async () => {
      const response = await apiClient.get('/staff/');
      return response.data.map((user: StaffUser) => ({
        id: String(user.id),
        name: user.name || user.email, // 'name' field from serializer (full_name)
        email: user.email,
        role: user.role,
      }));
    },
  });
};
 
/**
 * Hook to fetch platform staff members for ticket assignment.
 * Returns platform admins (superuser, platform_manager, platform_support) formatted for dropdown use.
 */
export const usePlatformStaffForAssignment = () => {
  return useQuery<{ id: string; name: string; email: string; role: string }[]>({
    queryKey: ['platformStaffForAssignment'],
    queryFn: async () => {
      const response = await apiClient.get('/platform/users/');
      // Filter to only platform-level roles and format for dropdown
      const platformRoles = ['superuser', 'platform_manager', 'platform_support'];
      return response.data
        .filter((user: { role: string }) => platformRoles.includes(user.role))
        .map((user: { id: number; name?: string; email: string; role: string }) => ({
          id: String(user.id),
          name: user.name || user.email,
          email: user.email,
          role: user.role,
        }));
    },
  });
};
 
/**
 * Hook to update a staff member's permissions
 */
export const useUpdateStaffPermissions = () => {
  const queryClient = useQueryClient();
 
  return useMutation({
    mutationFn: async ({ userId, permissions }: { userId: string | number; permissions: Record<string, boolean> }) => {
      const response = await apiClient.patch(`/staff/${userId}/`, { permissions });
      return response.data;
    },
    onSuccess: () => {
      queryClient.invalidateQueries({ queryKey: ['staff'] });
    },
  });
};