Skip to main content

Documentation 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.

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 the 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.
Use the ?tenant=<code> query parameter in your browser to switch between tenants in the demo environment. Each tenant’s dashboard loads its own KPI history, widget layout, and branding without any re-login required.

Data Model

The analytics system is built on six tables:
TablePurpose
kpi_definitionsMaster list of KPI codes, names, categories, and units per tenant. Each KPI gets a sequential code (KPI-000001).
kpi_formulasVersioned 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_historyComputed KPI values by period (e.g., '2026-06', '2026-W24'). Each (tenant_id, kpi_id, period) combination is unique; recalculation uses upsert semantics.
dashboardsNamed dashboard configurations, optionally scoped to a role_id. Each gets a sequential code (DSH-000001).
dashboard_widgetsIndividual widgets inside a dashboard. Stores widget_type, grid position (position_x, position_y, width, height), and configuration_json.
dashboard_preferencesPer-user layout customizations for a dashboard. Stored separately from the canonical widget definition.

KPI Calculation Engine

The calculate_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 CodeDescription
LEAD_SLA_BREACH_RATEPercentage of leads created in the period with sla_status = 'INCUMPLIDO'.
TOTAL_INVOICEDSum of total_amount across invoices in statuses EMITIDA, PARCIALMENTE_PAGADA, PAGADA, VENCIDA for the period.
TOTAL_PAYMENTSSum of payments with status = 'APLICADO' for the period.
After computing the value, 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

Each dashboard_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 latest kpi_history value with a period label.
Additional composite widget configurations are supported via configuration_json.

Technology

  • Charts: all visualizations are built with Recharts (recharts ^3.8.1), a composable React chart library that renders BAR, 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 in dashboard_widgets and can be customized per user in dashboard_preferences.
  • Immutable history: the block_physical_dashboard_delete trigger prevents physical deletion of any KPI definition, formula, historical record, dashboard, widget, or preference row. Soft delete via deleted_at is 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 AUDITOR role to also query soft-deleted records.
  • ADMINISTRADOR_TENANT, GERENTE, GERENTE_GENERAL to write KPI definitions, formulas, and dashboard configurations.
  • Platform super-admins to access all tenants cross-tenant.

Dashboard URL

/dashboard?tenant=<code>
The default route loads the primary dashboard for the authenticated user’s role. Navigate between dashboards using the top tabs or the sidebar shortcut links.

Build docs developers (and LLMs) love