Project Overview
Sistema Financiero is built with Next.js 15 using the App Router architecture, TypeScript, and Supabase for the database. This guide will help you navigate the codebase and understand how everything fits together.Technology Stack
| Layer | Technology | Purpose |
|---|---|---|
| Framework | Next.js 15.5.4 | Full-stack React framework |
| Language | TypeScript 5 | Type-safe JavaScript |
| Database | PostgreSQL (Supabase) | Data persistence |
| Styling | Tailwind CSS 4 | Utility-first CSS |
| Charts | Chart.js + react-chartjs-2 | Data visualization |
| Icons | Lucide React | Icon library |
| AI | OpenRouter API | Multi-LLM gateway |
Directory Structure
Core Directories
/app - Application Pages
Next.js App Router uses file-based routing. Each folder represents a route:
app/page.tsx
Dashboard - Main page displaying KPIs, charts, and transaction historyKey Features:
- Fetches transactions from
/api/transacciones - Calculates totals (income, expenses, balance)
- Renders
TrendChartandDataViewscomponents - Date range filtering (daily, weekly, monthly)
app/page.tsxapp/registro/page.tsx
Manual Registration - Form-based transaction entryKey Features:
- Type selection (income/expense)
- Category dropdown
- Payment method selection
- Optional receipt photo upload
- Form validation
app/registro/page.tsxapp/agente-mejorado/page.tsx
AI Agent - Natural language transaction entryKey Features:
- Chat interface with streaming responses
- OCR for receipt scanning
- Function calling to register transactions
- Uses
useEnhancedChathook
app/agente-mejorado/page.tsx/app/api - Backend API Routes
Serverless functions that run on the server:
api/transacciones/route.ts
Purpose: Fetch and create transactions
Endpoints:
GET /api/transacciones?vista=mensual&mes=2024-03- Returns transactions for specified period
- Supports:
diaria,semanal,mensual,personalizada
POST /api/transacciones- Creates new transaction
- Validates required fields
api/chat/stream/route.ts
Purpose: AI chat with function calling
How it works:
- Receives user message
- Sends to OpenRouter (Gemini 2.5 Flash)
- AI can call
registrar_gastoorregistrar_ingresofunctions - Inserts transaction to Supabase
- Streams response back to client
- Server-Sent Events (SSE) for streaming
- Function calling for transaction registration
- Error handling and validation
api/upload-image/route.ts
Purpose: OCR for receipt scanning
Process:
- Receives image file
- Uploads to Supabase Storage
- Sends to Gemini Vision API
- Extracts: amount, category, date, description
- Returns structured data
/components - UI Components
KPICard.tsx
Purpose: Display metric cards (income, expenses, balance)
Props:
TrendChart.tsx
Purpose: Line chart showing income vs expenses over time
Dependencies:
- Chart.js
- react-chartjs-2
- Responsive design
- Dark mode support
- Customizable date grouping
- Tooltips with formatted currency
DataViews.tsx
Purpose: Transaction table with filtering and grouping
Features:
- Group by date
- Filter by type (income/expense)
- Filter by category
- Search by description
- Date range filtering
- Sort by date/amount
components/DataViews.tsx (24,645 bytes - largest component)
Header.tsx
Purpose: Top navigation bar
Features:
- Logo and app name
- Navigation links (Dashboard, Register, AI Agent, Reports)
- Theme toggle button
- Responsive mobile menu
/hooks - Custom Hooks
useEnhancedChat.ts
Purpose: Manage AI chat state and streaming
Returns:
- Handles streaming responses
- Parses Server-Sent Events
- Manages message history
- Error handling
useImageUpload.ts
Purpose: Upload images to Supabase Storage
Process:
- Validates file type (image/*)
- Generates unique filename
- Uploads to
facturasbucket - Returns public URL
/lib - Utilities
supabase.ts
Purpose: Supabase client configuration
Exports:
Data Flow Architecture
Database Schema
transacciones Table
The only table in the database:
| Column | Type | Description |
|---|---|---|
id | UUID | Primary key |
fecha | TIMESTAMP | Transaction date |
tipo | TEXT | 'ingreso' or 'gasto' |
monto | NUMERIC(10,2) | Amount (always positive) |
categoria | TEXT | Category name |
concepto | TEXT | Short description |
descripcion | TEXT | Detailed description |
metodo_pago | TEXT | Payment method |
registrado_por | TEXT | Source (manual/AI/OCR) |
foto_url | TEXT | Receipt image URL |
usuario_id | UUID | User reference |
created_at | TIMESTAMP | Record creation time |
idx_transacciones_fecha- Onfecha DESCfor fast date queriesidx_transacciones_tipo- Ontipofor filteringidx_transacciones_usuario- Onusuario_idfor RLS
Configuration Files
package.json
Key dependencies:
[email protected]- Framework[email protected]- UI library@supabase/supabase-js@^2.58.0- Database clientchart.js@^4.5.0- Charting librarylucide-react@^0.544.0- Iconsnext-themes@^0.4.6- Dark mode
tsconfig.json
TypeScript configuration with path aliases:
tailwind.config.ts
Tailwind CSS with custom theme:
Key Files Reference
| File | Lines | Purpose | When to Modify |
|---|---|---|---|
app/page.tsx | ~150 | Dashboard with KPIs | Change layout or KPI logic |
components/DataViews.tsx | 800+ | Transaction table | Add filters or columns |
app/api/chat/stream/route.ts | ~200 | AI chat endpoint | Change AI behavior |
app/api/transacciones/route.ts | ~100 | Transaction CRUD | Modify data fetching |
hooks/useEnhancedChat.ts | ~150 | Chat state management | Add chat features |
components/TrendChart.tsx | ~130 | Charts visualization | Change chart types |
lib/supabase.ts | ~10 | Database client | Change DB connection |
Environment Variables
Required Variables
Variables prefixed with
NEXT_PUBLIC_ are exposed to the browser. Keep sensitive keys without this prefix.Next Steps
Start Contributing
Learn how to make your first contribution
API Reference
Explore the API endpoints
Quick Start
Set up your development environment