The Analytics module gives every tenant a live view of their business performance. All dashboards are tenant-isolated via Row Level Security and composed from modular, configurable widgets backed by theDocumentation Index
Fetch the complete documentation index at: https://mintlify.com/ency07/B2B-import/llms.txt
Use this file to discover all available pages before exploring further.
dashboards, dashboard_widgets, kpi_definitions, kpi_formulas, and kpi_history tables introduced in migration 20260617000015_dashboards_core.sql. KPI values are pre-computed and stored in kpi_history using versioned formulas tracked in kpi_formulas.
Data Model
The analytics system is built on six tables:| Table | Purpose |
|---|---|
kpi_definitions | Master list of KPI codes, names, categories, and units per tenant. Each KPI gets a sequential code (KPI-000001). |
kpi_formulas | Versioned SQL formula expressions for each KPI. Only one formula can be active = true per KPI at a time — the deactivate_other_kpi_formulas trigger enforces this automatically. |
kpi_history | Computed KPI values by period (e.g., '2026-06', '2026-W24'). Each (tenant_id, kpi_id, period) combination is unique; recalculation uses upsert semantics. |
dashboards | Named dashboard configurations, optionally scoped to a role_id. Each gets a sequential code (DSH-000001). |
dashboard_widgets | Individual widgets inside a dashboard. Stores widget_type, grid position (position_x, position_y, width, height), and configuration_json. |
dashboard_preferences | Per-user layout customizations for a dashboard. Stored separately from the canonical widget definition. |
KPI Calculation Engine
Thecalculate_kpi(p_tenant_id, p_kpi_code, p_period) database function handles computation and persistence in one call. It currently implements three built-in KPI codes:
| KPI Code | Description |
|---|---|
LEAD_SLA_BREACH_RATE | Percentage of leads created in the period with sla_status = 'INCUMPLIDO'. |
TOTAL_INVOICED | Sum of total_amount across invoices in statuses EMITIDA, PARCIALMENTE_PAGADA, PAGADA, VENCIDA for the period. |
TOTAL_PAYMENTS | Sum of payments with status = 'APLICADO' for the period. |
calculate_kpi upserts the result into kpi_history and emits a KPI_CALCULATED business event. Tenants can define additional KPI codes in kpi_definitions backed by custom formulas in kpi_formulas.
Dashboard Widget Types
Eachdashboard_widgets row has a widget_type that determines how it renders on the frontend. The confirmed types from the migration are:
BAR— Bar chart comparing values across categories or time periods.LINE— Line chart for trend visualization over time.KPI_CARD— Single-value metric card, typically showing the latestkpi_historyvalue with a period label.
configuration_json.
Technology
- Charts: all visualizations are built with Recharts (
recharts^3.8.1), a composable React chart library that rendersBAR,LINE, and custom widget types. - Data fetching: dashboard data is fetched via Next.js Server Actions with full tenant isolation enforced by Supabase RLS policies.
- Widget layout: each widget’s grid position (
position_x,position_y,width,height) is persisted indashboard_widgetsand can be customized per user indashboard_preferences. - Immutable history: the
block_physical_dashboard_deletetrigger prevents physical deletion of any KPI definition, formula, historical record, dashboard, widget, or preference row. Soft delete viadeleted_atis the only permitted removal path.
Exports
Excel (XLSX)
Dashboard data can be exported to an Excel workbook using the
xlsx library (^0.18.5). The export includes a formatted table per KPI category with the tenant name and date range in the header row.PDF Reports
Summary reports are exportable as PDF using
jspdf (^4.2.1) with tenant branding applied at generation time — logo, primary color, and company name are pulled from tenant_settings via getTenantBranding(). The same PDF engine is used by the Wizard to generate pre-engineering diagnostic reports.Access Control
RLS policies on all six analytics tables allow:- All authenticated users to read their tenant’s active data.
- Users with the
AUDITORrole to also query soft-deleted records. ADMINISTRADOR_TENANT,GERENTE,GERENTE_GENERALto write KPI definitions, formulas, and dashboard configurations.- Platform super-admins to access all tenants cross-tenant.