diff --git a/src/modules/parcel-sync/services/epay-client.ts b/src/modules/parcel-sync/services/epay-client.ts index 94003f2..0c832bf 100644 --- a/src/modules/parcel-sync/services/epay-client.ts +++ b/src/modules/parcel-sync/services/epay-client.ts @@ -202,29 +202,30 @@ export class EpayClient { JSON.stringify(allCookies), ); - const hasSession = allCookies.some( - (c) => c.key === "iPlanetDirectoryPro" || c.key === "JSESSIONID", + // OpenAM at ANCPI uses AMAuthCookie (not iPlanetDirectoryPro) + const SESSION_COOKIE_NAMES = [ + "AMAuthCookie", + "iPlanetDirectoryPro", + "JSESSIONID", + ]; + const hasSession = allCookies.some((c) => + SESSION_COOKIE_NAMES.includes(c.key), ); if (!hasSession) { - // Last resort: try the full HTML for credit info — if we can see credits, we're logged in - const checkResponse = await this.client.get( - `${BASE_URL}/LogIn.action`, - { - timeout: DEFAULT_TIMEOUT_MS, - maxRedirects: 5, - validateStatus: () => true, - }, - ); - const checkHtml = String(checkResponse.data ?? ""); - if (checkHtml.includes("puncte de credit")) { - console.log("[epay] Login successful (verified via credit check)."); - return; - } - throw new Error("ePay login failed (no session cookie)"); } + // Navigate to ePay to establish JSESSIONID (if not already there) + const hasJsessionId = allCookies.some((c) => c.key === "JSESSIONID"); + if (!hasJsessionId) { + await this.client.get(`${BASE_URL}/LogIn.action`, { + timeout: DEFAULT_TIMEOUT_MS, + maxRedirects: 5, + validateStatus: () => true, + }); + } + console.log("[epay] Login successful."); }