All files / services accounts.service.ts

100% Statements 17/17
100% Branches 3/3
100% Functions 6/6
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 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137                                                                                                                                            10x     105x     99x           10x     4x 3x           10x     4x 3x           10x       3x       2x           10x 3x           10x         3x           3x    
/**
 * Accounts API service
 * Handles API calls for account management
 */
 
import { apiClient } from "@/lib/api";
 
export interface Account {
  id: string;
  account_code: string;
  slug: string;
  name: string;
  created_at: string;
  updated_at: string;
}
 
export interface AccountListItem extends Account {
  user_count: number;
  status: string;
}
 
export interface AccountListResponse {
  items: AccountListItem[];
  total: number;
  page: number;
  page_size: number;
  total_pages: number;
}
 
export interface AccountDetail extends Account {
  user_count: number;
  collections_used: string[];
}
 
export interface CreateAccountData {
  name: string;
  slug?: string;
}
 
export interface UpdateAccountData {
  name: string;
}
 
export interface AccountUser {
  id: string;
  email: string;
  role: string;
  is_active: boolean;
  created_at: string;
}
 
export interface AccountUsersResponse {
  items: AccountUser[];
  total: number;
  page: number;
  page_size: number;
  total_pages: number;
}
 
export interface GetAccountsParams {
  page?: number;
  page_size?: number;
  sort_by?: string;
  sort_order?: "asc" | "desc";
  search?: string;
}
 
/**
 * Get list of accounts with pagination and search
 */
export const getAccounts = async (
  params: GetAccountsParams = {}
): Promise<AccountListResponse> => {
  const response = await apiClient.get<AccountListResponse>("/accounts", {
    params,
  });
  return response.data;
};
 
/**
 * Get account by ID
 */
export const getAccountById = async (
  accountId: string
): Promise<AccountDetail> => {
  const response = await apiClient.get<AccountDetail>(`/accounts/${accountId}`);
  return response.data;
};
 
/**
 * Create a new account
 */
export const createAccount = async (
  data: CreateAccountData
): Promise<AccountDetail> => {
  const response = await apiClient.post<AccountDetail>("/accounts", data);
  return response.data;
};
 
/**
 * Update an account
 */
export const updateAccount = async (
  accountId: string,
  data: UpdateAccountData
): Promise<AccountDetail> => {
  const response = await apiClient.put<AccountDetail>(
    `/accounts/${accountId}`,
    data
  );
  return response.data;
};
 
/**
 * Delete an account
 */
export const deleteAccount = async (accountId: string): Promise<void> => {
  await apiClient.delete(`/accounts/${accountId}`);
};
 
/**
 * Get users in an account
 */
export const getAccountUsers = async (
  accountId: string,
  page: number = 1,
  page_size: number = 25
): Promise<AccountUsersResponse> => {
  const response = await apiClient.get<AccountUsersResponse>(
    `/accounts/${accountId}/users`,
    {
      params: { page, page_size },
    }
  );
  return response.data;
};