All files / services admin.service.ts

100% Statements 45/45
100% Branches 17/17
100% Functions 15/15
100% Lines 45/45

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                                                                                                          5x   30x 30x       31x 31x       27x 27x 3x   27x 25x       15x 15x 15x 1x   15x 15x       24x 24x 24x 24x 1x   24x 23x       5x 4x       3x       5x 5x 1x   5x 5x       4x 3x       3x 2x       2x 2x                         3x 2x               3x 3x       3x 2x       3x 2x      
import { apiClient as api } from '@/lib/api';
import type { AccountListResponse } from './accounts.service';
 
export interface ProviderSchema {
  category: string;
  provider_name: string;
  display_name: string;
  logo_url?: string;
  properties: Record<string, {
    type: string;
    title?: string;
    description?: string;
    default?: unknown;
    enum?: string[];
    format?: string;
    writeOnly?: boolean;
  }>;
  required?: string[];
}
 
export interface AvailableProvider {
  category: string;
  provider_name: string;
  display_name: string;
  logo_url: string;
  description?: string;
}
 
export interface ConfigStats {
  total: number;
  by_category: Record<string, number>;
}
 
export interface ConfigurationStats {
  system_configs: ConfigStats;
  account_configs: ConfigStats;
}
 
export interface Configuration {
  id: string;
  display_name: string;
  provider_name: string;
  category: string;
  updated_at: string;
  is_system: boolean;
  is_builtin?: boolean;
  is_default?: boolean;
  account_id: string;
  logo_url: string;
  enabled: boolean;
  priority?: number;
}
 
export const adminService = {
  getStats: async (): Promise<ConfigurationStats> => {
    const response = await api.get('/admin/configuration/stats');
    return response.data;
  },
 
  getRecentConfigs: async (limit: number = 5): Promise<Configuration[]> => {
    const response = await api.get(`/admin/configuration/recent?limit=${limit}`);
    return response.data;
  },
 
  getSystemConfigs: async (category?: string): Promise<Configuration[]> => {
    const params = new URLSearchParams();
    if (category && category !== 'all') {
      params.append('category', category);
    }
    const response = await api.get(`/admin/configuration/system?${params.toString()}`);
    return response.data;
  },
 
  getAccountConfigs: async (accountId: string, category?: string): Promise<Configuration[]> => {
    const params = new URLSearchParams();
    params.append('account_id', accountId);
    if (category && category !== 'all') {
      params.append('category', category);
    }
    const response = await api.get(`/admin/configuration/account?${params.toString()}`);
    return response.data;
  },
 
  getAccounts: async (search?: string, page: number = 1, pageSize: number = 100): Promise<AccountListResponse> => {
    const params = new URLSearchParams();
    params.append('page', page.toString());
    params.append('page_size', pageSize.toString());
    if (search) {
      params.append('search', search);
    }
    const response = await api.get<AccountListResponse>(`/accounts?${params.toString()}`);
    return response.data;
  },
 
  updateConfig: async (configId: string, enabled: boolean): Promise<Configuration> => {
    const response = await api.patch<Configuration>(`/admin/configuration/${configId}`, { enabled });
    return response.data;
  },
 
  deleteConfig: async (configId: string): Promise<void> => {
    await api.delete(`/admin/configuration/${configId}`);
  },
 
  getAvailableProviders: async (category?: string): Promise<AvailableProvider[]> => {
    const params = new URLSearchParams();
    if (category && category !== 'all') {
      params.append('category', category);
    }
    const response = await api.get<AvailableProvider[]>(`/admin/configuration/providers?${params.toString()}`);
    return response.data;
  },
 
  getProviderSchema: async (category: string, providerName: string): Promise<ProviderSchema> => {
    const response = await api.get<ProviderSchema>(`/admin/configuration/schema/${category}/${providerName}`);
    return response.data;
  },
 
  getConfigValues: async (configId: string): Promise<Record<string, unknown>> => {
    const response = await api.get<Record<string, unknown>>(`/admin/configuration/${configId}/values`);
    return response.data;
  },
 
  updateConfigValues: async (configId: string, values: Record<string, unknown>): Promise<Configuration> => {
    const response = await api.patch<Configuration>(`/admin/configuration/${configId}/values`, values);
    return response.data;
  },
 
  createConfig: async (data: {
    category: string;
    provider_name: string;
    display_name: string;
    config: Record<string, unknown>;
    account_id?: string;
    logo_url?: string;
    enabled?: boolean;
    priority?: number;
  }): Promise<Configuration> => {
    const response = await api.post<Configuration>('/admin/configuration', data);
    return response.data;
  },
 
  testConnection: async (data: {
    category: string;
    provider_name: string;
    config: Record<string, unknown>;
  }): Promise<{ success: boolean; message: string }> => {
    const response = await api.post('/admin/configuration/test-connection', data);
    return response.data;
  },
 
  setDefaultConfig: async (configId: string): Promise<{ status: string; is_default: boolean; provider_name: string; display_name: string }> => {
    const response = await api.post(`/admin/configuration/${configId}/set-default`);
    return response.data;
  },
 
  unsetDefaultConfig: async (configId: string): Promise<{ status: string; is_default: boolean }> => {
    const response = await api.delete(`/admin/configuration/${configId}/set-default`);
    return response.data;
  },
};