feat: SAT sync improvements and documentation
- Add custom date range support for SAT synchronization - Fix UUID cast in SQL queries for sat_sync_job_id - Fix processInitialSync to respect custom dateFrom/dateTo parameters - Add date picker UI for custom period sync - Add comprehensive documentation for SAT sync implementation Co-Authored-By: Claude Opus 4.5 <noreply@anthropic.com>
This commit is contained in:
@@ -96,7 +96,7 @@ async function saveCfdis(
|
||||
estado = $22,
|
||||
xml_original = $23,
|
||||
last_sat_sync = NOW(),
|
||||
sat_sync_job_id = $24,
|
||||
sat_sync_job_id = $24::uuid,
|
||||
updated_at = NOW()
|
||||
WHERE uuid_fiscal = $1`,
|
||||
cfdi.uuidFiscal,
|
||||
@@ -137,7 +137,7 @@ async function saveCfdis(
|
||||
) VALUES (
|
||||
gen_random_uuid(), $1, $2, $3, $4, $5, $6, $7, $8, $9, $10,
|
||||
$11, $12, $13, $14, $15, $16, $17, $18, $19, $20, $21, $22,
|
||||
$23, 'sat', $24, NOW(), NOW()
|
||||
$23, 'sat', $24::uuid, NOW(), NOW()
|
||||
)`,
|
||||
cfdi.uuidFiscal,
|
||||
cfdi.tipo,
|
||||
@@ -278,11 +278,18 @@ async function processDateRange(
|
||||
}
|
||||
|
||||
/**
|
||||
* Ejecuta sincronización inicial (últimos 10 años)
|
||||
* Ejecuta sincronización inicial o por rango personalizado
|
||||
*/
|
||||
async function processInitialSync(ctx: SyncContext, jobId: string): Promise<void> {
|
||||
async function processInitialSync(
|
||||
ctx: SyncContext,
|
||||
jobId: string,
|
||||
customDateFrom?: Date,
|
||||
customDateTo?: Date
|
||||
): Promise<void> {
|
||||
const ahora = new Date();
|
||||
const inicioHistorico = new Date(ahora.getFullYear() - YEARS_TO_SYNC, ahora.getMonth(), 1);
|
||||
// Usar fechas personalizadas si se proporcionan, sino calcular desde YEARS_TO_SYNC
|
||||
const inicioHistorico = customDateFrom || new Date(ahora.getFullYear() - YEARS_TO_SYNC, ahora.getMonth(), 1);
|
||||
const fechaFin = customDateTo || ahora;
|
||||
|
||||
let totalFound = 0;
|
||||
let totalDownloaded = 0;
|
||||
@@ -292,9 +299,9 @@ async function processInitialSync(ctx: SyncContext, jobId: string): Promise<void
|
||||
// Procesar por meses para evitar límites del SAT
|
||||
let currentDate = new Date(inicioHistorico);
|
||||
|
||||
while (currentDate < ahora) {
|
||||
while (currentDate < fechaFin) {
|
||||
const monthEnd = new Date(currentDate.getFullYear(), currentDate.getMonth() + 1, 0, 23, 59, 59);
|
||||
const rangeEnd = monthEnd > ahora ? ahora : monthEnd;
|
||||
const rangeEnd = monthEnd > fechaFin ? fechaFin : monthEnd;
|
||||
|
||||
// Procesar emitidos
|
||||
try {
|
||||
@@ -446,7 +453,7 @@ export async function startSync(
|
||||
(async () => {
|
||||
try {
|
||||
if (type === 'initial') {
|
||||
await processInitialSync(ctx, job.id);
|
||||
await processInitialSync(ctx, job.id, dateFrom, dateTo);
|
||||
} else {
|
||||
await processDailySync(ctx, job.id);
|
||||
}
|
||||
|
||||
@@ -3,6 +3,8 @@
|
||||
import { useEffect, useState } from 'react';
|
||||
import { Card, CardContent, CardDescription, CardHeader, CardTitle } from '@/components/ui/card';
|
||||
import { Button } from '@/components/ui/button';
|
||||
import { Input } from '@/components/ui/input';
|
||||
import { Label } from '@/components/ui/label';
|
||||
import { getSyncStatus, startSync } from '@/lib/api/sat';
|
||||
import type { SatSyncStatusResponse } from '@horux/shared';
|
||||
|
||||
@@ -30,6 +32,9 @@ export function SyncStatus({ fielConfigured, onSyncStarted }: SyncStatusProps) {
|
||||
const [loading, setLoading] = useState(true);
|
||||
const [startingSync, setStartingSync] = useState(false);
|
||||
const [error, setError] = useState('');
|
||||
const [showCustomDate, setShowCustomDate] = useState(false);
|
||||
const [dateFrom, setDateFrom] = useState('');
|
||||
const [dateTo, setDateTo] = useState('');
|
||||
|
||||
const fetchStatus = async () => {
|
||||
try {
|
||||
@@ -53,12 +58,21 @@ export function SyncStatus({ fielConfigured, onSyncStarted }: SyncStatusProps) {
|
||||
}
|
||||
}, [fielConfigured]);
|
||||
|
||||
const handleStartSync = async (type: 'initial' | 'daily') => {
|
||||
const handleStartSync = async (type: 'initial' | 'daily', customDates?: boolean) => {
|
||||
setStartingSync(true);
|
||||
setError('');
|
||||
try {
|
||||
await startSync({ type });
|
||||
const params: { type: 'initial' | 'daily'; dateFrom?: string; dateTo?: string } = { type };
|
||||
|
||||
if (customDates && dateFrom && dateTo) {
|
||||
// Convertir a formato completo con hora
|
||||
params.dateFrom = `${dateFrom}T00:00:00`;
|
||||
params.dateTo = `${dateTo}T23:59:59`;
|
||||
}
|
||||
|
||||
await startSync(params);
|
||||
await fetchStatus();
|
||||
setShowCustomDate(false);
|
||||
onSyncStarted?.();
|
||||
} catch (err: any) {
|
||||
setError(err.response?.data?.error || 'Error al iniciar sincronizacion');
|
||||
@@ -162,6 +176,49 @@ export function SyncStatus({ fielConfigured, onSyncStarted }: SyncStatusProps) {
|
||||
<p className="text-sm text-red-500">{error}</p>
|
||||
)}
|
||||
|
||||
{/* Formulario de fechas personalizadas */}
|
||||
{showCustomDate && (
|
||||
<div className="p-4 bg-gray-50 rounded-lg space-y-4">
|
||||
<div className="grid grid-cols-2 gap-4">
|
||||
<div>
|
||||
<Label htmlFor="dateFrom">Fecha inicio</Label>
|
||||
<Input
|
||||
id="dateFrom"
|
||||
type="date"
|
||||
value={dateFrom}
|
||||
onChange={(e) => setDateFrom(e.target.value)}
|
||||
max={dateTo || undefined}
|
||||
/>
|
||||
</div>
|
||||
<div>
|
||||
<Label htmlFor="dateTo">Fecha fin</Label>
|
||||
<Input
|
||||
id="dateTo"
|
||||
type="date"
|
||||
value={dateTo}
|
||||
onChange={(e) => setDateTo(e.target.value)}
|
||||
min={dateFrom || undefined}
|
||||
/>
|
||||
</div>
|
||||
</div>
|
||||
<div className="flex gap-2">
|
||||
<Button
|
||||
disabled={startingSync || status?.hasActiveSync || !dateFrom || !dateTo}
|
||||
onClick={() => handleStartSync('initial', true)}
|
||||
className="flex-1"
|
||||
>
|
||||
{startingSync ? 'Iniciando...' : 'Sincronizar periodo'}
|
||||
</Button>
|
||||
<Button
|
||||
variant="outline"
|
||||
onClick={() => setShowCustomDate(false)}
|
||||
>
|
||||
Cancelar
|
||||
</Button>
|
||||
</div>
|
||||
</div>
|
||||
)}
|
||||
|
||||
<div className="flex gap-3">
|
||||
<Button
|
||||
variant="outline"
|
||||
@@ -169,18 +226,27 @@ export function SyncStatus({ fielConfigured, onSyncStarted }: SyncStatusProps) {
|
||||
onClick={() => handleStartSync('daily')}
|
||||
className="flex-1"
|
||||
>
|
||||
{startingSync ? 'Iniciando...' : 'Sincronizar ahora'}
|
||||
{startingSync ? 'Iniciando...' : 'Sincronizar mes actual'}
|
||||
</Button>
|
||||
<Button
|
||||
variant="outline"
|
||||
disabled={startingSync || status?.hasActiveSync}
|
||||
onClick={() => setShowCustomDate(!showCustomDate)}
|
||||
className="flex-1"
|
||||
>
|
||||
Periodo personalizado
|
||||
</Button>
|
||||
{!status?.lastCompletedJob && (
|
||||
<Button
|
||||
disabled={startingSync || status?.hasActiveSync}
|
||||
onClick={() => handleStartSync('initial')}
|
||||
className="flex-1"
|
||||
>
|
||||
{startingSync ? 'Iniciando...' : 'Sincronizacion inicial (10 anos)'}
|
||||
</Button>
|
||||
)}
|
||||
</div>
|
||||
|
||||
{!status?.lastCompletedJob && (
|
||||
<Button
|
||||
disabled={startingSync || status?.hasActiveSync}
|
||||
onClick={() => handleStartSync('initial')}
|
||||
className="w-full"
|
||||
>
|
||||
{startingSync ? 'Iniciando...' : 'Sincronizacion inicial (6 anos)'}
|
||||
</Button>
|
||||
)}
|
||||
</CardContent>
|
||||
</Card>
|
||||
);
|
||||
|
||||
Reference in New Issue
Block a user