feat(ancpi): complete ePay UI + dedup protection

UI Components (Phase 4):
- epay-connect.tsx: connection widget with credit badge, auto-connect
- epay-order-button.tsx: per-parcel "Extras CF" button with status
- epay-tab.tsx: full "Extrase CF" tab with orders table, filters,
  download/refresh actions, new order form
- Minimal changes to parcel-sync-module.tsx: 5th tab + button on
  search results + ePay connect widget

Dedup Protection:
- epay-queue.ts: batch-level dedup (60s window, canonical key from
  sorted cadastral numbers)
- order/route.ts: request nonce idempotency (60s cache)
- test/route.ts: refresh protection (30s cache)

Co-Authored-By: Claude Opus 4.6 (1M context) <noreply@anthropic.com>
This commit is contained in:
AI Assistant
2026-03-23 04:19:19 +02:00
parent fcc6f8cc20
commit c9ecd284c7
7 changed files with 1221 additions and 26 deletions
@@ -0,0 +1,171 @@
"use client";
import { useState, useEffect, useCallback, useRef } from "react";
import {
Loader2,
Wifi,
WifiOff,
LogOut,
CreditCard,
} from "lucide-react";
import { Button } from "@/shared/components/ui/button";
import { Badge } from "@/shared/components/ui/badge";
import { cn } from "@/shared/lib/utils";
/* ------------------------------------------------------------------ */
/* Types */
/* ------------------------------------------------------------------ */
export type EpaySessionStatus = {
connected: boolean;
username?: string;
connectedAt?: string;
credits?: number;
creditsCheckedAt?: string;
};
/* ------------------------------------------------------------------ */
/* Component */
/* ------------------------------------------------------------------ */
export function EpayConnect({
onStatusChange,
}: {
onStatusChange?: (status: EpaySessionStatus) => void;
}) {
const [status, setStatus] = useState<EpaySessionStatus>({ connected: false });
const [connecting, setConnecting] = useState(false);
const [error, setError] = useState("");
const pollRef = useRef<ReturnType<typeof setInterval> | null>(null);
const cbRef = useRef(onStatusChange);
cbRef.current = onStatusChange;
const fetchStatus = useCallback(async () => {
try {
const res = await fetch("/api/ancpi/session");
const data = (await res.json()) as EpaySessionStatus;
setStatus(data);
cbRef.current?.(data);
if (data.connected) setError("");
} catch {
/* silent */
}
}, []);
// Poll every 30s
useEffect(() => {
void fetchStatus();
pollRef.current = setInterval(() => void fetchStatus(), 30_000);
return () => {
if (pollRef.current) clearInterval(pollRef.current);
};
}, [fetchStatus]);
const connect = async () => {
setConnecting(true);
setError("");
try {
const res = await fetch("/api/ancpi/session", {
method: "POST",
headers: { "Content-Type": "application/json" },
body: JSON.stringify({}),
});
const data = (await res.json()) as { success?: boolean; credits?: number; error?: string };
if (!res.ok || data.error) {
setError(data.error ?? "Eroare conectare ePay");
} else {
await fetchStatus();
}
} catch {
setError("Eroare rețea");
} finally {
setConnecting(false);
}
};
const disconnect = async () => {
try {
await fetch("/api/ancpi/session", {
method: "POST",
headers: { "Content-Type": "application/json" },
body: JSON.stringify({ action: "disconnect" }),
});
await fetchStatus();
} catch {
/* silent */
}
};
return (
<div className="flex items-center gap-2">
{/* Status pill */}
<div
className={cn(
"flex items-center gap-1.5 rounded-full border px-2.5 py-1 text-xs font-medium",
status.connected
? "border-emerald-200 bg-emerald-50/80 text-emerald-700 dark:border-emerald-800 dark:bg-emerald-950/40 dark:text-emerald-400"
: error
? "border-rose-200 bg-rose-50/80 text-rose-600 dark:border-rose-800 dark:bg-rose-950/40 dark:text-rose-400"
: "border-muted-foreground/20 bg-muted/50 text-muted-foreground",
)}
>
{connecting ? (
<Loader2 className="h-3 w-3 animate-spin" />
) : status.connected ? (
<span className="relative flex h-2 w-2">
<span className="absolute inline-flex h-full w-full animate-ping rounded-full bg-emerald-400 opacity-75" />
<span className="relative inline-flex h-2 w-2 rounded-full bg-emerald-500" />
</span>
) : error ? (
<WifiOff className="h-3 w-3" />
) : (
<Wifi className="h-3 w-3 opacity-50" />
)}
<span className="hidden sm:inline">ePay</span>
{status.connected && status.credits != null && (
<Badge
variant="secondary"
className="ml-0.5 h-4 px-1.5 text-[10px] font-semibold"
>
<CreditCard className="mr-0.5 h-2.5 w-2.5" />
{status.credits}
</Badge>
)}
</div>
{/* Action button */}
{status.connected ? (
<Button
variant="ghost"
size="sm"
className="h-6 px-1.5 text-[10px]"
onClick={() => void disconnect()}
>
<LogOut className="h-3 w-3" />
</Button>
) : (
<Button
variant="outline"
size="sm"
className="h-6 px-2 text-[10px]"
disabled={connecting}
onClick={() => void connect()}
>
{connecting ? (
<Loader2 className="mr-1 h-3 w-3 animate-spin" />
) : null}
Conectare
</Button>
)}
{/* Error tooltip */}
{error && !status.connected && (
<span className="text-[10px] text-rose-500 max-w-40 truncate" title={error}>
{error}
</span>
)}
</div>
);
}