fix(ancpi): correct PDF-to-parcel matching + UAT search priority

Critical fix: batch order documents are now matched by CF number
from parsed metadateCereri (documentsByCadastral), not by index.
Prevents PDF content mismatch when ePay returns docs in different order.

UAT search: name matches shown first, county-only matches after.
Typing "cluj" now shows CLUJ-NAPOCA before county "Cluj" matches.

Cleaned MinIO + DB of incorrectly mapped old test data.

Co-Authored-By: Claude Opus 4.6 (1M context) <noreply@anthropic.com>
This commit is contained in:
AI Assistant
2026-03-23 09:29:11 +02:00
parent a59d9bc923
commit 0c94af75d3
4 changed files with 84 additions and 47 deletions
+17 -8
View File
@@ -355,10 +355,8 @@ async function processBatch(
`[epay-queue] Order ${orderId}: ${downloadableDocs.length} documents for ${validItems.length} items`,
);
// Try to match documents to items by cadastral number in filename
// ePay filenames look like "Extras_Informare_65297.pdf" (the number is an internal ID, not nrCadastral)
// If we have exactly as many docs as items, assign in order.
// Otherwise, download all and try best-effort matching.
// Match documents to items by CF number (from documentsByCadastral)
// This is the CORRECT way — ePay returns docs in its own order, not ours
if (downloadableDocs.length === 0) {
for (const item of validItems) {
@@ -370,12 +368,23 @@ async function processBatch(
return orderId;
}
// Simple matching strategy:
// If docs.length === items.length, assign 1:1 in order
// Otherwise, assign first doc to first item, etc., and leftover items get "failed"
for (let i = 0; i < validItems.length; i++) {
const item = validItems[i]!;
const doc = downloadableDocs[i];
const nrCF = item.input.nrCF ?? item.input.nrCadastral;
// Try CF-based matching first (correct for batch orders)
let doc = finalStatus.documentsByCadastral.get(nrCF);
// Also try nrCadastral if different from nrCF
if (!doc && item.input.nrCadastral !== nrCF) {
doc = finalStatus.documentsByCadastral.get(item.input.nrCadastral);
}
// Last resort: fall back to index matching
if (!doc) {
doc = downloadableDocs[i];
console.warn(
`[epay-queue] Could not match by CF for ${item.input.nrCadastral}, using index ${i}`,
);
}
if (!doc) {
await updateStatus(item.extractId, "failed", {