112 lines
2.9 KiB
TypeScript
112 lines
2.9 KiB
TypeScript
import { fetchWithAuth, postWithAuth } from '../fetchWithAuth';
|
|
|
|
// Tipos para la respuesta y registros
|
|
export interface Task {
|
|
task_id: string;
|
|
timestamp: string;
|
|
message: string;
|
|
status: string;
|
|
pedimento: string;
|
|
organizacion: string;
|
|
servicio: number;
|
|
}
|
|
|
|
export interface TasksResponse {
|
|
count: number;
|
|
next: string | null;
|
|
previous: string | null;
|
|
results: Task[];
|
|
}
|
|
|
|
// API para tasks/tasks/
|
|
export async function fetchTasks(
|
|
page: number = 1,
|
|
pageSize: number = 20,
|
|
filters: Record<string, any> = {}
|
|
): Promise<TasksResponse> {
|
|
try {
|
|
const API_URL = (import.meta as any).env.VITE_EFC_API_URL;
|
|
|
|
// Construir query params
|
|
const params = new URLSearchParams();
|
|
params.append('page', String(page));
|
|
params.append('page_size', String(pageSize));
|
|
|
|
// Agregar filtros
|
|
Object.entries(filters).forEach(([key, value]) => {
|
|
if (value !== undefined && value !== null && value !== '') {
|
|
params.append(key, String(value));
|
|
}
|
|
});
|
|
|
|
const res = await fetchWithAuth(`${API_URL}/tasks/tasks/?${params.toString()}`);
|
|
|
|
if (!res.ok) {
|
|
throw new Error('Error al obtener tareas');
|
|
}
|
|
|
|
return await res.json();
|
|
} catch (error) {
|
|
console.error('Error in fetchTasks:', error);
|
|
throw error;
|
|
}
|
|
}
|
|
|
|
// Interfaz para la respuesta del comando
|
|
export interface ComandoResponse {
|
|
message?: string;
|
|
error?: string;
|
|
}
|
|
|
|
// Interfaz para los parámetros de ejecución
|
|
export interface EjecutarComandoParams {
|
|
procesamiento?: string;
|
|
todos?: boolean;
|
|
}
|
|
|
|
// API para ejecutar comando de procesamiento
|
|
export async function ejecutarComando(
|
|
params: EjecutarComandoParams
|
|
): Promise<ComandoResponse> {
|
|
try {
|
|
const API_URL = (import.meta as any).env.VITE_EFC_API_URL;
|
|
|
|
console.log('API_URL:', API_URL);
|
|
// Preparar los datos para la petición POST
|
|
const requestData: any = {};
|
|
|
|
if (params.procesamiento !== undefined) {
|
|
requestData.procesamiento = params.procesamiento;
|
|
}
|
|
|
|
if (params.todos !== undefined) {
|
|
requestData.todos = params.todos;
|
|
}
|
|
|
|
// const res = await fetchWithAuth(`${API_URL}/customs/procesamientopedimentos-ejecutar-comando/`, {
|
|
// method: 'POST',
|
|
// headers: {
|
|
// 'Content-Type': 'application/json',
|
|
// },
|
|
// body: JSON.stringify(requestData),
|
|
// });
|
|
|
|
const res = await postWithAuth(`${API_URL}/customs/procesamientopedimentos-ejecutar-comando/`, requestData);
|
|
|
|
if (!res.ok) {
|
|
// Intentar obtener el mensaje de error del servidor
|
|
try {
|
|
const errorData = await res.json();
|
|
throw new Error(errorData.message || errorData.error || `Error ${res.status}: ${res.statusText}`);
|
|
} catch {
|
|
throw new Error(`Error ${res.status}: ${res.statusText}`);
|
|
}
|
|
}
|
|
|
|
return await res.json();
|
|
} catch (error) {
|
|
// console.error('Error in ejecutarComando:', error);
|
|
throw error;
|
|
}
|
|
}
|