fix(ancpi): add SearchEstate debug logging, try without uatId, add cart first
SearchEstate might need active cart and/or different headers. Add X-Requested-With: XMLHttpRequest, make uatId optional, log raw response (type, length, sample), and add-to-cart before searching. Co-Authored-By: Claude Opus 4.6 (1M context) <noreply@anthropic.com>
This commit is contained in:
@@ -81,26 +81,31 @@ export async function GET(req: Request) {
|
|||||||
const client = await EpayClient.create(username, password);
|
const client = await EpayClient.create(username, password);
|
||||||
const countyIdx = resolveEpayCountyIndex("Cluj")!;
|
const countyIdx = resolveEpayCountyIndex("Cluj")!;
|
||||||
|
|
||||||
// Try SearchEstate with different uatId values to find what works
|
|
||||||
const results: Record<string, unknown> = {};
|
const results: Record<string, unknown> = {};
|
||||||
|
|
||||||
// Test 1: uatId=-1 (maybe "all")
|
// First add to cart — SearchEstate might only work with active cart
|
||||||
results["345295_uatNeg1"] = await client.searchEstate("345295", countyIdx, -1).catch((e: Error) => ({ error: e.message }));
|
let basketRowId: number | null = null;
|
||||||
|
try {
|
||||||
|
basketRowId = await client.addToCart(14200);
|
||||||
|
results["_basketRowId"] = basketRowId;
|
||||||
|
} catch (e) {
|
||||||
|
results["_cartError"] = (e as Error).message;
|
||||||
|
}
|
||||||
|
|
||||||
// Test 2: uatId=0
|
// Test SearchEstate: without uatId (optional now)
|
||||||
results["345295_uat0"] = await client.searchEstate("345295", countyIdx, 0).catch((e: Error) => ({ error: e.message }));
|
results["345295_noUat"] = await client.searchEstate("345295", countyIdx).catch((e: Error) => ({ error: e.message }));
|
||||||
|
|
||||||
// Test 3: uatId=24 (from spec: Cluj-Napoca is index 24)
|
// Test with uatId=24 (spec says Cluj-Napoca)
|
||||||
results["345295_uat24"] = await client.searchEstate("345295", countyIdx, 24).catch((e: Error) => ({ error: e.message }));
|
results["345295_uat24"] = await client.searchEstate("345295", countyIdx, 24).catch((e: Error) => ({ error: e.message }));
|
||||||
|
|
||||||
// Test 4: no uatId at all — modify searchEstate to support optional
|
// Other parcels without uatId
|
||||||
// For now just try a few indices for Feleacu/Florești
|
results["63565_noUat"] = await client.searchEstate("63565", countyIdx).catch((e: Error) => ({ error: e.message }));
|
||||||
results["63565_uat0"] = await client.searchEstate("63565", countyIdx, 0).catch((e: Error) => ({ error: e.message }));
|
results["88089_noUat"] = await client.searchEstate("88089", countyIdx).catch((e: Error) => ({ error: e.message }));
|
||||||
results["88089_uat0"] = await client.searchEstate("88089", countyIdx, 0).catch((e: Error) => ({ error: e.message }));
|
|
||||||
|
|
||||||
return NextResponse.json({
|
return NextResponse.json({
|
||||||
step: "search",
|
step: "search",
|
||||||
countyIdx,
|
countyIdx,
|
||||||
|
basketRowId,
|
||||||
results,
|
results,
|
||||||
});
|
});
|
||||||
}
|
}
|
||||||
|
|||||||
@@ -329,33 +329,52 @@ export class EpayClient {
|
|||||||
async searchEstate(
|
async searchEstate(
|
||||||
identifier: string,
|
identifier: string,
|
||||||
countyIdx: number,
|
countyIdx: number,
|
||||||
uatId: number,
|
uatId?: number,
|
||||||
): Promise<EpaySearchResult[]> {
|
): Promise<EpaySearchResult[]> {
|
||||||
return this.retryOnAuthFail(async () => {
|
return this.retryOnAuthFail(async () => {
|
||||||
const body = new URLSearchParams();
|
const body = new URLSearchParams();
|
||||||
body.set("identifier", identifier);
|
body.set("identifier", identifier);
|
||||||
body.set("countyId", String(countyIdx));
|
body.set("countyId", String(countyIdx));
|
||||||
body.set("uatId", String(uatId));
|
if (uatId != null && uatId >= 0) {
|
||||||
|
body.set("uatId", String(uatId));
|
||||||
|
}
|
||||||
|
|
||||||
const response = await this.client.post(
|
const response = await this.client.post(
|
||||||
`${BASE_URL}/SearchEstate.action`,
|
`${BASE_URL}/SearchEstate.action`,
|
||||||
body.toString(),
|
body.toString(),
|
||||||
{
|
{
|
||||||
headers: { "Content-Type": "application/x-www-form-urlencoded" },
|
headers: {
|
||||||
|
"Content-Type": "application/x-www-form-urlencoded",
|
||||||
|
Accept: "application/json, text/plain, */*",
|
||||||
|
"X-Requested-With": "XMLHttpRequest",
|
||||||
|
},
|
||||||
timeout: DEFAULT_TIMEOUT_MS,
|
timeout: DEFAULT_TIMEOUT_MS,
|
||||||
},
|
},
|
||||||
);
|
);
|
||||||
|
|
||||||
const data = response.data;
|
const data = response.data;
|
||||||
|
|
||||||
|
// Log raw response for debugging
|
||||||
|
const rawStr = typeof data === "string" ? data : JSON.stringify(data);
|
||||||
|
console.log(
|
||||||
|
`[epay] SearchEstate(${identifier}, county=${countyIdx}, uat=${uatId ?? "none"}):`,
|
||||||
|
`type=${typeof data}, len=${rawStr?.length ?? 0}, sample=${rawStr?.slice(0, 300)}`,
|
||||||
|
);
|
||||||
|
|
||||||
if (Array.isArray(data)) return data as EpaySearchResult[];
|
if (Array.isArray(data)) return data as EpaySearchResult[];
|
||||||
|
|
||||||
// Sometimes wrapped in a string
|
|
||||||
if (typeof data === "string") {
|
if (typeof data === "string") {
|
||||||
try {
|
// Try JSON parse
|
||||||
const parsed = JSON.parse(data);
|
const trimmed = data.trim();
|
||||||
if (Array.isArray(parsed)) return parsed as EpaySearchResult[];
|
if (trimmed.startsWith("[") || trimmed.startsWith("{")) {
|
||||||
} catch {
|
try {
|
||||||
// Not JSON
|
const parsed = JSON.parse(trimmed);
|
||||||
|
if (Array.isArray(parsed)) return parsed as EpaySearchResult[];
|
||||||
|
// Wrapped in object?
|
||||||
|
if (parsed?.results) return parsed.results as EpaySearchResult[];
|
||||||
|
} catch {
|
||||||
|
// Not JSON
|
||||||
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|||||||
Reference in New Issue
Block a user