# kenjidavila/rd architecture

> Architecture diagram of the GitHub repository kenjidavila/rd, generated by GitDiagram from its file tree, README and a sample of source files (last updated 2025-08-07). It is an AI-made overview: check details against the source.

- Interactive diagram: https://gitdiagram.com/kenjidavila/rd
- Repository: https://github.com/kenjidavila/rd

## Overview

##### Project type and purpose
- This is a full-stack web application: a Next.js 14 (App Router) TypeScript front-end coupled with serverless API routes and a Supabase backend.
- Purpose: complete electronic invoicing system (e-CF) for the Dominican Republic, complying with DGII regulations (XML validation, digital signatures, NCF management, real-time DGII queries), plus multi-tenant company and user management, PDF/XML generation, and audit trails.

##### File structure
- app/
- app/page.tsx, app/layout.tsx: root layout and entry page
- app/[route]/page.tsx: each UI section (dashboard, emitir, consultas, configuracion, etc.)
- app/api/**/route.ts: Next.js serverless API endpoints organized by feature (auth, comprobantes, clientes, DGII, pdf-storage, etc.)
- components/
- ui/: low-level reusable UI controls (buttons, dialogs, tables)
- auth/, dashboard/, invoices/, configuracion/, items/, pdf-storage/, system/: domain-specific React components
- layout/: header, sidebar, navigation
- lib/
- services/: business logic (base-service.ts, empresa-service, contingency-manager, pdf-generator, dgii-services)
- repositories/: data access layer for Postgres via Supabase (cliente-repository, item-repository)
- middleware/: auth-middleware for protecting API routes
- supabase-client/server-utils/auth: Supabase clients and utilities
- notification-service, monitoring-service, logger
- scripts/: SQL migrations, seeds, and deployment helper scripts
- public/: static assets (images, JSON data)
- config files: next.config.mjs, tailwind.config.ts, tsconfig.json, ESLint, Prettier

##### README for additional insights
- Next.js SSR/SSG frontend, tailwind + Radix UI + React Hook Form + Zod for validation.
- Supabase BaaS: Auth, RLS, PostgreSQL, Storage.
- External DGII webservices for RNC/NCF validation, plus official XSD schemas in libraries/.
- Workflow: front end → API routes → service layer → Supabase (DB, Auth, Storage) & DGII → back to UI with PDF/XML.

##### Diagramming conventions
- Labels: each component must have a clear name (e.g., “Next.js API Routes,” “Supabase PostgreSQL DB”).
- UI / Client: light blue rectangle
- API / Serverless: green hexagon
- External Services: gray cloud
- Security layers (JWT token flows, RLS enforcement).

## Diagram (Mermaid)

```mermaid
flowchart TD
  %% Swimlanes
  subgraph "Client (Browser)"
    direction TB
    Layout["Next.js App Router (app/layout.tsx & app/page.tsx & app/[route]/page.tsx)"]:::uiClient
    UIComponents["React Components (components/*)"]:::uiClient
  end

  subgraph "Server (Next.js)"
    direction TB
    APIRoutes["Next.js API Routes (app/api/**/route.ts)"]:::api
    AuthMiddleware["Auth Middleware (lib/middleware/auth-middleware.ts)"]:::services
    BusinessLogic["Business Logic Layer (lib/services/*.ts)"]:::services
    Repositories["Data Repositories (lib/repositories/*.ts)"]:::services
    PDFGen["PDF Generation Service (lib/pdf-generator.ts)"]:::services
    XMLGen["XML Generation & Mapper (lib/xml-generator.ts & lib/ecf-data-mapper.ts)"]:::services
    Migration["Migration & Deployment Scripts (scripts/*.sql)"]:::services
  end

  subgraph "Supabase BaaS"
    direction TB
    SupabaseAuth["Auth"]:::db
    SupabaseDB["PostgreSQL DB"]:::db
    SupabaseStorage["Storage (PDF/XML)"]:::db
  end

  subgraph "External Services"
    direction TB
    DGII["DGII Webservices & Catalogs (lib/dgii-webservices.ts)"]:::external
    XSD["E-CF XSD Schemas (libraries/e-cf-schemas/*.xsd)"]:::external
    CertAuth["Certificate Authority (lib/digital-signature.ts & lib/certificate-service.ts)"]:::external
  end

  Logging["Logging & Monitoring (lib/logger.ts & lib/monitoring-service.ts)"]:::services
  Notification["Notification Service (lib/notification-service.ts)"]:::services

  %% Connections
  Layout -->|"fetch/axios"| APIRoutes
  UIComponents -->|"fetch/axios"| APIRoutes
  APIRoutes -->|uses| AuthMiddleware
  AuthMiddleware -->|calls| BusinessLogic
  BusinessLogic -->|reads/writes| Repositories
  Repositories -->|SQL over TLS| SupabaseDB
  BusinessLogic -->|store/retrieve| SupabaseStorage
  BusinessLogic -->|invoke| DGII
  BusinessLogic -->|sign XML| CertAuth
  BusinessLogic -->|generate PDF| PDFGen
  BusinessLogic -->|generate XML| XMLGen
  BusinessLogic -->|deploy| Migration
  BusinessLogic -->|emit logs| Logging
  BusinessLogic -->|send notifications| Notification
  SupabaseAuth -->|JWT Validation| AuthMiddleware
  DGII -->|XSD schemas| XSD

  %% Click Events
  click Layout "https://github.com/kenjidavila/rd/blob/main/app/layout.tsx"
  click Layout "https://github.com/kenjidavila/rd/blob/main/app/page.tsx"
  click Layout "https://github.com/kenjidavila/rd/blob/main/app/dashboard/page.tsx"
  click APIRoutes "https://github.com/kenjidavila/rd/blob/main/app/api/comprobantes/route.ts"
  click UIComponents "https://github.com/kenjidavila/rd/tree/main/components/auth/"
  click BusinessLogic "https://github.com/kenjidavila/rd/blob/main/lib/services/base-service.ts"
  click Repositories "https://github.com/kenjidavila/rd/blob/main/lib/repositories/cliente-repository.ts"
  click AuthMiddleware "https://github.com/kenjidavila/rd/blob/main/lib/middleware/auth-middleware.ts"
  click PDFGen "https://github.com/kenjidavila/rd/blob/main/lib/pdf-generator.ts"
  click XMLGen "https://github.com/kenjidavila/rd/blob/main/lib/xml-generator.ts"
  click DGII "https://github.com/kenjidavila/rd/blob/main/lib/dgii-webservices.ts"
  click CertAuth "https://github.com/kenjidavila/rd/blob/main/lib/digital-signature.ts"
  click Notification "https://github.com/kenjidavila/rd/blob/main/lib/notification-service.ts"
  click Migration "https://github.com/kenjidavila/rd/blob/main/scripts/01-create-tables.sql"

  %% Styles
  classDef uiClient fill:#D4E6F1,stroke:#1B4F72,color:#1B2631;
  classDef api fill:#D5F5E3,stroke:#196F3D,color:#145A32;
  classDef services fill:#FCF3CF,stroke:#7D6608,color:#6E2C00;
  classDef db fill:#FDEBD0,stroke:#B9770E,color:#7E5109;
  classDef external fill:#E5E8E8,stroke:#566573,color:#2D3436;
```
