Purpose

Secondary backend used for financial trend, gaps analysis, and contact distribution charts. The SPA talks to it through frontend/kundkort/services/ecoApiClient.ts via the useEcoApi hook.

Base URL

http://localhost:3100 by default. Overridable at runtime via window.__ECOAPI_URL__ (ecoApiClient.ts:2). No build-time env wiring.

Auth

Warning

The client sends a hard-coded bearer token: 'change-me-to-a-random-secret' (ecoApiClient.ts:7-9). Comment claims the dev backend accepts any bearer if FINANCIALS_API_SECRET is unset. The user JWT is sent as a separate X-Client-Token header for logging only (ecoApiClient.ts:46-48). Replace before any non-local deploy.

Functions

FunctionEndpointPurpose
fetchInsights(orgNr, token)GET ${ECO_API_URL}/api/companies/:orgNr/financialsReturns EcoApiInsights. 404 → empty default. Transforms the EcoAPI response (financials[] with fiscal_year, revenue_sek, employees) into financial_trend.{years,turnover,employees}.
fetchGaps(orgNr, token)GET /api/kundkort/:orgNr (main API)Synthesises a GapsResponse client-side by inspecting the main payload for missing finansiell_historik, kontakter, hemsida. Does NOT hit EcoAPI.
fetchFinancialTrend(orgNr, token)wraps fetchInsightsReturns { financial_trend, has_historical_data }.
fetchContactDistribution(orgNr, token)GET /api/kundkort/:orgNr (main API)Group-counts contacts by role from the main payload. Does NOT hit EcoAPI.
triggerEcoEnrichment(orgNr, token)POST /api/kundkort/:orgNr/enrich (main API)Sends { force_refresh: true }. Misnamed — does not hit EcoAPI.

Warning

Three of the five “EcoAPI” client functions actually call the main API at /api/kundkort/:orgNr. Only fetchInsights (and fetchFinancialTrend which wraps it) hits the EcoAPI host. The force_refresh flag in triggerEcoEnrichment differs from the bypass_cache flag used by useKundkort.enrich — verify which the backend honours.

Hook

hooks/useEcoApi.ts runs all four fetches in parallel via Promise.all. Each .catch(() => null), so a single failure does not block the others. Returns { insights, gaps, financialTrend, contactDistribution, loading, error, refetch, enrich }.

Consumers

Kundkort Page Analys & Grafer tab — feeds Gaps Panel, Insights Panel, Tremor Charts.

See also

Kundkort API Client, Kundkort Page.

See also