Skip to content
πŸ“¦ ARCHIVED SNAPSHOT (v1.0) You are viewing historical business logic and documentation for Microservices (PHP) v1.0 (frozen on 2026-11-23, commit 96144c89). Switch to Latest β†’

5. eIDAS Electronic Agreement Signing Portal

Repository: Microservices (planovi-microservices) β€’ Rule Prefix: BR-SIG β€’ Documented Rules: 7 β€’ Open [TEAM_WHY_NEEDED] Questions: 2

1. Business Purpose & Actors

  • Problem Solved: Provides a white-labeled web signing portal where agreement signatories review an agreement document (HTML srcdoc or PDF iframe) and execute a Simple Electronic Signature (eIDAS Regulation (EU) 910/2014) backed by planovi-backend’s signing-api Edge Function (which records IP, UTC timestamp, and SHA-256 hash).
  • Actors:
    • Agreement Signatory (Client / Counterparty / Cooperative Rep): Receives an email link containing ?signatoryId={id}&token={token}, reviews the document, types their full name, and clicks "Podpisz umowe".
    • planovi-backend (signing-api Edge Function): Authorizes token, returns agreement content + tenant branding from company_settings, records signature, and emails final signed document once all signatories complete.

2. All Features & End-to-End Flows (Happy Path + Error Paths)

  • Feature A: Load & Verify Signing Session (GET agreements/sign.php?signatoryId=...&token=... or dealflow/sign.php?...)
    • Trigger: Signatory opens link from invitation email.
    • Validation: On page load, init() checks SID and TOK are non-empty; if missing β†’ stateError ("Nieprawidlowy link" / "Brak parametrow signatoryId lub token. Sprawdz email i sprobuj ponownie.").
    • API Call: POST {SUPABASE_URL}/functions/v1/signing-api with {"signatoryId": SID, "token": TOK, "action": "load"} (no apikey/Authorization headers sent β€” relies on verify_jwt = false on signing-api).
    • Response Handling:
      • data.error β†’ stateError ("Blad weryfikacji", message = data.error).
      • data.alreadySigned β†’ stateAlreadySigned ("Juz podpisano").
      • Valid pending signatory β†’ Populates signatory name/email, signature level badge ('Poziom: ' + (data.signatureLevel || 'SES')), signing order ('Kolejnosc podpisu: ' + data.signingOrder), applies dynamic tenant branding (companyName, companyTagline, companyLogo), loads document into #docFrame (frame.srcdoc = data.htmlContent preferred over frame.src = data.pdfUrl, else hides iframe), and transitions to stateMain.
  • Feature B: Name Input Soft-Check & Signature Execution
    • Validation (onNameInput()): Button #signBtn stays disabled while typedName.length < 2. Once >= 2, normalizes case and whitespace (toLowerCase().replace(/\s+/g,' ').trim()) against SNAME:
      • Exact normalized match β†’ green hint 'Imie i nazwisko zgodne'
      • Mismatch β†’ amber warning 'Nie pasuje dokladnie β€” mozesz podpisac mimo to' (does NOT block signing).
    • API Call (doSign()): Disables button, shows spinner, posts {"signatoryId": SID, "token": TOK, "action": "sign", "typedName": typedName} to signing-api.
    • Response Handling:
      • data.error === 'already_signed' β†’ stateAlreadySigned.
      • !data.success β†’ throws error, re-enables button, shows inline #errBox.
      • data.success β†’ shows stateSuccess with different copy for data.allSigned === true ("Wszystkie podpisy zostaly zlozone! Obie strony otrzymaja email z podpisana umowa.") vs false ("Twoj podpis zostal zarejestrowany. Oczekujemy na podpisy pozostalych sygnatariuszy.").

3. Hard Business Rules, Validations, Calculations & Edge Cases

  • BR-SIG-01 (Exact Duplicate Across Two Microservices): agreements/sign.php and dealflow/sign.php are byte-for-byte identical (523 lines each, diff exit 0).
  • BR-SIG-02 (Minimum Signature Length & Non-Blocking Name Match): typedName.trim().length >= 2 is required to enable signing (sign.php:394, 474). Name mismatch against data.signatoryName displays an amber hint but deliberately permits signing (sign.php:401-407).
  • BR-SIG-03 (Document Rendering Precedence): data.htmlContent is rendered via iframe.srcdoc (avoiding Content-Type issues, sign.php:455-458); falls back to iframe.src = data.pdfUrl; if neither exists, #docFrame is hidden. Note: #docFrame has no sandbox attribute.
  • BR-SIG-04 (Dynamic Company Branding Injection): If data.companyLogo is returned, JS prepends a new <img> inside #brandLogo (sign.php:445-453) WITHOUT hiding the default #brandIcon hexagon SVG or #brandName span, resulting in both the uploaded logo and default Planovi hexagon icon showing simultaneously.
  • BR-SIG-05 (Theme Storage Key Typo): sign.php:366, 369 stores dark/light toggle in localStorage['planowi_theme'] (with w), whereas inquiry.php uses planovi_theme (with v).
  • BR-SIG-06 (Unstripped Quotes in .env Loader & Unused Env Var): sign_load_env() (sign.php:6-15) does not strip surrounding quotes from .env values. $logoUrl = $_ENV['PLANOVI_LOGO_URL'] ?? ''; (sign.php:23) is loaded and never referenced.
  • BR-SIG-07 (Anti-Cache Headers): Explicitly sends Cache-Control: no-store, no-cache, must-revalidate, max-age=0 and Pragma: no-cache (sign.php:2-3).

4. State Machines & Data Lifecycle

Signing Portal UI State Machine (showState()):

stateDiagram-v2
    [*] --> stateLoading: Page load -> init()
    stateLoading --> stateError: Missing SID/TOK OR data.error OR network failure
    stateLoading --> stateAlreadySigned: data.alreadySigned == true
    stateLoading --> stateMain: Valid pending signatory loaded
    stateMain --> stateAlreadySigned: doSign() -> data.error == 'already_signed'
    stateMain --> stateSuccess: doSign() -> data.success == true (allSigned true/false)
    stateMain --> stateMain: doSign() error -> inline errBox + retry
    stateError --> [*]
    stateAlreadySigned --> [*]
    stateSuccess --> [*]

5. Technical Contract (Inputs β†’ Processing β†’ Side Effects)

  • Endpoints Exposed:
    • GET /agreements/sign.php?signatoryId={id}&token={token}
    • GET /dealflow/sign.php?signatoryId={id}&token={token}
  • External API Called: POST {SUPABASE_URL}/functions/v1/signing-api
    • Action load: Request {signatoryId, token, action: "load"} β†’ Response {signatoryName, signatoryEmail, title, signatureLevel, signingOrder, companyName, companyTagline, companyLogo, htmlContent?, pdfUrl?, alreadySigned?, error?}.
    • Action sign: Request {signatoryId, token, action: "sign", typedName} β†’ Response {success, allSigned?, error?}.

6. Cross-Repository Dependencies

CallerCalleeProtocol & AuthExact Endpoint / Contract
Signing invitation email (planovi-backend)planovi-microservices (agreements / dealflow)Browser GET with URL signatoryId + token/sign.php?signatoryId=...&token=...
agreements/sign.php & dealflow/sign.php (browser JS)planovi-backend (Supabase Edge Function)HTTPS POST JSON (signatory token in body, no JWT header)POST /functions/v1/signing-api

7. Why It Works This Way (Decisions, Trade-offs & Workarounds)

  • iframe.srcdoc for Agreement HTML (sign.php:455-458): Comment explicitly states Embed agreement HTML via srcdoc β€” no Content-Type issues (avoids Supabase Storage serving HTML as text/plain or triggering downloads).
  • Soft Name Matching (sign.php:401-407): Allows signing even on minor spelling/diacritic differences while signaling visual confirmation on exact match.
  • [TEAM_WHY_NEEDED: Why do identical copies exist at both agreements/sign.php and dealflow/sign.php? Which subdomain/path is canonical in production emails, and can one be removed or redirected?]
  • [TEAM_WHY_NEEDED: Does agreements/sign.php intentionally omit iframe sandbox="allow-same-origin" (or empty sandbox) on #docFrame when rendering arbitrary htmlContent via srcdoc?]

8. Debugging, Logs & Common Failure Modes

Symptom / Error MessageRoot Cause in CodeHow to Verify / Exact Log or QueryFix
"Blad polaczenia" on every linkSUPABASE_URL missing or wrapped in quotes in .env (sign_load_env keeps literal quotes, corrupting API URL at sign.php:18-19), or CORS/network block on signing-api.Inspect var API = ... in page source and check browser Console/Network tab.Strip quotes in sign_load_env() and verify SUPABASE_URL in root .env.
Both tenant logo and default Planovi hexagon icon appear in headersign.php:445-453 inserts logoImg before #brandIcon without setting #brandIcon.style.display = 'none'.Inspect #brandLogo children in DOM when data.companyLogo is returned.Hide #brandIcon (or populate existing #brandImg element at line 241 which is currently unused).
Broken footer links (planowi.app)Typos in sign.php:345, 348, 350 (https://planowi.app and https://dev-main.planowi.app/assets/for_preview/index).Click footer links.Replace planowi.app with planovi.app and production legal URLs.