feat(ancpi): add test endpoint for step-by-step ePay verification
GET /api/ancpi/test?step=login|uats|search|order Temporary diagnostic route to test ePay integration before building UI. Co-Authored-By: Claude Opus 4.6 (1M context) <noreply@anthropic.com>
This commit is contained in:
@@ -0,0 +1,206 @@
|
|||||||
|
import { NextResponse } from "next/server";
|
||||||
|
import { EpayClient } from "@/modules/parcel-sync/services/epay-client";
|
||||||
|
import { resolveEpayCountyIndex } from "@/modules/parcel-sync/services/epay-counties";
|
||||||
|
import {
|
||||||
|
createEpaySession,
|
||||||
|
getEpayCredentials,
|
||||||
|
} from "@/modules/parcel-sync/services/epay-session-store";
|
||||||
|
import { enqueueOrder } from "@/modules/parcel-sync/services/epay-queue";
|
||||||
|
|
||||||
|
export const runtime = "nodejs";
|
||||||
|
export const dynamic = "force-dynamic";
|
||||||
|
|
||||||
|
/**
|
||||||
|
* GET /api/ancpi/test?step=login|credits|uats|search|order
|
||||||
|
*
|
||||||
|
* Temporary diagnostic endpoint to test ePay integration step-by-step.
|
||||||
|
*
|
||||||
|
* Steps:
|
||||||
|
* login — test login + show credits
|
||||||
|
* uats — resolve ePay UAT IDs for Cluj-Napoca, Feleacu, Florești
|
||||||
|
* search — search estates for the 3 test parcels
|
||||||
|
* order — enqueue orders for the 3 test parcels (USES 3 CREDITS!)
|
||||||
|
*/
|
||||||
|
export async function GET(req: Request) {
|
||||||
|
const url = new URL(req.url);
|
||||||
|
const step = url.searchParams.get("step") ?? "login";
|
||||||
|
|
||||||
|
const username = process.env.ANCPI_USERNAME ?? "";
|
||||||
|
const password = process.env.ANCPI_PASSWORD ?? "";
|
||||||
|
|
||||||
|
if (!username || !password) {
|
||||||
|
return NextResponse.json({
|
||||||
|
error: "ANCPI_USERNAME / ANCPI_PASSWORD not configured",
|
||||||
|
});
|
||||||
|
}
|
||||||
|
|
||||||
|
try {
|
||||||
|
// ── Step: login ──
|
||||||
|
if (step === "login") {
|
||||||
|
const client = await EpayClient.create(username, password);
|
||||||
|
const credits = await client.getCredits();
|
||||||
|
createEpaySession(username, password, credits);
|
||||||
|
return NextResponse.json({
|
||||||
|
step: "login",
|
||||||
|
success: true,
|
||||||
|
credits,
|
||||||
|
countyIdxCluj: resolveEpayCountyIndex("Cluj"),
|
||||||
|
});
|
||||||
|
}
|
||||||
|
|
||||||
|
// ── Step: uats ──
|
||||||
|
if (step === "uats") {
|
||||||
|
const client = await EpayClient.create(username, password);
|
||||||
|
const countyIdx = resolveEpayCountyIndex("Cluj");
|
||||||
|
if (countyIdx === null) {
|
||||||
|
return NextResponse.json({ error: "Could not resolve Cluj county index" });
|
||||||
|
}
|
||||||
|
|
||||||
|
const uatList = await client.getUatList(countyIdx);
|
||||||
|
// Find the 3 UATs
|
||||||
|
const normalize = (s: string) =>
|
||||||
|
s.normalize("NFD").replace(/[\u0300-\u036f]/g, "").toUpperCase();
|
||||||
|
|
||||||
|
const clujNapoca = uatList.find((u) => normalize(u.value).includes("CLUJ-NAPOCA") || normalize(u.value).includes("CLUJNAPOCA"));
|
||||||
|
const feleacu = uatList.find((u) => normalize(u.value).includes("FELEACU"));
|
||||||
|
const floresti = uatList.find((u) => normalize(u.value).includes("FLORESTI"));
|
||||||
|
|
||||||
|
return NextResponse.json({
|
||||||
|
step: "uats",
|
||||||
|
countyIdx,
|
||||||
|
totalUats: uatList.length,
|
||||||
|
first5: uatList.slice(0, 5),
|
||||||
|
clujNapoca,
|
||||||
|
feleacu,
|
||||||
|
floresti,
|
||||||
|
});
|
||||||
|
}
|
||||||
|
|
||||||
|
// ── Step: search ──
|
||||||
|
if (step === "search") {
|
||||||
|
const client = await EpayClient.create(username, password);
|
||||||
|
const countyIdx = resolveEpayCountyIndex("Cluj")!;
|
||||||
|
const uatList = await client.getUatList(countyIdx);
|
||||||
|
const normalize = (s: string) =>
|
||||||
|
s.normalize("NFD").replace(/[\u0300-\u036f]/g, "").toUpperCase();
|
||||||
|
|
||||||
|
const findUat = (name: string) =>
|
||||||
|
uatList.find((u) => normalize(u.value).includes(name));
|
||||||
|
|
||||||
|
const clujNapoca = findUat("CLUJ-NAPOCA") ?? findUat("CLUJNAPOCA");
|
||||||
|
const feleacu = findUat("FELEACU");
|
||||||
|
const floresti = findUat("FLORESTI");
|
||||||
|
|
||||||
|
const results: Record<string, unknown> = {};
|
||||||
|
|
||||||
|
if (clujNapoca) {
|
||||||
|
results["345295_ClujNapoca"] = await client.searchEstate(
|
||||||
|
"345295", countyIdx, clujNapoca.id
|
||||||
|
);
|
||||||
|
}
|
||||||
|
if (feleacu) {
|
||||||
|
results["63565_Feleacu"] = await client.searchEstate(
|
||||||
|
"63565", countyIdx, feleacu.id
|
||||||
|
);
|
||||||
|
}
|
||||||
|
if (floresti) {
|
||||||
|
results["88089_Floresti"] = await client.searchEstate(
|
||||||
|
"88089", countyIdx, floresti.id
|
||||||
|
);
|
||||||
|
}
|
||||||
|
|
||||||
|
return NextResponse.json({
|
||||||
|
step: "search",
|
||||||
|
uats: { clujNapoca, feleacu, floresti },
|
||||||
|
results,
|
||||||
|
});
|
||||||
|
}
|
||||||
|
|
||||||
|
// ── Step: order ── (USES 3 CREDITS!)
|
||||||
|
if (step === "order") {
|
||||||
|
// Ensure session exists
|
||||||
|
if (!getEpayCredentials()) {
|
||||||
|
createEpaySession(username, password, 0);
|
||||||
|
}
|
||||||
|
|
||||||
|
const client = await EpayClient.create(username, password);
|
||||||
|
const credits = await client.getCredits();
|
||||||
|
createEpaySession(username, password, credits);
|
||||||
|
|
||||||
|
if (credits < 3) {
|
||||||
|
return NextResponse.json({
|
||||||
|
error: `Doar ${credits} credite disponibile, trebuie 3.`,
|
||||||
|
});
|
||||||
|
}
|
||||||
|
|
||||||
|
const countyIdx = resolveEpayCountyIndex("Cluj")!;
|
||||||
|
const uatList = await client.getUatList(countyIdx);
|
||||||
|
const normalize = (s: string) =>
|
||||||
|
s.normalize("NFD").replace(/[\u0300-\u036f]/g, "").toUpperCase();
|
||||||
|
|
||||||
|
const findUat = (name: string) =>
|
||||||
|
uatList.find((u) => normalize(u.value).includes(name));
|
||||||
|
|
||||||
|
const clujNapoca = findUat("CLUJ-NAPOCA") ?? findUat("CLUJNAPOCA");
|
||||||
|
const feleacu = findUat("FELEACU");
|
||||||
|
const floresti = findUat("FLORESTI");
|
||||||
|
|
||||||
|
if (!clujNapoca || !feleacu || !floresti) {
|
||||||
|
return NextResponse.json({
|
||||||
|
error: "Nu s-au găsit UAT-urile.",
|
||||||
|
clujNapoca, feleacu, floresti,
|
||||||
|
});
|
||||||
|
}
|
||||||
|
|
||||||
|
const parcels = [
|
||||||
|
{
|
||||||
|
nrCadastral: "345295",
|
||||||
|
judetIndex: countyIdx,
|
||||||
|
judetName: "CLUJ",
|
||||||
|
uatId: clujNapoca.id,
|
||||||
|
uatName: "Cluj-Napoca",
|
||||||
|
},
|
||||||
|
{
|
||||||
|
nrCadastral: "63565",
|
||||||
|
judetIndex: countyIdx,
|
||||||
|
judetName: "CLUJ",
|
||||||
|
uatId: feleacu.id,
|
||||||
|
uatName: "Feleacu",
|
||||||
|
},
|
||||||
|
{
|
||||||
|
nrCadastral: "88089",
|
||||||
|
judetIndex: countyIdx,
|
||||||
|
judetName: "CLUJ",
|
||||||
|
uatId: floresti.id,
|
||||||
|
uatName: "Florești",
|
||||||
|
},
|
||||||
|
];
|
||||||
|
|
||||||
|
const ids: string[] = [];
|
||||||
|
for (const p of parcels) {
|
||||||
|
const id = await enqueueOrder(p);
|
||||||
|
ids.push(id);
|
||||||
|
}
|
||||||
|
|
||||||
|
return NextResponse.json({
|
||||||
|
step: "order",
|
||||||
|
credits,
|
||||||
|
message: `Enqueued ${ids.length} orders. Queue processing started.`,
|
||||||
|
orderIds: ids,
|
||||||
|
parcels: parcels.map((p, i) => ({
|
||||||
|
nrCadastral: p.nrCadastral,
|
||||||
|
uatName: p.uatName,
|
||||||
|
extractId: ids[i],
|
||||||
|
})),
|
||||||
|
});
|
||||||
|
}
|
||||||
|
|
||||||
|
return NextResponse.json({
|
||||||
|
error: `Unknown step: ${step}. Use: login, uats, search, order`,
|
||||||
|
});
|
||||||
|
} catch (error) {
|
||||||
|
const message = error instanceof Error ? error.message : String(error);
|
||||||
|
console.error(`[ancpi-test] Step ${step} failed:`, message);
|
||||||
|
return NextResponse.json({ error: message, step }, { status: 500 });
|
||||||
|
}
|
||||||
|
}
|
||||||
Reference in New Issue
Block a user