Files
frontend/src/api/notificaciones.ts
2026-05-28 07:15:26 -06:00

65 lines
2.1 KiB
TypeScript

import { fetchWithAuth, putWithAuth } from '../fetchWithAuth';
// PUT para marcar una notificación como vista
export async function marcarNotificacionComoVista(id: number): Promise<Notificacion> {
const url = `${API_URL}/notificaciones/notificaciones/${id}/`;
const res = await putWithAuth(url, { visto: true });
if (!res.ok) throw new Error('Error al actualizar notificación');
return await res.json();
}
// src/api/notificaciones.ts
export interface TipoNotificacion {
id: number;
tipo: string;
descripcion: string;
}
export interface NotificacionDatos {
task_id: string;
label: string;
resultado: Record<string, unknown>;
}
export interface Notificacion {
id: number;
tipo: TipoNotificacion;
dirigido: string;
mensaje: string;
datos: NotificacionDatos | null;
fecha_envio: string;
created_at: string;
visto: boolean;
}
export interface NotificacionesResponse {
count: number;
next: string | null;
previous: string | null;
results: Notificacion[];
}
const API_URL = import.meta.env.VITE_EFC_API_URL;
export async function fetchNotificaciones({ page = 1, pageSize = 10, visto = false } = {}): Promise<NotificacionesResponse> {
const url = `${API_URL}/notificaciones/notificaciones/?page=${page}&page_size=${pageSize}&visto=${visto}`;
const res = await fetchWithAuth(url);
if (!res.ok) throw new Error('Error al obtener notificaciones');
return await res.json();
}
export async function fetchAllNotifications({page = 1, page_size=10}): Promise<NotificacionesResponse>{
const url = `${API_URL}/notificaciones/notificaciones/?page=${page}&page_size=${page_size}`;
const res = await fetchWithAuth(url);
if (!res.ok) throw new Error('Error al obtener notificaciones');
return await res.json();
}
export async function fetchNotificacionByTaskId(taskId: string): Promise<Notificacion | null> {
const url = `${API_URL}/notificaciones/notificaciones/by-task/${taskId}/`;
const res = await fetchWithAuth(url);
if (res.status === 404) return null;
if (!res.ok) throw new Error('Error al obtener notificación por task_id');
return await res.json();
}