import { apiClient } from "@/shared/api/axios.instance"; import type { AgentInfo, TokenCreate, TokenUser, LogEntry, LogFilters, InsertLogRequest, InsertLogsRequest, TokenUpdate, TokenUpdatePermissions, TokenPasswordReset, RegistrationRequest, DeployAgentsRequest, DeployResponse, SystemMetrics, } from "../types/agent.types"; import type { GraphApiResponse } from "@/modules/graph/types"; class AgentApiService { private readonly basePath = "/agents"; private readonly authBasePath = "/auth"; private readonly logsBasePath = "/logs"; async getAgents(): Promise { const response = await apiClient.get(this.basePath); return Array.isArray(response.data) ? response.data : []; } async getUsers(): Promise { const response = await apiClient.get( `${this.authBasePath}/tokens`, ); return Array.isArray(response.data) ? response.data : []; } async createUser(data: TokenCreate): Promise { await apiClient.post(`${this.authBasePath}/token`, data); } async deleteUser(login: string): Promise { await apiClient.delete(`${this.authBasePath}/tokens/${login}`); } async deleteMyAccount(): Promise { await apiClient.delete(`${this.authBasePath}/token`); } async searchLogs(filters?: LogFilters): Promise { const response = await apiClient.get(this.logsBasePath, { params: { level: filters?.level || undefined, service: filters?.service || undefined, agent: filters?.agent || undefined, date_from: filters?.date_from || undefined, date_to: filters?.date_to || undefined, limit: filters?.limit ?? 100, offset: filters?.offset ?? 0, }, }); if (!Array.isArray(response.data)) { console.error( "[Logs] Unexpected response format:", typeof response.data, response.data, ); return []; } return response.data; } async insertLog(entry: InsertLogRequest): Promise { await apiClient.post(this.logsBasePath, entry); } async insertLogsBatch(data: InsertLogsRequest): Promise { await apiClient.post(`${this.logsBasePath}/batch`, data); } async getDistinctAgents(): Promise { const response = await apiClient.get( `${this.logsBasePath}/agents`, ); return Array.isArray(response.data) ? response.data : []; } async getDistinctLevels(): Promise { const response = await apiClient.get( `${this.logsBasePath}/levels`, ); return Array.isArray(response.data) ? response.data : []; } async getDistinctServices(): Promise { const response = await apiClient.get( `${this.logsBasePath}/services`, ); return Array.isArray(response.data) ? response.data : []; } // User management methods async getUserByLogin(login: string): Promise { const response = await apiClient.get( `${this.authBasePath}/users/${login}`, ); if (!response.data || typeof response.data !== "object") { throw new Error(`User not found: ${login}`); } return response.data; } async getInactiveUsers(): Promise { const response = await apiClient.get( `${this.authBasePath}/users/inactive`, ); return Array.isArray(response.data) ? response.data : []; } async updateUser(login: string, data: TokenUpdate): Promise { await apiClient.put(`${this.authBasePath}/users/${login}`, data); } async updateUserPermissions( login: string, data: TokenUpdatePermissions, ): Promise { await apiClient.put( `${this.authBasePath}/users/${login}/permissions`, data, ); } async resetUserPassword( login: string, data: TokenPasswordReset, ): Promise { await apiClient.put(`${this.authBasePath}/users/${login}/password`, data); } async activateUser(login: string): Promise { await apiClient.post(`${this.authBasePath}/users/${login}/activate`); } async deactivateUser(login: string): Promise { await apiClient.post(`${this.authBasePath}/users/${login}/deactivate`); } async createRegistrationToken( data: RegistrationRequest, ): Promise> { const response = await apiClient.post>( `${this.basePath}/register-token`, data, ); return response.data; } async deployAgents(data: DeployAgentsRequest): Promise { const response = await apiClient.post( `${this.basePath}/deploy`, data, ); return response.data; } async getSystemMetrics(): Promise { const response = await apiClient.get( `${this.basePath}/system-metrics`, ); return Array.isArray(response.data) ? response.data : []; } async getGraph(): Promise { const response = await apiClient.get("/graph"); return response.data; } } export const agentApiService = new AgentApiService();