Feat: Cambios Varios

This commit is contained in:
2025-12-23 15:12:57 -03:00
parent 32663e6324
commit 8bc1308bc5
58 changed files with 4080 additions and 663 deletions

View File

@@ -1,9 +1,10 @@
import axios from 'axios';
const api = axios.create({
baseURL: import.meta.env.VITE_API_URL, // Usa la variable de entorno
baseURL: import.meta.env.VITE_API_URL,
});
// 1. Interceptor de Solicitud (Request): Pega el token antes de salir
api.interceptors.request.use((config) => {
const token = localStorage.getItem('token');
if (token) {
@@ -12,4 +13,30 @@ api.interceptors.request.use((config) => {
return config;
});
// 2. Interceptor de Respuesta (Response): Atrapa errores al volver
api.interceptors.response.use(
(response) => {
// Si la respuesta es exitosa (200-299), la dejamos pasar
return response;
},
(error) => {
// Si hay un error de respuesta
if (error.response && error.response.status === 401) {
console.warn("Sesión expirada o no autorizada. Redirigiendo al login...");
// Borramos el token vencido/inválido
localStorage.removeItem('token');
// Forzamos la redirección al Login
// Usamos window.location.href para asegurar una recarga limpia fuera del Router de React si es necesario
if (window.location.pathname !== '/login') {
window.location.href = '/login';
}
}
// Rechazamos la promesa para que el componente sepa que hubo error (si necesita mostrar alerta)
return Promise.reject(error);
}
);
export default api;

View File

@@ -0,0 +1,12 @@
import api from './api';
export const clientService = {
getAll: async () => {
const res = await api.get('/clients');
return res.data;
},
getHistory: async (id: number) => {
const res = await api.get(`/clients/${id}/history`);
return res.data;
}
};

View File

@@ -0,0 +1,19 @@
import api from './api';
export interface DashboardData {
revenueToday: number;
adsToday: number;
ticketAverage: number;
paperOccupation: number;
weeklyTrend: { day: string; amount: number }[];
channelMix: { name: string; value: number }[];
}
export const dashboardService = {
getStats: async (from?: string, to?: string): Promise<DashboardData> => {
const response = await api.get<DashboardData>('/reports/dashboard', {
params: { from, to }
});
return response.data;
}
};

View File

@@ -0,0 +1,12 @@
import api from './api';
export const listingService = {
getPendingCount: async (): Promise<number> => {
const response = await api.get<number>('/listings/pending/count');
return response.data;
},
getById: async (id: number) => {
const res = await api.get(`/listings/${id}`);
return res.data;
},
};

View File

@@ -0,0 +1,32 @@
import api from './api';
export interface CategorySales {
categoryId: number;
categoryName: string;
totalSales: number;
adCount: number;
percentage: number;
}
export const reportService = {
getSalesByCategory: async (from?: string, to?: string): Promise<CategorySales[]> => {
const response = await api.get<CategorySales[]>('/reports/sales-by-category', {
params: { from, to }
});
return response.data;
},
exportCierre: async (from: string, to: string, userId?: number) => {
const response = await api.get(`/reports/export-cierre`, {
params: { from, to, userId },
responseType: 'blob'
});
const url = window.URL.createObjectURL(new Blob([response.data], { type: 'application/pdf' }));
const link = document.createElement('a');
link.href = url;
link.setAttribute('download', userId ? `Caja_Cajero_${userId}.pdf` : `Cierre_Global.pdf`);
document.body.appendChild(link);
link.click();
link.remove();
}
};