# luiscastillocruz/sistema_cobranzas architecture

> Architecture diagram of the GitHub repository luiscastillocruz/sistema_cobranzas, generated by GitDiagram from its file tree, README and a sample of source files (last updated 2026-04-15). It is an AI-made overview: check details against the source.

- Interactive diagram: https://gitdiagram.com/luiscastillocruz/sistema_cobranzas
- Repository: https://github.com/luiscastillocruz/sistema_cobranzas

## Overview

1. This repository is a monorepo with three distinct layers: a React/Vite single-page frontend, a primary FastAPI backend in Python, and a smaller TypeScript/Node backend under `backend/src` that appears focused on authentication and email-settings operations. The FastAPI app is the main business backend.

2. The frontend is organized as a routed SPA with auth protection. `AuthProvider` and `ProtectedRoute` wrap the app, `MainLayout` provides the shell, and feature pages are split by domain: Cobros/accounts receivable, Clientes, Dashboard, Vendedores, Login, and email POP3 settings. Each page has its own `api.ts`, suggesting a page-centric API boundary.

3. The FastAPI backend entrypoint is `app/main.py`, with routing centralized in `app/api/router.py`. Request middleware and cross-cutting concerns live in `app/middlewares`, while `app/core` holds configuration, security, normalization, dependency injection, logging, and crypto-vault helpers.

4. The core domain model is centered on collections and customer/account management. SQLAlchemy models include `account_receivable`, `client`, `vendedores`, `user`, `email_settings`, `email_template`, and logging tables for email/statement sends. Pydantic schemas mirror these domains for API input/output.

5. The backend is layered in the usual FastAPI style: routes call `crud` and `services`, which then access SQLAlchemy models through the DB session layer in `app/db`. `app/crud/accounts_receivable.py` and service modules like `dashboard_service.py`, `reminder_engine.py`, and `mailer.py` indicate a separation between data access, orchestration, and integration logic.

6. The main data flows are:
   - Browser -> SPA route/page -> page API module -> backend route.
   - Backend route -> dependency/auth checks -> CRUD/service -> SQLAlchemy model -> SQLite DB.
   - Dashboard pages consume aggregated metrics from `dashboard_service`.
   - Import/export flows likely move bulk receivable data between Excel files and the DB.

7. Reminder and notification delivery are a key subsystem. `reminder_scheduler.py` and `reminder_engine.py` suggest periodic processing of overdue or due receivables, while `email_recipients.py`, `smtp_sender.py`, and `mailer.py` handle recipient resolution and outbound email. The logging models imply every send is tracked for auditability.

8. Email configuration is treated as its own bounded area. The FastAPI side stores email settings/templates and logs, while the TypeScript backend under `backend/src` contains `emailSettings.routes.ts`, `emailSettings.service.ts`, plus SMTP/POP3 test services and validators. That separation suggests a dedicated service or a partial migration for mail configuration workflows.

9. The TypeScript backend uses an Express-style stack: `app.ts`, `index.ts`, middleware for auth/error handling/rate limiting, Prisma schemas under `backend/prisma`, and a `cryptoVault` helper. It likely serves admin-style endpoints around email settings and connection tests rather than the full receivables domain.

10. Authentication and security are handled in both backend stacks. The Python app has `security.py`, `deps.py`, and an auth route plus `user` model; the TS service has its own auth middleware and rate limiter. The frontend’s auth provider and protected routes imply token-based session handling across the API boundary.

11. Persistence defaults to SQLite via `backend/cobranzas.db`, with a note in the README that PostgreSQL can replace it in production. The presence of both SQLAlchemy and Prisma means the repo currently mixes ORM/data layers across the Python and TypeScript services.

12. Operationally, the repository is designed for local development with Vite on the frontend and Uvicorn on the FastAPI backend, and for deployment behind Nginx/systemd on Ubuntu. Helper scripts like `create_user.py`, `reset_pass.py`, and `seed_admin.py` indicate bootstrap and admin lifecycle support.

Main entry points: [`frontend/src/app/App.tsx`](https://github.com/luiscastillocruz/sistema_cobranzas/blob/main/frontend/src/app/App.tsx) (App shell), [`backend/app/api/routes/accounts_receivable.py`](https://github.com/luiscastillocruz/sistema_cobranzas/blob/main/backend/app/api/routes/accounts_receivable.py) (Domain routes), [`backend/app/crud/accounts_receivable.py`](https://github.com/luiscastillocruz/sistema_cobranzas/blob/main/backend/app/crud/accounts_receivable.py) (Receivables domain), [`backend/app/services/dashboard_service.py`](https://github.com/luiscastillocruz/sistema_cobranzas/blob/main/backend/app/services/dashboard_service.py) (Workers)

## Diagram (Mermaid)

```mermaid
flowchart TD

subgraph group_frontend["Frontend SPA"]
  node_frontend_app["App shell<br/>react spa<br/>[App.tsx]"]
  node_frontend_auth["Auth guard<br/>auth ui<br/>[AuthProvider.tsx]"]
  node_frontend_layout["Main layout<br/>shell<br/>[MainLayout.tsx]"]
  node_frontend_cobros["Cobros<br/>feature page<br/>[index.tsx]"]
  node_frontend_clientes["Clientes<br/>feature page<br/>[Clientes.tsx]"]
  node_frontend_dashboard["Dashboard<br/>feature page<br/>[Dashboard.tsx]"]
  node_frontend_login["Login<br/>auth page<br/>[Login.tsx]"]
  node_frontend_email_pop3["Email POP3<br/>settings page"]
end

subgraph group_python_backend["FastAPI Backend"]
  node_python_main["API app<br/>[main.py]"]
  node_python_router["API router<br/>routes hub<br/>[router.py]"]
  node_python_core["Core services<br/>platform core<br/>[config.py]"]
  node_python_db[("DB layer<br/>sqlalchemy db<br/>[session.py]")]
  node_python_receivables["Receivables domain<br/>crud/service"]
  node_python_models["Domain models<br/>orm models"]
  node_python_schemas["API schemas<br/>pydantic schemas"]
  node_python_routes["Domain routes<br/>api routes"]
  node_python_services["Workers<br/>service layer"]
end

subgraph group_ts_backend["Node Email Admin"]
  node_ts_app["Node app<br/>express app<br/>[app.ts]"]
  node_ts_routes["Email routes<br/>api routes"]
  node_ts_services["Email services<br/>service layer"]
  node_ts_security["Node security<br/>middleware/crypto<br/>[auth.ts]"]
end

node_frontend_auth -->|"wraps"| node_frontend_app
node_frontend_app -->|"uses"| node_frontend_layout
node_frontend_app -->|"routes to"| node_frontend_login
node_frontend_app -->|"routes to"| node_frontend_cobros
node_frontend_app -->|"routes to"| node_frontend_clientes
node_frontend_app -->|"routes to"| node_frontend_dashboard
node_frontend_app -->|"routes to"| node_frontend_email_pop3
node_frontend_cobros -->|"calls API"| node_python_routes
node_frontend_clientes -->|"calls API"| node_python_routes
node_frontend_dashboard -->|"reads metrics"| node_python_services
node_frontend_email_pop3 -->|"calls API"| node_ts_routes
node_python_main -->|"mounts"| node_python_router
node_python_router -->|"dispatches"| node_python_routes
node_python_routes -->|"depends on"| node_python_core
node_python_routes -->|"orchestrates"| node_python_receivables
node_python_receivables -->|"persists via"| node_python_db
node_python_db -->|"maps"| node_python_models
node_python_receivables -->|"uses"| node_python_models
node_python_receivables -->|"shapes data"| node_python_schemas
node_python_services -->|"reads/writes"| node_python_models
node_python_services -->|"queries via"| node_python_db
node_ts_app -->|"uses"| node_ts_security
node_ts_app -->|"mounts"| node_ts_routes
node_ts_routes -->|"delegates"| node_ts_services

click node_frontend_app "https://github.com/luiscastillocruz/sistema_cobranzas/blob/main/frontend/src/app/App.tsx"
click node_frontend_auth "https://github.com/luiscastillocruz/sistema_cobranzas/blob/main/frontend/src/auth/AuthProvider.tsx"
click node_frontend_layout "https://github.com/luiscastillocruz/sistema_cobranzas/blob/main/frontend/src/components/layout/MainLayout.tsx"
click node_frontend_cobros "https://github.com/luiscastillocruz/sistema_cobranzas/blob/main/frontend/src/pages/Cobros/index.tsx"
click node_frontend_clientes "https://github.com/luiscastillocruz/sistema_cobranzas/blob/main/frontend/src/pages/Clientes/Clientes.tsx"
click node_frontend_dashboard "https://github.com/luiscastillocruz/sistema_cobranzas/blob/main/frontend/src/pages/Dasboard/Dashboard.tsx"
click node_frontend_login "https://github.com/luiscastillocruz/sistema_cobranzas/blob/main/frontend/src/pages/Login.tsx"
click node_frontend_email_pop3 "https://github.com/luiscastillocruz/sistema_cobranzas/blob/main/frontend/src/pages/EmailSettingsPop3.tsx"
click node_python_main "https://github.com/luiscastillocruz/sistema_cobranzas/blob/main/backend/app/main.py"
click node_python_router "https://github.com/luiscastillocruz/sistema_cobranzas/blob/main/backend/app/api/router.py"
click node_python_core "https://github.com/luiscastillocruz/sistema_cobranzas/blob/main/backend/app/core/config.py"
click node_python_db "https://github.com/luiscastillocruz/sistema_cobranzas/blob/main/backend/app/db/session.py"
click node_python_receivables "https://github.com/luiscastillocruz/sistema_cobranzas/blob/main/backend/app/crud/accounts_receivable.py"
click node_python_models "https://github.com/luiscastillocruz/sistema_cobranzas/blob/main/backend/app/models/account_receivable.py"
click node_python_schemas "https://github.com/luiscastillocruz/sistema_cobranzas/blob/main/backend/app/schemas/accounts_receivable.py"
click node_python_routes "https://github.com/luiscastillocruz/sistema_cobranzas/blob/main/backend/app/api/routes/accounts_receivable.py"
click node_python_services "https://github.com/luiscastillocruz/sistema_cobranzas/blob/main/backend/app/services/dashboard_service.py"
click node_ts_app "https://github.com/luiscastillocruz/sistema_cobranzas/blob/main/backend/src/app.ts"
click node_ts_routes "https://github.com/luiscastillocruz/sistema_cobranzas/blob/main/backend/src/routes/emailSettings.routes.ts"
click node_ts_services "https://github.com/luiscastillocruz/sistema_cobranzas/blob/main/backend/src/services/emailSettings.service.ts"
click node_ts_security "https://github.com/luiscastillocruz/sistema_cobranzas/blob/main/backend/src/middlewares/auth.ts"

classDef toneNeutral fill:#f8fafc,stroke:#334155,stroke-width:1.5px,color:#0f172a
classDef toneBlue fill:#dbeafe,stroke:#2563eb,stroke-width:1.5px,color:#172554
classDef toneAmber fill:#fef3c7,stroke:#d97706,stroke-width:1.5px,color:#78350f
classDef toneMint fill:#dcfce7,stroke:#16a34a,stroke-width:1.5px,color:#14532d
classDef toneRose fill:#ffe4e6,stroke:#e11d48,stroke-width:1.5px,color:#881337
classDef toneIndigo fill:#e0e7ff,stroke:#4f46e5,stroke-width:1.5px,color:#312e81
classDef toneTeal fill:#ccfbf1,stroke:#0f766e,stroke-width:1.5px,color:#134e4a
class node_frontend_app,node_frontend_auth,node_frontend_layout,node_frontend_cobros,node_frontend_clientes,node_frontend_dashboard,node_frontend_login,node_frontend_email_pop3 toneBlue
class node_python_main,node_python_router,node_python_core,node_python_db,node_python_receivables,node_python_models,node_python_schemas,node_python_routes,node_python_services toneAmber
class node_ts_app,node_ts_routes,node_ts_services,node_ts_security toneMint
```

## Components

### Frontend SPA

React/Vite single-page app with routed, auth-protected feature pages.

- [App shell](https://github.com/luiscastillocruz/sistema_cobranzas/blob/main/frontend/src/app/App.tsx): react spa · Root app composition, routing, auth provider, and protected layout. · `frontend/src/app/App.tsx`
- [Auth guard](https://github.com/luiscastillocruz/sistema_cobranzas/blob/main/frontend/src/auth/AuthProvider.tsx): auth ui · Session provider and protected route gate for the SPA. · `frontend/src/auth/AuthProvider.tsx`
- [Main layout](https://github.com/luiscastillocruz/sistema_cobranzas/blob/main/frontend/src/components/layout/MainLayout.tsx): shell · Shared application chrome and navigation wrapper. · `frontend/src/components/layout/MainLayout.tsx`
- [Cobros](https://github.com/luiscastillocruz/sistema_cobranzas/blob/main/frontend/src/pages/Cobros/index.tsx): feature page · Receivables list, dialogs, bulk import/export, and page-specific API layer. · `frontend/src/pages/Cobros/index.tsx`
- [Clientes](https://github.com/luiscastillocruz/sistema_cobranzas/blob/main/frontend/src/pages/Clientes/Clientes.tsx): feature page · Customer management page with its own API and utilities. · `frontend/src/pages/Clientes/Clientes.tsx`
- [Dashboard](https://github.com/luiscastillocruz/sistema_cobranzas/blob/main/frontend/src/pages/Dasboard/Dashboard.tsx): feature page · Metrics and drilldowns for receivables operations. · `frontend/src/pages/Dasboard/Dashboard.tsx`
- [Login](https://github.com/luiscastillocruz/sistema_cobranzas/blob/main/frontend/src/pages/Login.tsx): auth page · Authentication entry page for the SPA. · `frontend/src/pages/Login.tsx`
- [Email POP3](https://github.com/luiscastillocruz/sistema_cobranzas/blob/main/frontend/src/pages/EmailSettingsPop3.tsx): settings page · Frontend page for email POP3 settings management. · `frontend/src/pages/EmailSettingsPop3.tsx`

### FastAPI Backend

Primary business API, domain logic, and persistence layer.

- [API app](https://github.com/luiscastillocruz/sistema_cobranzas/blob/main/backend/app/main.py): fastapi app · FastAPI application entrypoint and startup composition. · `backend/app/main.py`
- [API router](https://github.com/luiscastillocruz/sistema_cobranzas/blob/main/backend/app/api/router.py): routes hub · Central router that mounts domain endpoints. · `backend/app/api/router.py`
- [Core services](https://github.com/luiscastillocruz/sistema_cobranzas/blob/main/backend/app/core/config.py): platform core · Configuration, security, dependencies, logging, normalization, and vault helpers. · `backend/app/core/config.py`
- [DB layer](https://github.com/luiscastillocruz/sistema_cobranzas/blob/main/backend/app/db/session.py): sqlalchemy db · Session and base wiring for the SQLite-backed persistence layer. · `backend/app/db/session.py`
- [Receivables domain](https://github.com/luiscastillocruz/sistema_cobranzas/blob/main/backend/app/crud/accounts_receivable.py): crud/service · Receivables CRUD plus reminder, dashboard, mail, and vendedores orchestration. · `backend/app/crud/accounts_receivable.py`
- [Domain models](https://github.com/luiscastillocruz/sistema_cobranzas/blob/main/backend/app/models/account_receivable.py): orm models · SQLAlchemy models for receivables, clients, users, vendors, email settings, templates, and logs. · `backend/app/models/account_receivable.py`
- [API schemas](https://github.com/luiscastillocruz/sistema_cobranzas/blob/main/backend/app/schemas/accounts_receivable.py): pydantic schemas · Request/response models for auth, clients, dashboard, receivables, vendors, and email settings. · `backend/app/schemas/accounts_receivable.py`
- [Domain routes](https://github.com/luiscastillocruz/sistema_cobranzas/blob/main/backend/app/api/routes/accounts_receivable.py): api routes · Feature endpoints for auth, receivables, clients, dashboard, imports, exports, reminders, vendors, and email settings. · `backend/app/api/routes/accounts_receivable.py`
- [Workers](https://github.com/luiscastillocruz/sistema_cobranzas/blob/main/backend/app/services/dashboard_service.py): service layer · Dashboard aggregation, recipient selection, mail delivery, reminder scheduling, SMTP integration, and vendor logic. · `backend/app/services/dashboard_service.py`

### Node Email Admin

Smaller Express-style backend for auth and email-settings workflows.

- [Node app](https://github.com/luiscastillocruz/sistema_cobranzas/blob/main/backend/src/app.ts): express app · Express-style entrypoint and server bootstrap for the secondary backend. · `backend/src/app.ts`
- [Email routes](https://github.com/luiscastillocruz/sistema_cobranzas/blob/main/backend/src/routes/emailSettings.routes.ts): api routes · Routes for auth and email-settings operations. · `backend/src/routes/emailSettings.routes.ts`
- [Email services](https://github.com/luiscastillocruz/sistema_cobranzas/blob/main/backend/src/services/emailSettings.service.ts): service layer · Email settings CRUD plus SMTP and POP3 connection tests. · `backend/src/services/emailSettings.service.ts`
- [Node security](https://github.com/luiscastillocruz/sistema_cobranzas/blob/main/backend/src/middlewares/auth.ts): middleware/crypto · Auth middleware, rate limiting, error handling, and crypto vault helper. · `backend/src/middlewares/auth.ts`

## Connections

- Auth guard → App shell: wraps · Authentication context and protected routing guard the app.
- App shell → Main layout: uses · The router renders pages inside the shared layout shell.
- App shell → Login: routes to · Unauthenticated users land on the login page.
- App shell → Cobros: routes to · Receivables feature page.
- App shell → Clientes: routes to · Customer management page.
- App shell → Dashboard: routes to · Dashboard page.
- App shell → Email POP3: routes to · Email settings page.
- Cobros → Domain routes: calls API · Page-specific API module talks to the backend routes.
- Clientes → Domain routes: calls API · Client page uses the FastAPI domain endpoints.
- Dashboard → Workers: reads metrics · Dashboard requests aggregated operational metrics.
- Email POP3 → Email routes: calls API · Email settings page targets the Node backend for settings workflows.
- API app → API router: mounts · Application startup wires the central API router.
- API router → Domain routes: dispatches · Router exposes the domain route modules.
- Domain routes → Core services: depends on · Routes use security, config, and dependency helpers.
- Domain routes → Receivables domain: orchestrates · Endpoints delegate business work to CRUD and services.
- Receivables domain → DB layer: persists via · Data access runs through the DB session layer.
- DB layer → Domain models: maps · SQLAlchemy models define the persisted tables.
- Receivables domain → Domain models: uses · Domain logic operates on receivables and related entities.
- Receivables domain → API schemas: shapes data · Services and routes exchange typed API payloads.
- Workers → Domain models: reads/writes · Dashboard, reminder, and mail services use domain records and logs.
- Workers → DB layer: queries via · Services access persistence through the DB layer.
- Node app → Node security: uses · Server bootstrap installs middleware and security helpers.
- Node app → Email routes: mounts · Express app registers the email/admin routes.
- Email routes → Email services: delegates · Route handlers forward to service implementations.
