feat(sat): add frontend components for SAT configuration (Phase 8)
- Add FielUploadModal component for FIEL credential upload - Add SyncStatus component showing current sync progress - Add SyncHistory component with pagination and retry - Add SAT configuration page at /configuracion/sat - Add API client functions for FIEL and SAT endpoints Features: - File upload with Base64 encoding - Real-time sync progress tracking - Manual sync trigger (initial/daily) - Sync history with retry capability - FIEL status display with expiration warning Co-Authored-By: Claude Opus 4.5 <noreply@anthropic.com>
This commit is contained in:
156
apps/web/app/(dashboard)/configuracion/sat/page.tsx
Normal file
156
apps/web/app/(dashboard)/configuracion/sat/page.tsx
Normal file
@@ -0,0 +1,156 @@
|
|||||||
|
'use client';
|
||||||
|
|
||||||
|
import { useEffect, useState } from 'react';
|
||||||
|
import { Card, CardContent, CardDescription, CardHeader, CardTitle } from '@/components/ui/card';
|
||||||
|
import { Button } from '@/components/ui/button';
|
||||||
|
import { FielUploadModal } from '@/components/sat/FielUploadModal';
|
||||||
|
import { SyncStatus } from '@/components/sat/SyncStatus';
|
||||||
|
import { SyncHistory } from '@/components/sat/SyncHistory';
|
||||||
|
import { getFielStatus, deleteFiel } from '@/lib/api/fiel';
|
||||||
|
import type { FielStatus } from '@horux/shared';
|
||||||
|
|
||||||
|
export default function SatConfigPage() {
|
||||||
|
const [fielStatus, setFielStatus] = useState<FielStatus | null>(null);
|
||||||
|
const [loading, setLoading] = useState(true);
|
||||||
|
const [showUploadModal, setShowUploadModal] = useState(false);
|
||||||
|
const [deleting, setDeleting] = useState(false);
|
||||||
|
|
||||||
|
const fetchFielStatus = async () => {
|
||||||
|
try {
|
||||||
|
const status = await getFielStatus();
|
||||||
|
setFielStatus(status);
|
||||||
|
} catch (err) {
|
||||||
|
console.error('Error fetching FIEL status:', err);
|
||||||
|
} finally {
|
||||||
|
setLoading(false);
|
||||||
|
}
|
||||||
|
};
|
||||||
|
|
||||||
|
useEffect(() => {
|
||||||
|
fetchFielStatus();
|
||||||
|
}, []);
|
||||||
|
|
||||||
|
const handleUploadSuccess = (status: FielStatus) => {
|
||||||
|
setFielStatus(status);
|
||||||
|
setShowUploadModal(false);
|
||||||
|
};
|
||||||
|
|
||||||
|
const handleDelete = async () => {
|
||||||
|
if (!confirm('Estas seguro de eliminar la FIEL? Se detendran las sincronizaciones automaticas.')) {
|
||||||
|
return;
|
||||||
|
}
|
||||||
|
|
||||||
|
setDeleting(true);
|
||||||
|
try {
|
||||||
|
await deleteFiel();
|
||||||
|
setFielStatus({ configured: false });
|
||||||
|
} catch (err) {
|
||||||
|
console.error('Error deleting FIEL:', err);
|
||||||
|
} finally {
|
||||||
|
setDeleting(false);
|
||||||
|
}
|
||||||
|
};
|
||||||
|
|
||||||
|
if (loading) {
|
||||||
|
return (
|
||||||
|
<div className="p-6">
|
||||||
|
<h1 className="text-2xl font-bold mb-6">Configuracion SAT</h1>
|
||||||
|
<p>Cargando...</p>
|
||||||
|
</div>
|
||||||
|
);
|
||||||
|
}
|
||||||
|
|
||||||
|
return (
|
||||||
|
<div className="p-6 space-y-6">
|
||||||
|
<div className="flex justify-between items-center">
|
||||||
|
<div>
|
||||||
|
<h1 className="text-2xl font-bold">Configuracion SAT</h1>
|
||||||
|
<p className="text-muted-foreground">
|
||||||
|
Gestiona tu FIEL y la sincronizacion automatica de CFDIs
|
||||||
|
</p>
|
||||||
|
</div>
|
||||||
|
</div>
|
||||||
|
|
||||||
|
{/* Estado de la FIEL */}
|
||||||
|
<Card>
|
||||||
|
<CardHeader>
|
||||||
|
<CardTitle>FIEL (e.firma)</CardTitle>
|
||||||
|
<CardDescription>
|
||||||
|
Tu firma electronica para autenticarte con el SAT
|
||||||
|
</CardDescription>
|
||||||
|
</CardHeader>
|
||||||
|
<CardContent>
|
||||||
|
{fielStatus?.configured ? (
|
||||||
|
<div className="space-y-4">
|
||||||
|
<div className="grid grid-cols-2 md:grid-cols-4 gap-4">
|
||||||
|
<div>
|
||||||
|
<p className="text-sm text-muted-foreground">RFC</p>
|
||||||
|
<p className="font-medium">{fielStatus.rfc}</p>
|
||||||
|
</div>
|
||||||
|
<div>
|
||||||
|
<p className="text-sm text-muted-foreground">No. Serie</p>
|
||||||
|
<p className="font-medium text-xs">{fielStatus.serialNumber}</p>
|
||||||
|
</div>
|
||||||
|
<div>
|
||||||
|
<p className="text-sm text-muted-foreground">Vigente hasta</p>
|
||||||
|
<p className="font-medium">
|
||||||
|
{fielStatus.validUntil ? new Date(fielStatus.validUntil).toLocaleDateString('es-MX') : '-'}
|
||||||
|
</p>
|
||||||
|
</div>
|
||||||
|
<div>
|
||||||
|
<p className="text-sm text-muted-foreground">Estado</p>
|
||||||
|
<p className={`font-medium ${fielStatus.isExpired ? 'text-red-500' : 'text-green-500'}`}>
|
||||||
|
{fielStatus.isExpired ? 'Vencida' : `Valida (${fielStatus.daysUntilExpiration} dias)`}
|
||||||
|
</p>
|
||||||
|
</div>
|
||||||
|
</div>
|
||||||
|
|
||||||
|
<div className="flex gap-3">
|
||||||
|
<Button
|
||||||
|
variant="outline"
|
||||||
|
onClick={() => setShowUploadModal(true)}
|
||||||
|
>
|
||||||
|
Actualizar FIEL
|
||||||
|
</Button>
|
||||||
|
<Button
|
||||||
|
variant="destructive"
|
||||||
|
onClick={handleDelete}
|
||||||
|
disabled={deleting}
|
||||||
|
>
|
||||||
|
{deleting ? 'Eliminando...' : 'Eliminar FIEL'}
|
||||||
|
</Button>
|
||||||
|
</div>
|
||||||
|
</div>
|
||||||
|
) : (
|
||||||
|
<div className="space-y-4">
|
||||||
|
<p className="text-muted-foreground">
|
||||||
|
No tienes una FIEL configurada. Sube tu certificado y llave privada para habilitar
|
||||||
|
la sincronizacion automatica de CFDIs con el SAT.
|
||||||
|
</p>
|
||||||
|
<Button onClick={() => setShowUploadModal(true)}>
|
||||||
|
Configurar FIEL
|
||||||
|
</Button>
|
||||||
|
</div>
|
||||||
|
)}
|
||||||
|
</CardContent>
|
||||||
|
</Card>
|
||||||
|
|
||||||
|
{/* Estado de Sincronizacion */}
|
||||||
|
<SyncStatus
|
||||||
|
fielConfigured={fielStatus?.configured || false}
|
||||||
|
onSyncStarted={fetchFielStatus}
|
||||||
|
/>
|
||||||
|
|
||||||
|
{/* Historial */}
|
||||||
|
<SyncHistory fielConfigured={fielStatus?.configured || false} />
|
||||||
|
|
||||||
|
{/* Modal de carga */}
|
||||||
|
{showUploadModal && (
|
||||||
|
<FielUploadModal
|
||||||
|
onSuccess={handleUploadSuccess}
|
||||||
|
onClose={() => setShowUploadModal(false)}
|
||||||
|
/>
|
||||||
|
)}
|
||||||
|
</div>
|
||||||
|
);
|
||||||
|
}
|
||||||
138
apps/web/components/sat/FielUploadModal.tsx
Normal file
138
apps/web/components/sat/FielUploadModal.tsx
Normal file
@@ -0,0 +1,138 @@
|
|||||||
|
'use client';
|
||||||
|
|
||||||
|
import { useState, useCallback } from 'react';
|
||||||
|
import { Button } from '@/components/ui/button';
|
||||||
|
import { Input } from '@/components/ui/input';
|
||||||
|
import { Label } from '@/components/ui/label';
|
||||||
|
import { Card, CardContent, CardDescription, CardHeader, CardTitle } from '@/components/ui/card';
|
||||||
|
import { uploadFiel } from '@/lib/api/fiel';
|
||||||
|
import type { FielStatus } from '@horux/shared';
|
||||||
|
|
||||||
|
interface FielUploadModalProps {
|
||||||
|
onSuccess: (status: FielStatus) => void;
|
||||||
|
onClose: () => void;
|
||||||
|
}
|
||||||
|
|
||||||
|
export function FielUploadModal({ onSuccess, onClose }: FielUploadModalProps) {
|
||||||
|
const [cerFile, setCerFile] = useState<File | null>(null);
|
||||||
|
const [keyFile, setKeyFile] = useState<File | null>(null);
|
||||||
|
const [password, setPassword] = useState('');
|
||||||
|
const [error, setError] = useState('');
|
||||||
|
const [loading, setLoading] = useState(false);
|
||||||
|
|
||||||
|
const fileToBase64 = (file: File): Promise<string> => {
|
||||||
|
return new Promise((resolve, reject) => {
|
||||||
|
const reader = new FileReader();
|
||||||
|
reader.readAsDataURL(file);
|
||||||
|
reader.onload = () => {
|
||||||
|
const result = reader.result as string;
|
||||||
|
// Remove data URL prefix (e.g., "data:application/x-x509-ca-cert;base64,")
|
||||||
|
const base64 = result.split(',')[1];
|
||||||
|
resolve(base64);
|
||||||
|
};
|
||||||
|
reader.onerror = reject;
|
||||||
|
});
|
||||||
|
};
|
||||||
|
|
||||||
|
const handleSubmit = useCallback(async (e: React.FormEvent) => {
|
||||||
|
e.preventDefault();
|
||||||
|
setError('');
|
||||||
|
|
||||||
|
if (!cerFile || !keyFile || !password) {
|
||||||
|
setError('Todos los campos son requeridos');
|
||||||
|
return;
|
||||||
|
}
|
||||||
|
|
||||||
|
setLoading(true);
|
||||||
|
|
||||||
|
try {
|
||||||
|
const cerBase64 = await fileToBase64(cerFile);
|
||||||
|
const keyBase64 = await fileToBase64(keyFile);
|
||||||
|
|
||||||
|
const result = await uploadFiel({
|
||||||
|
cerFile: cerBase64,
|
||||||
|
keyFile: keyBase64,
|
||||||
|
password,
|
||||||
|
});
|
||||||
|
|
||||||
|
if (result.status) {
|
||||||
|
onSuccess(result.status);
|
||||||
|
}
|
||||||
|
} catch (err: any) {
|
||||||
|
setError(err.response?.data?.error || 'Error al subir la FIEL');
|
||||||
|
} finally {
|
||||||
|
setLoading(false);
|
||||||
|
}
|
||||||
|
}, [cerFile, keyFile, password, onSuccess]);
|
||||||
|
|
||||||
|
return (
|
||||||
|
<div className="fixed inset-0 bg-black/50 flex items-center justify-center z-50">
|
||||||
|
<Card className="w-full max-w-md mx-4">
|
||||||
|
<CardHeader>
|
||||||
|
<CardTitle>Configurar FIEL (e.firma)</CardTitle>
|
||||||
|
<CardDescription>
|
||||||
|
Sube tu certificado y llave privada para sincronizar CFDIs con el SAT
|
||||||
|
</CardDescription>
|
||||||
|
</CardHeader>
|
||||||
|
<CardContent>
|
||||||
|
<form onSubmit={handleSubmit} className="space-y-4">
|
||||||
|
<div className="space-y-2">
|
||||||
|
<Label htmlFor="cer">Certificado (.cer)</Label>
|
||||||
|
<Input
|
||||||
|
id="cer"
|
||||||
|
type="file"
|
||||||
|
accept=".cer"
|
||||||
|
onChange={(e) => setCerFile(e.target.files?.[0] || null)}
|
||||||
|
className="cursor-pointer"
|
||||||
|
/>
|
||||||
|
</div>
|
||||||
|
|
||||||
|
<div className="space-y-2">
|
||||||
|
<Label htmlFor="key">Llave Privada (.key)</Label>
|
||||||
|
<Input
|
||||||
|
id="key"
|
||||||
|
type="file"
|
||||||
|
accept=".key"
|
||||||
|
onChange={(e) => setKeyFile(e.target.files?.[0] || null)}
|
||||||
|
className="cursor-pointer"
|
||||||
|
/>
|
||||||
|
</div>
|
||||||
|
|
||||||
|
<div className="space-y-2">
|
||||||
|
<Label htmlFor="password">Contrasena de la llave</Label>
|
||||||
|
<Input
|
||||||
|
id="password"
|
||||||
|
type="password"
|
||||||
|
value={password}
|
||||||
|
onChange={(e) => setPassword(e.target.value)}
|
||||||
|
placeholder="Ingresa la contrasena de tu FIEL"
|
||||||
|
/>
|
||||||
|
</div>
|
||||||
|
|
||||||
|
{error && (
|
||||||
|
<p className="text-sm text-red-500">{error}</p>
|
||||||
|
)}
|
||||||
|
|
||||||
|
<div className="flex gap-3 pt-4">
|
||||||
|
<Button
|
||||||
|
type="button"
|
||||||
|
variant="outline"
|
||||||
|
onClick={onClose}
|
||||||
|
className="flex-1"
|
||||||
|
>
|
||||||
|
Cancelar
|
||||||
|
</Button>
|
||||||
|
<Button
|
||||||
|
type="submit"
|
||||||
|
disabled={loading}
|
||||||
|
className="flex-1"
|
||||||
|
>
|
||||||
|
{loading ? 'Subiendo...' : 'Configurar FIEL'}
|
||||||
|
</Button>
|
||||||
|
</div>
|
||||||
|
</form>
|
||||||
|
</CardContent>
|
||||||
|
</Card>
|
||||||
|
</div>
|
||||||
|
);
|
||||||
|
}
|
||||||
182
apps/web/components/sat/SyncHistory.tsx
Normal file
182
apps/web/components/sat/SyncHistory.tsx
Normal file
@@ -0,0 +1,182 @@
|
|||||||
|
'use client';
|
||||||
|
|
||||||
|
import { useEffect, useState } from 'react';
|
||||||
|
import { Card, CardContent, CardDescription, CardHeader, CardTitle } from '@/components/ui/card';
|
||||||
|
import { Button } from '@/components/ui/button';
|
||||||
|
import { getSyncHistory, retrySync } from '@/lib/api/sat';
|
||||||
|
import type { SatSyncJob } from '@horux/shared';
|
||||||
|
|
||||||
|
interface SyncHistoryProps {
|
||||||
|
fielConfigured: boolean;
|
||||||
|
}
|
||||||
|
|
||||||
|
const statusLabels: Record<string, string> = {
|
||||||
|
pending: 'Pendiente',
|
||||||
|
running: 'En progreso',
|
||||||
|
completed: 'Completado',
|
||||||
|
failed: 'Fallido',
|
||||||
|
};
|
||||||
|
|
||||||
|
const statusColors: Record<string, string> = {
|
||||||
|
pending: 'bg-yellow-100 text-yellow-800',
|
||||||
|
running: 'bg-blue-100 text-blue-800',
|
||||||
|
completed: 'bg-green-100 text-green-800',
|
||||||
|
failed: 'bg-red-100 text-red-800',
|
||||||
|
};
|
||||||
|
|
||||||
|
const typeLabels: Record<string, string> = {
|
||||||
|
initial: 'Inicial',
|
||||||
|
daily: 'Diaria',
|
||||||
|
};
|
||||||
|
|
||||||
|
export function SyncHistory({ fielConfigured }: SyncHistoryProps) {
|
||||||
|
const [jobs, setJobs] = useState<SatSyncJob[]>([]);
|
||||||
|
const [loading, setLoading] = useState(true);
|
||||||
|
const [page, setPage] = useState(1);
|
||||||
|
const [total, setTotal] = useState(0);
|
||||||
|
const limit = 10;
|
||||||
|
|
||||||
|
const fetchHistory = async () => {
|
||||||
|
try {
|
||||||
|
const data = await getSyncHistory(page, limit);
|
||||||
|
setJobs(data.jobs);
|
||||||
|
setTotal(data.total);
|
||||||
|
} catch (err) {
|
||||||
|
console.error('Error fetching sync history:', err);
|
||||||
|
} finally {
|
||||||
|
setLoading(false);
|
||||||
|
}
|
||||||
|
};
|
||||||
|
|
||||||
|
useEffect(() => {
|
||||||
|
if (fielConfigured) {
|
||||||
|
fetchHistory();
|
||||||
|
} else {
|
||||||
|
setLoading(false);
|
||||||
|
}
|
||||||
|
}, [fielConfigured, page]);
|
||||||
|
|
||||||
|
const handleRetry = async (jobId: string) => {
|
||||||
|
try {
|
||||||
|
await retrySync(jobId);
|
||||||
|
fetchHistory();
|
||||||
|
} catch (err) {
|
||||||
|
console.error('Error retrying job:', err);
|
||||||
|
}
|
||||||
|
};
|
||||||
|
|
||||||
|
if (!fielConfigured) {
|
||||||
|
return null;
|
||||||
|
}
|
||||||
|
|
||||||
|
if (loading) {
|
||||||
|
return (
|
||||||
|
<Card>
|
||||||
|
<CardHeader>
|
||||||
|
<CardTitle>Historial de Sincronizaciones</CardTitle>
|
||||||
|
</CardHeader>
|
||||||
|
<CardContent>
|
||||||
|
<p className="text-muted-foreground">Cargando historial...</p>
|
||||||
|
</CardContent>
|
||||||
|
</Card>
|
||||||
|
);
|
||||||
|
}
|
||||||
|
|
||||||
|
if (jobs.length === 0) {
|
||||||
|
return (
|
||||||
|
<Card>
|
||||||
|
<CardHeader>
|
||||||
|
<CardTitle>Historial de Sincronizaciones</CardTitle>
|
||||||
|
<CardDescription>
|
||||||
|
Registro de todas las sincronizaciones con el SAT
|
||||||
|
</CardDescription>
|
||||||
|
</CardHeader>
|
||||||
|
<CardContent>
|
||||||
|
<p className="text-muted-foreground">No hay sincronizaciones registradas.</p>
|
||||||
|
</CardContent>
|
||||||
|
</Card>
|
||||||
|
);
|
||||||
|
}
|
||||||
|
|
||||||
|
const totalPages = Math.ceil(total / limit);
|
||||||
|
|
||||||
|
return (
|
||||||
|
<Card>
|
||||||
|
<CardHeader>
|
||||||
|
<CardTitle>Historial de Sincronizaciones</CardTitle>
|
||||||
|
<CardDescription>
|
||||||
|
Registro de todas las sincronizaciones con el SAT
|
||||||
|
</CardDescription>
|
||||||
|
</CardHeader>
|
||||||
|
<CardContent>
|
||||||
|
<div className="space-y-3">
|
||||||
|
{jobs.map((job) => (
|
||||||
|
<div
|
||||||
|
key={job.id}
|
||||||
|
className="flex items-center justify-between p-4 border rounded-lg"
|
||||||
|
>
|
||||||
|
<div className="flex-1">
|
||||||
|
<div className="flex items-center gap-2 mb-1">
|
||||||
|
<span className={`px-2 py-0.5 rounded text-xs ${statusColors[job.status]}`}>
|
||||||
|
{statusLabels[job.status]}
|
||||||
|
</span>
|
||||||
|
<span className="text-xs text-muted-foreground">
|
||||||
|
{typeLabels[job.type]}
|
||||||
|
</span>
|
||||||
|
</div>
|
||||||
|
<p className="text-sm">
|
||||||
|
{job.startedAt ? new Date(job.startedAt).toLocaleString('es-MX') : 'No iniciado'}
|
||||||
|
</p>
|
||||||
|
<p className="text-xs text-muted-foreground">
|
||||||
|
{job.cfdisInserted} nuevos, {job.cfdisUpdated} actualizados
|
||||||
|
</p>
|
||||||
|
{job.errorMessage && (
|
||||||
|
<p className="text-xs text-red-500 mt-1">{job.errorMessage}</p>
|
||||||
|
)}
|
||||||
|
</div>
|
||||||
|
{job.status === 'failed' && (
|
||||||
|
<Button
|
||||||
|
size="sm"
|
||||||
|
variant="outline"
|
||||||
|
onClick={() => handleRetry(job.id)}
|
||||||
|
>
|
||||||
|
Reintentar
|
||||||
|
</Button>
|
||||||
|
)}
|
||||||
|
{job.status === 'running' && (
|
||||||
|
<div className="text-right">
|
||||||
|
<p className="text-sm font-medium">{job.progressPercent}%</p>
|
||||||
|
<p className="text-xs text-muted-foreground">{job.cfdisDownloaded} descargados</p>
|
||||||
|
</div>
|
||||||
|
)}
|
||||||
|
</div>
|
||||||
|
))}
|
||||||
|
</div>
|
||||||
|
|
||||||
|
{totalPages > 1 && (
|
||||||
|
<div className="flex justify-center gap-2 mt-4">
|
||||||
|
<Button
|
||||||
|
size="sm"
|
||||||
|
variant="outline"
|
||||||
|
disabled={page === 1}
|
||||||
|
onClick={() => setPage(p => p - 1)}
|
||||||
|
>
|
||||||
|
Anterior
|
||||||
|
</Button>
|
||||||
|
<span className="py-2 px-3 text-sm">
|
||||||
|
Pagina {page} de {totalPages}
|
||||||
|
</span>
|
||||||
|
<Button
|
||||||
|
size="sm"
|
||||||
|
variant="outline"
|
||||||
|
disabled={page === totalPages}
|
||||||
|
onClick={() => setPage(p => p + 1)}
|
||||||
|
>
|
||||||
|
Siguiente
|
||||||
|
</Button>
|
||||||
|
</div>
|
||||||
|
)}
|
||||||
|
</CardContent>
|
||||||
|
</Card>
|
||||||
|
);
|
||||||
|
}
|
||||||
187
apps/web/components/sat/SyncStatus.tsx
Normal file
187
apps/web/components/sat/SyncStatus.tsx
Normal file
@@ -0,0 +1,187 @@
|
|||||||
|
'use client';
|
||||||
|
|
||||||
|
import { useEffect, useState } from 'react';
|
||||||
|
import { Card, CardContent, CardDescription, CardHeader, CardTitle } from '@/components/ui/card';
|
||||||
|
import { Button } from '@/components/ui/button';
|
||||||
|
import { getSyncStatus, startSync } from '@/lib/api/sat';
|
||||||
|
import type { SatSyncStatusResponse } from '@horux/shared';
|
||||||
|
|
||||||
|
interface SyncStatusProps {
|
||||||
|
fielConfigured: boolean;
|
||||||
|
onSyncStarted?: () => void;
|
||||||
|
}
|
||||||
|
|
||||||
|
const statusLabels: Record<string, string> = {
|
||||||
|
pending: 'Pendiente',
|
||||||
|
running: 'En progreso',
|
||||||
|
completed: 'Completado',
|
||||||
|
failed: 'Fallido',
|
||||||
|
};
|
||||||
|
|
||||||
|
const statusColors: Record<string, string> = {
|
||||||
|
pending: 'bg-yellow-100 text-yellow-800',
|
||||||
|
running: 'bg-blue-100 text-blue-800',
|
||||||
|
completed: 'bg-green-100 text-green-800',
|
||||||
|
failed: 'bg-red-100 text-red-800',
|
||||||
|
};
|
||||||
|
|
||||||
|
export function SyncStatus({ fielConfigured, onSyncStarted }: SyncStatusProps) {
|
||||||
|
const [status, setStatus] = useState<SatSyncStatusResponse | null>(null);
|
||||||
|
const [loading, setLoading] = useState(true);
|
||||||
|
const [startingSync, setStartingSync] = useState(false);
|
||||||
|
const [error, setError] = useState('');
|
||||||
|
|
||||||
|
const fetchStatus = async () => {
|
||||||
|
try {
|
||||||
|
const data = await getSyncStatus();
|
||||||
|
setStatus(data);
|
||||||
|
} catch (err) {
|
||||||
|
console.error('Error fetching sync status:', err);
|
||||||
|
} finally {
|
||||||
|
setLoading(false);
|
||||||
|
}
|
||||||
|
};
|
||||||
|
|
||||||
|
useEffect(() => {
|
||||||
|
if (fielConfigured) {
|
||||||
|
fetchStatus();
|
||||||
|
// Actualizar cada 30 segundos si hay sync activo
|
||||||
|
const interval = setInterval(fetchStatus, 30000);
|
||||||
|
return () => clearInterval(interval);
|
||||||
|
} else {
|
||||||
|
setLoading(false);
|
||||||
|
}
|
||||||
|
}, [fielConfigured]);
|
||||||
|
|
||||||
|
const handleStartSync = async (type: 'initial' | 'daily') => {
|
||||||
|
setStartingSync(true);
|
||||||
|
setError('');
|
||||||
|
try {
|
||||||
|
await startSync({ type });
|
||||||
|
await fetchStatus();
|
||||||
|
onSyncStarted?.();
|
||||||
|
} catch (err: any) {
|
||||||
|
setError(err.response?.data?.error || 'Error al iniciar sincronizacion');
|
||||||
|
} finally {
|
||||||
|
setStartingSync(false);
|
||||||
|
}
|
||||||
|
};
|
||||||
|
|
||||||
|
if (!fielConfigured) {
|
||||||
|
return (
|
||||||
|
<Card>
|
||||||
|
<CardHeader>
|
||||||
|
<CardTitle>Sincronizacion SAT</CardTitle>
|
||||||
|
<CardDescription>
|
||||||
|
Configura tu FIEL para habilitar la sincronizacion automatica
|
||||||
|
</CardDescription>
|
||||||
|
</CardHeader>
|
||||||
|
<CardContent>
|
||||||
|
<p className="text-muted-foreground">
|
||||||
|
La sincronizacion con el SAT requiere una FIEL valida configurada.
|
||||||
|
</p>
|
||||||
|
</CardContent>
|
||||||
|
</Card>
|
||||||
|
);
|
||||||
|
}
|
||||||
|
|
||||||
|
if (loading) {
|
||||||
|
return (
|
||||||
|
<Card>
|
||||||
|
<CardHeader>
|
||||||
|
<CardTitle>Sincronizacion SAT</CardTitle>
|
||||||
|
</CardHeader>
|
||||||
|
<CardContent>
|
||||||
|
<p className="text-muted-foreground">Cargando estado...</p>
|
||||||
|
</CardContent>
|
||||||
|
</Card>
|
||||||
|
);
|
||||||
|
}
|
||||||
|
|
||||||
|
return (
|
||||||
|
<Card>
|
||||||
|
<CardHeader>
|
||||||
|
<CardTitle>Sincronizacion SAT</CardTitle>
|
||||||
|
<CardDescription>
|
||||||
|
Estado de la sincronizacion automatica de CFDIs
|
||||||
|
</CardDescription>
|
||||||
|
</CardHeader>
|
||||||
|
<CardContent className="space-y-4">
|
||||||
|
{status?.hasActiveSync && status.currentJob && (
|
||||||
|
<div className="p-4 bg-blue-50 rounded-lg">
|
||||||
|
<div className="flex items-center gap-2 mb-2">
|
||||||
|
<span className={`px-2 py-1 rounded text-sm ${statusColors[status.currentJob.status]}`}>
|
||||||
|
{statusLabels[status.currentJob.status]}
|
||||||
|
</span>
|
||||||
|
<span className="text-sm text-muted-foreground">
|
||||||
|
{status.currentJob.type === 'initial' ? 'Sincronizacion inicial' : 'Sincronizacion diaria'}
|
||||||
|
</span>
|
||||||
|
</div>
|
||||||
|
{status.currentJob.status === 'running' && (
|
||||||
|
<div className="w-full bg-gray-200 rounded-full h-2 mt-2">
|
||||||
|
<div
|
||||||
|
className="bg-blue-600 h-2 rounded-full transition-all"
|
||||||
|
style={{ width: `${status.currentJob.progressPercent}%` }}
|
||||||
|
/>
|
||||||
|
</div>
|
||||||
|
)}
|
||||||
|
<p className="text-sm mt-2">
|
||||||
|
{status.currentJob.cfdisDownloaded} CFDIs descargados
|
||||||
|
</p>
|
||||||
|
</div>
|
||||||
|
)}
|
||||||
|
|
||||||
|
{status?.lastCompletedJob && !status.hasActiveSync && (
|
||||||
|
<div className="p-4 bg-green-50 rounded-lg">
|
||||||
|
<div className="flex items-center gap-2 mb-2">
|
||||||
|
<span className={`px-2 py-1 rounded text-sm ${statusColors.completed}`}>
|
||||||
|
Ultima sincronizacion exitosa
|
||||||
|
</span>
|
||||||
|
</div>
|
||||||
|
<p className="text-sm">
|
||||||
|
{new Date(status.lastCompletedJob.completedAt!).toLocaleString('es-MX')}
|
||||||
|
</p>
|
||||||
|
<p className="text-sm text-muted-foreground">
|
||||||
|
{status.lastCompletedJob.cfdisInserted} CFDIs nuevos, {status.lastCompletedJob.cfdisUpdated} actualizados
|
||||||
|
</p>
|
||||||
|
</div>
|
||||||
|
)}
|
||||||
|
|
||||||
|
<div className="grid grid-cols-2 gap-4 text-center">
|
||||||
|
<div className="p-4 bg-gray-50 rounded-lg">
|
||||||
|
<p className="text-2xl font-bold">{status?.totalCfdisSynced || 0}</p>
|
||||||
|
<p className="text-sm text-muted-foreground">CFDIs sincronizados</p>
|
||||||
|
</div>
|
||||||
|
<div className="p-4 bg-gray-50 rounded-lg">
|
||||||
|
<p className="text-2xl font-bold">3:00 AM</p>
|
||||||
|
<p className="text-sm text-muted-foreground">Sincronizacion diaria</p>
|
||||||
|
</div>
|
||||||
|
</div>
|
||||||
|
|
||||||
|
{error && (
|
||||||
|
<p className="text-sm text-red-500">{error}</p>
|
||||||
|
)}
|
||||||
|
|
||||||
|
<div className="flex gap-3">
|
||||||
|
<Button
|
||||||
|
variant="outline"
|
||||||
|
disabled={startingSync || status?.hasActiveSync}
|
||||||
|
onClick={() => handleStartSync('daily')}
|
||||||
|
className="flex-1"
|
||||||
|
>
|
||||||
|
{startingSync ? 'Iniciando...' : 'Sincronizar ahora'}
|
||||||
|
</Button>
|
||||||
|
{!status?.lastCompletedJob && (
|
||||||
|
<Button
|
||||||
|
disabled={startingSync || status?.hasActiveSync}
|
||||||
|
onClick={() => handleStartSync('initial')}
|
||||||
|
className="flex-1"
|
||||||
|
>
|
||||||
|
{startingSync ? 'Iniciando...' : 'Sincronizacion inicial (10 anos)'}
|
||||||
|
</Button>
|
||||||
|
)}
|
||||||
|
</div>
|
||||||
|
</CardContent>
|
||||||
|
</Card>
|
||||||
|
);
|
||||||
|
}
|
||||||
16
apps/web/lib/api/fiel.ts
Normal file
16
apps/web/lib/api/fiel.ts
Normal file
@@ -0,0 +1,16 @@
|
|||||||
|
import { apiClient } from './client';
|
||||||
|
import type { FielStatus, FielUploadRequest } from '@horux/shared';
|
||||||
|
|
||||||
|
export async function uploadFiel(data: FielUploadRequest): Promise<{ message: string; status: FielStatus }> {
|
||||||
|
const response = await apiClient.post('/fiel/upload', data);
|
||||||
|
return response.data;
|
||||||
|
}
|
||||||
|
|
||||||
|
export async function getFielStatus(): Promise<FielStatus> {
|
||||||
|
const response = await apiClient.get<FielStatus>('/fiel/status');
|
||||||
|
return response.data;
|
||||||
|
}
|
||||||
|
|
||||||
|
export async function deleteFiel(): Promise<void> {
|
||||||
|
await apiClient.delete('/fiel');
|
||||||
|
}
|
||||||
45
apps/web/lib/api/sat.ts
Normal file
45
apps/web/lib/api/sat.ts
Normal file
@@ -0,0 +1,45 @@
|
|||||||
|
import { apiClient } from './client';
|
||||||
|
import type {
|
||||||
|
SatSyncJob,
|
||||||
|
SatSyncStatusResponse,
|
||||||
|
SatSyncHistoryResponse,
|
||||||
|
StartSyncRequest,
|
||||||
|
StartSyncResponse,
|
||||||
|
} from '@horux/shared';
|
||||||
|
|
||||||
|
export async function startSync(data?: StartSyncRequest): Promise<StartSyncResponse> {
|
||||||
|
const response = await apiClient.post<StartSyncResponse>('/sat/sync', data || {});
|
||||||
|
return response.data;
|
||||||
|
}
|
||||||
|
|
||||||
|
export async function getSyncStatus(): Promise<SatSyncStatusResponse> {
|
||||||
|
const response = await apiClient.get<SatSyncStatusResponse>('/sat/sync/status');
|
||||||
|
return response.data;
|
||||||
|
}
|
||||||
|
|
||||||
|
export async function getSyncHistory(page: number = 1, limit: number = 10): Promise<SatSyncHistoryResponse> {
|
||||||
|
const response = await apiClient.get<SatSyncHistoryResponse>('/sat/sync/history', {
|
||||||
|
params: { page, limit },
|
||||||
|
});
|
||||||
|
return response.data;
|
||||||
|
}
|
||||||
|
|
||||||
|
export async function getSyncJob(id: string): Promise<SatSyncJob> {
|
||||||
|
const response = await apiClient.get<SatSyncJob>(`/sat/sync/${id}`);
|
||||||
|
return response.data;
|
||||||
|
}
|
||||||
|
|
||||||
|
export async function retrySync(id: string): Promise<StartSyncResponse> {
|
||||||
|
const response = await apiClient.post<StartSyncResponse>(`/sat/sync/${id}/retry`);
|
||||||
|
return response.data;
|
||||||
|
}
|
||||||
|
|
||||||
|
export async function getCronInfo(): Promise<{ scheduled: boolean; expression: string; timezone: string }> {
|
||||||
|
const response = await apiClient.get('/sat/cron');
|
||||||
|
return response.data;
|
||||||
|
}
|
||||||
|
|
||||||
|
export async function runCron(): Promise<{ message: string }> {
|
||||||
|
const response = await apiClient.post('/sat/cron/run');
|
||||||
|
return response.data;
|
||||||
|
}
|
||||||
Reference in New Issue
Block a user