21a058b429
Plan 003 Faza F. Pilot users (session.useGisAc=true) get their CF
extract flow routed through api.gis.ac (RLS-filtered, RLS-owned
writes); everyone else keeps the legacy /api/ancpi/* path
unchanged. Feature-flag preserves rollback.
New routes (5):
- POST /api/cf/order → gisApi.enrichment.cf.create. Forwards
409 catalog_hit verbatim.
- GET /api/cf/orders → gisApi.enrichment.cf.list (limit, offset, status).
- GET /api/cf/[id] → gisApi.enrichment.cf.get.
- PATCH /api/cf/[id] → gisApi.enrichment.cf.patch.
- GET /api/cf/[id]/pdf → streams gisApi.enrichment.cf.getPdf
through to browser. Filename from documentName via cf.get; falls
back to cf-<id>.pdf.
- GET /api/cf/catalog → gisApi.enrichment.catalog.
All use getAuthSession() → 401 on no session, forward GisApiError
status+code+body, fallback {error:"internal_error", hint} at 500.
runtime=nodejs, dynamic=force-dynamic.
Helper module `cf-api-base.ts`:
- cfApiBase(useGisAc) → "/api/cf" | "/api/ancpi"
- adaptCfRow(row) → maps gisApi.CfExtractRow into the UI shape
expected by epay-tab.tsx (CfExtractRecord). Fields not in gis-api
(siruta, judetName, uatName, errorMessage, etc.) default to
empty/zero — filter-by-judet/uat on the pilot path is reduced
until gis-api enriches the response.
- fetchCfOrdersList, fetchCfHasCompletedForCadastral, placeCfOrder,
cfDownloadUrl — used by components.
UI changes:
- epay-tab.tsx: reads session.useGisAc; list fetch, reorder, single
+ bulk download routed via helpers. UI shape unchanged.
- epay-order-button.tsx: existence check uses catalog endpoint on
gis-ac path; order placement uses placeCfOrder which treats 409
catalog_hit as a soft success ("Extras CF valid").
Known gaps (followups):
- /api/ancpi/session still serves ePay session/credits — no gis-api
equivalent today. epay-connect.tsx untouched.
- ZIP bulk download has no gis-api analog; "Descarcă tot" falls back
to N tabs on gis-ac path.
- src/app/api/geoportal/cf-status returns hardcoded /api/ancpi/download
URL — separate followup.
Co-Authored-By: Claude Opus 4.7 (1M context) <noreply@anthropic.com>
214 lines
6.1 KiB
TypeScript
214 lines
6.1 KiB
TypeScript
"use client";
|
|
|
|
import { useState, useEffect, useCallback, useRef } from "react";
|
|
import { useSession } from "next-auth/react";
|
|
import { FileText, Loader2, Check, RefreshCw } from "lucide-react";
|
|
import { Button } from "@/shared/components/ui/button";
|
|
import {
|
|
Tooltip,
|
|
TooltipContent,
|
|
TooltipProvider,
|
|
TooltipTrigger,
|
|
} from "@/shared/components/ui/tooltip";
|
|
import { cn } from "@/shared/lib/utils";
|
|
import type { EpaySessionStatus } from "./epay-connect";
|
|
import {
|
|
fetchCfHasCompletedForCadastral,
|
|
placeCfOrder,
|
|
} from "./cf-api-base";
|
|
|
|
/* ------------------------------------------------------------------ */
|
|
/* Props */
|
|
/* ------------------------------------------------------------------ */
|
|
|
|
type Props = {
|
|
nrCadastral: string;
|
|
siruta: string;
|
|
judetName: string;
|
|
uatName: string;
|
|
/** Custom label for the button (e.g. "Actualizeaza" for expired extracts) */
|
|
label?: string;
|
|
/** Custom tooltip text */
|
|
tooltipText?: string;
|
|
};
|
|
|
|
/* ------------------------------------------------------------------ */
|
|
/* Component */
|
|
/* ------------------------------------------------------------------ */
|
|
|
|
export function EpayOrderButton({
|
|
nrCadastral,
|
|
siruta,
|
|
judetName,
|
|
uatName,
|
|
label,
|
|
tooltipText,
|
|
}: Props) {
|
|
// Plan 003, Faza F — flag drives which backend handles the order.
|
|
const { data: session } = useSession();
|
|
const useGisAc = Boolean(
|
|
(session as { useGisAc?: boolean } | null)?.useGisAc,
|
|
);
|
|
|
|
const [ordering, setOrdering] = useState(false);
|
|
const [ordered, setOrdered] = useState(false);
|
|
const [error, setError] = useState("");
|
|
const [epayStatus, setEpayStatus] = useState<EpaySessionStatus>({
|
|
connected: false,
|
|
});
|
|
const mountedRef = useRef(true);
|
|
|
|
useEffect(() => {
|
|
return () => {
|
|
mountedRef.current = false;
|
|
};
|
|
}, []);
|
|
|
|
// Check ePay status and whether an extract exists
|
|
useEffect(() => {
|
|
let cancelled = false;
|
|
|
|
const check = async () => {
|
|
try {
|
|
// ePay credit/session status is still served by the legacy route
|
|
// (gis-api does not expose ePay session state — server-side creds
|
|
// there). Read it regardless of cutover flag.
|
|
const sRes = await fetch("/api/ancpi/session");
|
|
const sData = (await sRes.json()) as EpaySessionStatus;
|
|
if (!cancelled) setEpayStatus(sData);
|
|
|
|
// Check if a completed/fresh extract already exists for this
|
|
// cadastral. Routes the call through legacy or gis-ac per flag.
|
|
const has = await fetchCfHasCompletedForCadastral(
|
|
useGisAc,
|
|
nrCadastral,
|
|
);
|
|
if (!cancelled && has) setOrdered(true);
|
|
} catch {
|
|
/* silent */
|
|
}
|
|
};
|
|
void check();
|
|
|
|
return () => {
|
|
cancelled = true;
|
|
};
|
|
}, [nrCadastral, useGisAc]);
|
|
|
|
const handleOrder = useCallback(async () => {
|
|
setOrdering(true);
|
|
setError("");
|
|
const result = await placeCfOrder(useGisAc, {
|
|
nrCadastral,
|
|
siruta,
|
|
judetName,
|
|
uatName,
|
|
});
|
|
if (mountedRef.current) {
|
|
if (result.ok) {
|
|
setOrdered(true);
|
|
} else {
|
|
setError(result.error ?? "Eroare comanda");
|
|
}
|
|
setOrdering(false);
|
|
}
|
|
}, [nrCadastral, siruta, judetName, uatName, useGisAc]);
|
|
|
|
const disabled =
|
|
!epayStatus.connected ||
|
|
(epayStatus.credits != null && epayStatus.credits < 1) ||
|
|
ordering;
|
|
|
|
const isRenew = !!label; // "Actualizeaza" mode for expired extracts
|
|
const Icon = isRenew ? RefreshCw : FileText;
|
|
|
|
const resolveTooltip = (): string => {
|
|
if (error) return error;
|
|
if (ordering) return "Se comanda...";
|
|
if (ordered) return "Extras CF valid";
|
|
if (!epayStatus.connected) return "ePay neconectat";
|
|
if (epayStatus.credits != null && epayStatus.credits < 1) return "Credite insuficiente";
|
|
return tooltipText ?? "Comanda extras CF (1 credit)";
|
|
};
|
|
|
|
if (ordered) {
|
|
return (
|
|
<TooltipProvider>
|
|
<Tooltip>
|
|
<TooltipTrigger asChild>
|
|
<Button
|
|
size="sm"
|
|
variant="ghost"
|
|
className="h-7 gap-1 px-1.5 text-emerald-600 dark:text-emerald-400"
|
|
disabled
|
|
>
|
|
<Check className="h-3.5 w-3.5" />
|
|
<span className="text-[10px]">Extras CF valid</span>
|
|
</Button>
|
|
</TooltipTrigger>
|
|
<TooltipContent>Extras CF comandat cu succes</TooltipContent>
|
|
</Tooltip>
|
|
</TooltipProvider>
|
|
);
|
|
}
|
|
|
|
if (label) {
|
|
// Render as a compact text button with label (for "Actualizeaza" etc.)
|
|
return (
|
|
<TooltipProvider>
|
|
<Tooltip>
|
|
<TooltipTrigger asChild>
|
|
<Button
|
|
size="sm"
|
|
variant="ghost"
|
|
className={cn(
|
|
"h-7 gap-1 px-1.5 text-[10px]",
|
|
error && "text-destructive",
|
|
ordering && "text-muted-foreground",
|
|
)}
|
|
disabled={disabled}
|
|
onClick={() => void handleOrder()}
|
|
>
|
|
{ordering ? (
|
|
<Loader2 className="h-3 w-3 animate-spin" />
|
|
) : (
|
|
<Icon className="h-3 w-3" />
|
|
)}
|
|
{ordering ? "Se comanda..." : label}
|
|
</Button>
|
|
</TooltipTrigger>
|
|
<TooltipContent>{resolveTooltip()}</TooltipContent>
|
|
</Tooltip>
|
|
</TooltipProvider>
|
|
);
|
|
}
|
|
|
|
// Default: icon-only button
|
|
return (
|
|
<TooltipProvider>
|
|
<Tooltip>
|
|
<TooltipTrigger asChild>
|
|
<Button
|
|
size="sm"
|
|
variant="ghost"
|
|
className={cn(
|
|
"h-7 w-7 p-0",
|
|
error && "text-destructive",
|
|
ordering && "text-muted-foreground",
|
|
)}
|
|
disabled={disabled}
|
|
onClick={() => void handleOrder()}
|
|
>
|
|
{ordering ? (
|
|
<Loader2 className="h-3.5 w-3.5 animate-spin" />
|
|
) : (
|
|
<FileText className="h-3.5 w-3.5" />
|
|
)}
|
|
</Button>
|
|
</TooltipTrigger>
|
|
<TooltipContent>{resolveTooltip()}</TooltipContent>
|
|
</Tooltip>
|
|
</TooltipProvider>
|
|
);
|
|
}
|