# rsp-byte/frutosparaiso architecture

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

- Interactive diagram: https://gitdiagram.com/rsp-byte/frutosparaiso
- Repository: https://github.com/rsp-byte/frutosparaiso

## Overview

1. **High-level shape**
   - This repository is a **single Spring Boot 3 backend** written in **Java 21**, built with **Gradle**.
   - It is organized as a layered application: **HTTP controllers → services → repositories/entities**, with several outbound integrations for authentication, messaging, AI, Redis, and an external billing service.

2. **Primary architectural boundary**
   - The main boundary is the **REST API layer** under `controller/`.
   - Controllers expose endpoints for **auth**, **chat**, **products**, **orders**, **items**, **Factus**, and a **WhatsApp webhook**.
   - Business logic is pushed into `service/` and `service/impl/`, while persistence is isolated in `repository/`.

3. **Domain and persistence model**
   - The app uses **Spring Data JPA** with entities such as `User`, `ProfileEntity`, `ProductEntity`, `OrderEntity`, `Bill`, `DetailBill`, `Customer`, `RefreshToken`, and `ItemFPEntity`.
   - Repositories like `UserRepository`, `ProductRepository`, `OrderRepository`, `BillRepository`, and `RefreshTokenRepository` indicate a relational data model centered on users, commerce/order data, and token storage.
   - `src/main/resources/SQL_SC/FRUTOS_PARAISO_INSERT_PRODUCT.sql` suggests the app can be bootstrapped with seed product data.

4. **Authentication and session management**
   - Authentication is handled by `AuthController`, `AuthService`, `JwtService`, and `JwtFilter`, with `SecurityConfig` and `CustomUserDetailsService` forming the Spring Security boundary.
   - JWT-related configuration lives in `JWTProperties`.
   - Refresh and transient token state is split between the database (`RefreshTokenRepository`) and Redis-backed cache services (`TokenCacheService`, `RedisService`), which implies short-lived/session-like state outside the main JPA store.

5. **Commerce workflows**
   - `ProductController`/`ProductService` manage catalog operations.
   - `OrderController`/`OrderService` manage order lifecycle and likely assemble products/items into orders.
   - `ItemController` and `ItemFPService` suggest a secondary item domain, likely tied to “Frutos Paraíso” business-specific products/items.
   - These workflows are data-centric and persist through JPA repositories, with DTOs used to avoid exposing entities directly.

6. **Billing / external invoicing integration**
   - `FactusController` and `FactusService` indicate an integration with an external **Factus** service.
   - `FactusProperties` and entities like `Factus`, `Bill`, `DetailBill`, `Customer`, and `Establishment` suggest the service handles invoice/billing payloads and maps internal order data into an external billing format.
   - `ExternalServiceException` implies outbound integration failures are treated as a distinct error class.

7. **Chat, AI, and WhatsApp integration**
   - `ChatController`, `ChatEventBus`, `OpenAiService`, `AgentMcpService`, `AgentPrompt`, `WhatsAppService`, and `WhatsappTwiloWebHook` form a separate conversational subsystem.
   - The likely flow is: **incoming chat/WhatsApp message → webhook/controller → internal event bus/service orchestration → AI agent/OpenAI → reply back through WhatsApp**.
   - `TwilioConfig` and `TwilioInitializer` show WhatsApp messaging is mediated through **Twilio**.
   - `AiConfig` and `WebClientConfig` suggest outbound AI calls are done over HTTP using Spring’s reactive client.

8. **Redis as a supporting infrastructure component**
   - Redis is a first-class infrastructure dependency, configured in `RedisConfig` and monitored by `RedisHealthCheck`.
   - `RedisService` and `TokenCacheService` indicate Redis is used for **fast transient state**, most visibly token/session caching and possibly chat context or other ephemeral coordination data.
   - This creates a clear separation between durable data in the relational database and short-lived operational data in Redis.

9. **API contract and mapping layer**
   - The `dto/` package is extensive: auth requests, refresh/token payloads, order/product/user/profile data, chat context, and AI response/request models.
   - This indicates the API does not expose JPA entities directly; instead it uses DTOs as the contract boundary for clients and external services.
   - The presence of both `entity/TokenResponse` and `dto/TokenResponse` suggests separate shapes for persistence/internal use versus API response use.

10. **Error handling and resilience**
    - `GlobalExceptionHandler` centralizes API error translation.
    - `ApiException` is used for application-level failures, while `ExternalServiceException` captures integration problems.
    - Supporting types like `ErrorResponse` and `ErrorCode` show the service returns structured error payloads rather than raw stack traces.

11. **Configuration and cross-cutting concerns**
    - `CorsConfig` handles cross-origin access for client applications.
    - `WebClientConfig` supports outbound HTTP integration.
    - `FactusProperties`, `JWTProperties`, `TwilioConfig`, and `RedisConfig` externalize runtime configuration, making the app environment-driven.
    - `ChatEventBus` is a notable internal coordination mechanism for decoupling chat message ingestion from response generation.

12. **Runtime characteristics**
    - The application is a **server-side monolith** with multiple integration points, not a distributed set of services.
    - Its core runtime dependencies are:
      - a relational database via JPA,
      - Redis for caching/transient state,
      - Twilio for WhatsApp delivery,
      - an AI provider via HTTP,
      - and a billing/invoicing service via HTTP.

13. **Testing and entry point**
    - `FrutosparaisoApplication.java` is the Spring Boot entry point.
    - `FrutosparaisoApplicationTests` suggests at least a basic context-load test exists.
    - Overall, the repository is structured for a conventional layered backend with several business domains sharing the same runtime and deployment boundary.

Main entry points: [`src/main/java/com/plataforma/frutosparaiso/FrutosparaisoApplication.java`](https://github.com/rsp-byte/frutosparaiso/blob/master/src/main/java/com/plataforma/frutosparaiso/FrutosparaisoApplication.java) (App entry), [`src/main/java/com/plataforma/frutosparaiso/controller/ProductController.java`](https://github.com/rsp-byte/frutosparaiso/blob/master/src/main/java/com/plataforma/frutosparaiso/controller/ProductController.java) (Commerce API), [`src/main/java/com/plataforma/frutosparaiso/controller/AuthController.java`](https://github.com/rsp-byte/frutosparaiso/blob/master/src/main/java/com/plataforma/frutosparaiso/controller/AuthController.java) (Auth API), [`src/main/java/com/plataforma/frutosparaiso/service/AuthService.java`](https://github.com/rsp-byte/frutosparaiso/blob/master/src/main/java/com/plataforma/frutosparaiso/service/AuthService.java) (Auth service)

## Diagram (Mermaid)

```mermaid
flowchart TD

subgraph group_g1["API layer"]
  node_n1(("App entry<br/>spring boot"))
  node_n2["Auth API<br/>controller"]
  node_n3["Commerce API<br/>controllers"]
  node_n4["Chat API<br/>controller"]
  node_n5["Webhook"]
end

subgraph group_g2["Core services"]
  node_n6{{"Auth service<br/>[AuthService.java]"}}
  node_n7{{"Catalog service"}}
  node_n8{{"Order service<br/>[OrderService.java]"}}
  node_n9{{"Billing service<br/>integration service<br/>[FactusService.java]"}}
  node_n10{{"Chat orchestrator"}}
  node_n11{{"WhatsApp service<br/>integration service"}}
  node_n12{{"OpenAI service<br/>integration service<br/>[OpenAiService.java]"}}
end

subgraph group_g3["Data model"]
  node_n13[("User data<br/>entity/repository")]
  node_n14[("Commerce data<br/>entity/repository")]
end

subgraph group_g4["Infrastructure"]
  node_n15["Auth cache<br/>redis-backed state"]
  node_n16["Security core<br/>spring security"]
  node_n17[("Redis infra<br/>cache/config<br/>[RedisConfig.java]")]
  node_n18["External adapters<br/>config/adapters"]
end

node_n1 -->|"exposes"| node_n2
node_n1 -->|"exposes"| node_n3
node_n1 -->|"exposes"| node_n4
node_n1 -->|"exposes"| node_n5
node_n2 -->|"uses"| node_n6
node_n2 -->|"secures"| node_n16
node_n3 -->|"uses"| node_n7
node_n3 -->|"uses"| node_n8
node_n3 -->|"uses"| node_n9
node_n4 -->|"uses"| node_n10
node_n5 -->|"routes to"| node_n11
node_n6 -->|"persists"| node_n13
node_n6 -->|"caches"| node_n15
node_n7 -->|"persists"| node_n14
node_n8 -->|"persists"| node_n14
node_n9 -->|"reads"| node_n14
node_n10 -->|"calls"| node_n12
node_n10 -->|"replies via"| node_n11
node_n11 -->|"depends on"| node_n18
node_n12 -->|"depends on"| node_n18
node_n9 -->|"depends on"| node_n18
node_n16 -->|"integrates"| node_n15
node_n15 -->|"backed by"| node_n17

click node_n1 "https://github.com/rsp-byte/frutosparaiso/blob/master/src/main/java/com/plataforma/frutosparaiso/FrutosparaisoApplication.java"
click node_n2 "https://github.com/rsp-byte/frutosparaiso/blob/master/src/main/java/com/plataforma/frutosparaiso/controller/AuthController.java"
click node_n3 "https://github.com/rsp-byte/frutosparaiso/blob/master/src/main/java/com/plataforma/frutosparaiso/controller/ProductController.java"
click node_n4 "https://github.com/rsp-byte/frutosparaiso/blob/master/src/main/java/com/plataforma/frutosparaiso/controller/ChatController.java"
click node_n5 "https://github.com/rsp-byte/frutosparaiso/blob/master/src/main/java/com/plataforma/frutosparaiso/controller/WhatsappTwiloWebHook.java"
click node_n6 "https://github.com/rsp-byte/frutosparaiso/blob/master/src/main/java/com/plataforma/frutosparaiso/service/AuthService.java"
click node_n7 "https://github.com/rsp-byte/frutosparaiso/blob/master/src/main/java/com/plataforma/frutosparaiso/service/impl/ProductService.java"
click node_n8 "https://github.com/rsp-byte/frutosparaiso/blob/master/src/main/java/com/plataforma/frutosparaiso/service/impl/OrderService.java"
click node_n9 "https://github.com/rsp-byte/frutosparaiso/blob/master/src/main/java/com/plataforma/frutosparaiso/service/FactusService.java"
click node_n10 "https://github.com/rsp-byte/frutosparaiso/blob/master/src/main/java/com/plataforma/frutosparaiso/service/impl/AgentMcpService.java"
click node_n11 "https://github.com/rsp-byte/frutosparaiso/blob/master/src/main/java/com/plataforma/frutosparaiso/service/impl/WhatsAppService.java"
click node_n12 "https://github.com/rsp-byte/frutosparaiso/blob/master/src/main/java/com/plataforma/frutosparaiso/service/impl/OpenAiService.java"
click node_n13 "https://github.com/rsp-byte/frutosparaiso/blob/master/src/main/java/com/plataforma/frutosparaiso/repository/UserRepository.java"
click node_n14 "https://github.com/rsp-byte/frutosparaiso/blob/master/src/main/java/com/plataforma/frutosparaiso/repository/ProductRepository.java"
click node_n15 "https://github.com/rsp-byte/frutosparaiso/blob/master/src/main/java/com/plataforma/frutosparaiso/service/TokenCacheService.java"
click node_n16 "https://github.com/rsp-byte/frutosparaiso/blob/master/src/main/java/com/plataforma/frutosparaiso/security/SecurityConfig.java"
click node_n17 "https://github.com/rsp-byte/frutosparaiso/blob/master/src/main/java/com/plataforma/frutosparaiso/config/RedisConfig.java"
click node_n18 "https://github.com/rsp-byte/frutosparaiso/blob/master/src/main/java/com/plataforma/frutosparaiso/config/WebClientConfig.java"

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_n1,node_n2,node_n3,node_n4,node_n5 toneBlue
class node_n6,node_n7,node_n8,node_n9,node_n10,node_n11,node_n12 toneAmber
class node_n13,node_n14 toneMint
class node_n15,node_n16,node_n17,node_n18 toneRose
```

## Components

### API layer

REST endpoints and webhook ingress

- [App entry](https://github.com/rsp-byte/frutosparaiso/blob/master/src/main/java/com/plataforma/frutosparaiso/FrutosparaisoApplication.java): spring boot · Application bootstrap · `src/main/java/com/plataforma/frutosparaiso/FrutosparaisoApplication.java`
- [Auth API](https://github.com/rsp-byte/frutosparaiso/blob/master/src/main/java/com/plataforma/frutosparaiso/controller/AuthController.java): controller · Login, refresh, password and session endpoints · `src/main/java/com/plataforma/frutosparaiso/controller/AuthController.java`
- [Commerce API](https://github.com/rsp-byte/frutosparaiso/blob/master/src/main/java/com/plataforma/frutosparaiso/controller/ProductController.java): controllers · Catalog, orders, items, and billing endpoints · `src/main/java/com/plataforma/frutosparaiso/controller/ProductController.java`
- [Chat API](https://github.com/rsp-byte/frutosparaiso/blob/master/src/main/java/com/plataforma/frutosparaiso/controller/ChatController.java): controller · Chat ingress and response endpoints · `src/main/java/com/plataforma/frutosparaiso/controller/ChatController.java`
- [Webhook](https://github.com/rsp-byte/frutosparaiso/blob/master/src/main/java/com/plataforma/frutosparaiso/controller/WhatsappTwiloWebHook.java): twilio webhook · WhatsApp callback endpoint · `src/main/java/com/plataforma/frutosparaiso/controller/WhatsappTwiloWebHook.java`

### Core services

Business workflows and orchestration

- [Auth service](https://github.com/rsp-byte/frutosparaiso/blob/master/src/main/java/com/plataforma/frutosparaiso/service/AuthService.java): service · Authentication and token lifecycle · `src/main/java/com/plataforma/frutosparaiso/service/AuthService.java`
- [Catalog service](https://github.com/rsp-byte/frutosparaiso/blob/master/src/main/java/com/plataforma/frutosparaiso/service/impl/ProductService.java): service · Product and item workflows · `src/main/java/com/plataforma/frutosparaiso/service/impl/ProductService.java`
- [Order service](https://github.com/rsp-byte/frutosparaiso/blob/master/src/main/java/com/plataforma/frutosparaiso/service/impl/OrderService.java): service · Order lifecycle and assembly · `src/main/java/com/plataforma/frutosparaiso/service/impl/OrderService.java`
- [Billing service](https://github.com/rsp-byte/frutosparaiso/blob/master/src/main/java/com/plataforma/frutosparaiso/service/FactusService.java): integration service · Factus invoice generation and submission · `src/main/java/com/plataforma/frutosparaiso/service/FactusService.java`
- [Chat orchestrator](https://github.com/rsp-byte/frutosparaiso/blob/master/src/main/java/com/plataforma/frutosparaiso/service/impl/AgentMcpService.java): service · Agent coordination and message handling · `src/main/java/com/plataforma/frutosparaiso/service/impl/AgentMcpService.java`
- [WhatsApp service](https://github.com/rsp-byte/frutosparaiso/blob/master/src/main/java/com/plataforma/frutosparaiso/service/impl/WhatsAppService.java): integration service · Outbound WhatsApp messaging via Twilio · `src/main/java/com/plataforma/frutosparaiso/service/impl/WhatsAppService.java`
- [OpenAI service](https://github.com/rsp-byte/frutosparaiso/blob/master/src/main/java/com/plataforma/frutosparaiso/service/impl/OpenAiService.java): integration service · LLM calls over HTTP · `src/main/java/com/plataforma/frutosparaiso/service/impl/OpenAiService.java`

### Data model

JPA entities and repositories

- [User data](https://github.com/rsp-byte/frutosparaiso/blob/master/src/main/java/com/plataforma/frutosparaiso/repository/UserRepository.java): entity/repository · Users, profiles, and refresh tokens · `src/main/java/com/plataforma/frutosparaiso/repository/UserRepository.java`
- [Commerce data](https://github.com/rsp-byte/frutosparaiso/blob/master/src/main/java/com/plataforma/frutosparaiso/repository/ProductRepository.java): entity/repository · Products, orders, items, and bills · `src/main/java/com/plataforma/frutosparaiso/repository/ProductRepository.java`

### Infrastructure

Security, caching, configuration, and outbound adapters

- [Auth cache](https://github.com/rsp-byte/frutosparaiso/blob/master/src/main/java/com/plataforma/frutosparaiso/service/TokenCacheService.java): redis-backed state · Token and transient session storage · `src/main/java/com/plataforma/frutosparaiso/service/TokenCacheService.java`
- [Security core](https://github.com/rsp-byte/frutosparaiso/blob/master/src/main/java/com/plataforma/frutosparaiso/security/SecurityConfig.java): spring security · JWT filter, user details, and config · `src/main/java/com/plataforma/frutosparaiso/security/SecurityConfig.java`
- [Redis infra](https://github.com/rsp-byte/frutosparaiso/blob/master/src/main/java/com/plataforma/frutosparaiso/config/RedisConfig.java): cache/config · Redis configuration and health check · `src/main/java/com/plataforma/frutosparaiso/config/RedisConfig.java`
- [External adapters](https://github.com/rsp-byte/frutosparaiso/blob/master/src/main/java/com/plataforma/frutosparaiso/config/WebClientConfig.java): config/adapters · Twilio, WebClient, AI, Factus, CORS, and event bus support · `src/main/java/com/plataforma/frutosparaiso/config/WebClientConfig.java`

## Connections

- App entry → Auth API: exposes · Application starts the REST API stack
- App entry → Commerce API: exposes · Application starts commerce endpoints
- App entry → Chat API: exposes · Application starts chat endpoints
- App entry → Webhook: exposes · Application starts webhook ingress
- Auth API → Auth service: uses · Authentication controller delegates to auth services
- Auth API → Security core: secures · Auth endpoints participate in security boundary
- Commerce API → Catalog service: uses · Catalog endpoints delegate to product/item services
- Commerce API → Order service: uses · Order endpoints delegate to order workflow
- Commerce API → Billing service: uses · Billing endpoints delegate to Factus integration
- Chat API → Chat orchestrator: uses · Chat controller orchestrates agent flow
- Webhook → WhatsApp service: routes to · Webhook feeds WhatsApp message handling
- Auth service → User data: persists · Auth reads users and refresh tokens
- Auth service → Auth cache: caches · Session state and refresh tokens use Redis
- Catalog service → Commerce data: persists · Catalog data is stored in the commerce model
- Order service → Commerce data: persists · Orders are assembled from commerce entities
- Billing service → Commerce data: reads · Billing builds invoices from order and product data
- Chat orchestrator → OpenAI service: calls · Agent orchestration queries the AI provider
- Chat orchestrator → WhatsApp service: replies via · Generated responses are sent through WhatsApp
- WhatsApp service → External adapters: depends on · Twilio and HTTP client configuration support delivery
- OpenAI service → External adapters: depends on · OpenAI service uses outbound HTTP configuration
- Billing service → External adapters: depends on · Factus integration uses external client configuration
- Security core → Auth cache: integrates · Security works with token cache for transient state
- Auth cache → Redis infra: backed by · Redis service sits on Redis infrastructure
