diff --git a/src/modules/parcel-sync/services/epay-client.ts b/src/modules/parcel-sync/services/epay-client.ts index ef9a0dd..5eaae0e 100644 --- a/src/modules/parcel-sync/services/epay-client.ts +++ b/src/modules/parcel-sync/services/epay-client.ts @@ -540,10 +540,21 @@ export class EpayClient { else if (html.includes("Plata refuzata")) status = "Plata refuzata"; const documents: EpaySolutionDoc[] = []; - const docPattern = + + // Try multiple patterns to find document info in the Angular HTML + // Pattern 1: JSON-like in Angular scope + const docPattern1 = /"idDocument"\s*:\s*(\d+)[\s\S]*?"nume"\s*:\s*"([^"]+)"[\s\S]*?"dataDocument"\s*:\s*"([^"]+)"[\s\S]*?"linkDownload"\s*:\s*"([^"]*)"/g; + + // Pattern 2: DownloadFile links in HTML + const docPattern2 = + /DownloadFile\.action\?typeD=4&(?:amp;)?id=(\d+)/g; + + // Pattern 3: Look for solutii JSON array + const solutiiMatch = html.match(/solutii['"]\s*:\s*(\[[\s\S]*?\])/); + let docMatch; - while ((docMatch = docPattern.exec(html)) !== null) { + while ((docMatch = docPattern1.exec(html)) !== null) { documents.push({ idDocument: parseInt(docMatch[1] ?? "0", 10), idTipDocument: null, @@ -560,6 +571,78 @@ export class EpayClient { }); } + // If pattern 1 didn't find anything, try pattern 2 (DownloadFile links) + if (documents.length === 0) { + while ((docMatch = docPattern2.exec(html)) !== null) { + const docId = parseInt(docMatch[1] ?? "0", 10); + if (docId > 0 && !documents.some((d) => d.idDocument === docId)) { + documents.push({ + idDocument: docId, + idTipDocument: null, + nume: `document_${docId}.pdf`, + numar: null, + serie: null, + dataDocument: new Date().toISOString().slice(0, 10), + contentType: "application/pdf", + linkDownload: "", + downloadValabil: true, + valabilNelimitat: true, + zileValabilitateDownload: -1, + transactionId: 0, + }); + } + } + } + + // If pattern 3 matched, try to parse JSON + if (documents.length === 0 && solutiiMatch) { + try { + const solutii = JSON.parse(solutiiMatch[1] ?? "[]"); + for (const s of solutii) { + if (s?.idDocument) { + documents.push({ + idDocument: s.idDocument, + idTipDocument: s.idTipDocument ?? null, + nume: s.nume ?? `document_${s.idDocument}.pdf`, + numar: s.numar ?? null, + serie: s.serie ?? null, + dataDocument: s.dataDocument ?? "", + contentType: s.contentType ?? "application/pdf", + linkDownload: s.linkDownload ?? "", + downloadValabil: s.downloadValabil ?? true, + valabilNelimitat: s.valabilNelimitat ?? true, + zileValabilitateDownload: s.zileValabilitateDownload ?? -1, + transactionId: s.transactionId ?? 0, + }); + } + } + } catch { /* parse failed */ } + } + + // Debug: if no documents found, log relevant HTML sections + if (documents.length === 0 && status === "Finalizata") { + // Find sections containing "idDocument", "DownloadFile", "solutii", or "Extras" + const relevantPatterns = [ + /idDocument[^<]{0,200}/gi, + /DownloadFile[^<]{0,200}/gi, + /solutii[^<]{0,200}/gi, + /Extras_Informare[^<]{0,200}/gi, + /downloadFile[^<]{0,200}/gi, + /tipFisier[^<]{0,200}/gi, + ]; + for (const pat of relevantPatterns) { + const matches = html.match(pat); + if (matches) { + console.log(`[epay] OrderDetails ${orderId} match [${pat.source.slice(0, 20)}]:`, matches.slice(0, 3)); + } + } + // Also log around "Finalizata" for context + const finIdx = html.indexOf("Finalizata"); + if (finIdx >= 0) { + console.log(`[epay] OrderDetails ${orderId} near Finalizata:`, html.slice(finIdx - 100, finIdx + 300).replace(/\s+/g, " ")); + } + } + return { orderId, status, documents }; }