All files / services groups.service.ts

100% Statements 20/20
100% Branches 8/8
100% Functions 7/7
100% Lines 17/17

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                                                                                                        26x   26x 26x 26x   26x     21x 1x           20x             11x 9x             4x 3x             3x 2x             3x             3x             3x    
/**
 * Groups API service
 * Handles all API calls related to group management
 */
 
import { apiClient } from '@/lib/api';
import type { User } from './users.service';
 
// Types
export interface Group {
  id: string;
  account_id: string;
  name: string;
  description: string | null;
  created_at: string;
  updated_at: string;
  member_count?: number;
  users?: User[];
}
 
export interface GroupListParams {
  skip?: number;
  limit?: number;
  search?: string;
}
 
export interface CreateGroupRequest {
  name: string;
  description?: string | null;
  account_id?: string;
}
 
export interface UpdateGroupRequest {
  name?: string;
  description?: string | null;
}
 
export interface AddUserToGroupRequest {
  user_id: string;
}
 
export interface GroupListResponse {
  items: Group[];
  total: number;
}
 
// API Functions
 
/**
 * Get list of groups with optional filtering and pagination
 */
export async function getGroups(params?: GroupListParams): Promise<GroupListResponse> {
  const queryParams = new URLSearchParams();
  
  if (params?.skip !== undefined) queryParams.append('skip', params.skip.toString());
  if (params?.limit !== undefined) queryParams.append('limit', params.limit.toString());
  if (params?.search) queryParams.append('search', params.search);
 
  const response = await apiClient.get(`/groups?${queryParams.toString()}`);
  
  // Backend returns array directly, we need to wrap it for consistency
  if (Array.isArray(response.data)) {
    return {
      items: response.data,
      total: response.data.length,
    };
  }
  
  return response.data;
}
 
/**
 * Get a single group by ID
 */
export async function getGroup(id: string): Promise<Group> {
  const response = await apiClient.get(`/groups/${id}`);
  return response.data;
}
 
/**
 * Create a new group
 */
export async function createGroup(data: CreateGroupRequest): Promise<Group> {
  const response = await apiClient.post('/groups', data);
  return response.data;
}
 
/**
 * Update an existing group
 */
export async function updateGroup(id: string, data: UpdateGroupRequest): Promise<Group> {
  const response = await apiClient.patch(`/groups/${id}`, data);
  return response.data;
}
 
/**
 * Delete a group
 */
export async function deleteGroup(id: string): Promise<void> {
  await apiClient.delete(`/groups/${id}`);
}
 
/**
 * Add a user to a group
 */
export async function addUserToGroup(groupId: string, userId: string): Promise<void> {
  await apiClient.post(`/groups/${groupId}/users`, { user_id: userId });
}
 
/**
 * Remove a user from a group
 */
export async function removeUserFromGroup(groupId: string, userId: string): Promise<void> {
  await apiClient.delete(`/groups/${groupId}/users/${userId}`);
}