# gustavoleal1194/projeto_gestao_casa_di_ana architecture

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

- Interactive diagram: https://gitdiagram.com/gustavoleal1194/projeto_gestao_casa_di_ana
- Repository: https://github.com/gustavoleal1194/projeto_gestao_casa_di_ana

## Overview

1. **Overall shape**
   Casa di Ana is a two-part ERP: a **.NET 8 backend API** and a **React 18 SPA frontend**. The backend owns all business rules and persistence; the frontend is a feature-based client that calls the API over REST.

2. **Backend architecture**
   The backend follows **Clean Architecture + CQRS**. The solution is split into `Domain`, `Application`, `Infrastructure`, and `API`, with one-way dependencies inward. `API` only orchestrates HTTP traffic; `Application` defines use cases; `Infrastructure` provides EF Core and external services; `Domain` contains the core model and contracts.

3. **Domain layer boundary**
   `CasaDiAna.Domain` is the business core. It holds entities such as `Ingrediente`, `Produto`, `EntradaMercadoria`, `Inventario`, `ProducaoDiaria`, `VendaDiaria`, `NotificacaoEstoque`, and `Usuario`, plus enums like roles, movement types, and statuses. Repositories and services are only exposed as interfaces here, so the rest of the system depends on abstractions.

4. **Application layer use-case model**
   `CasaDiAna.Application` is organized by business module: auth, estoque, entradas, inventários, produção, relatórios, notificações, usuários, etc. Each operation is represented as a MediatR **Command** or **Query**, with handlers and FluentValidation validators alongside DTOs. This is where orchestration happens: validation, domain method calls, repository access, and response shaping.

5. **HTTP entry and request pipeline**
   `CasaDiAna.API` contains the REST controllers, a global exception middleware, and startup bootstrapping in `Program.cs`. Controllers are thin; they translate HTTP requests into MediatR calls and return a standardized `ApiResponse<T>`. The middleware converts validation, domain, auth, and unexpected exceptions into consistent HTTP responses.

6. **Infrastructure and persistence**
   `CasaDiAna.Infrastructure` implements the domain interfaces using **EF Core 8 + PostgreSQL**. `AppDbContext` maps the database with explicit fluent configurations and supports three logical schemas: `auth`, `estoque`, and `producao`. Migrations are checked in and applied on startup, so schema evolution is part of the runtime setup.

7. **Cross-cutting backend services**
   The infrastructure also provides JWT issuance, current-user extraction, and stock notification generation. Authentication relies on `JwtService`; authorization claims are consumed by `CurrentUserService`; `NotificacaoEstoqueService` evaluates ingredient stock levels and creates notifications. These services are used across multiple modules, not tied to one controller.

8. **Primary business flows**
   The main data flows are inventory-related: **entradas** increase stock, **produção diária** decreases stock via product recipe/ficha técnica, **vendas diárias** and **perdas** record outflows, and **inventários/correções** reconcile physical counts against system balances. Every stock change is tracked through `Movimentacao`, which preserves origin references for auditability.

9. **Production and product modeling**
   The production domain centers on `Produto`, `CategoriaProduto`, and `ItemFichaTecnica`. Products define how ingredients are consumed when production is registered, and the system calculates cost and stock impact from that recipe. This module also includes daily production registration, sales by product, and loss tracking.

10. **Notifications, labels, and reports**
   Three supporting subsystems sit on top of the operational data: **stock notifications**, **label generation**, and **reports**. Notifications surface low/critical/empty stock; labels persist nutritional models and print history; reports aggregate stock, movements, entradas, production vs sales, and ingredient consumption, with PDF export on the client side.

11. **Frontend architecture**
   The frontend is structured by **features**, not by technical layers. Each module under `frontend/src/features/` owns its pages, services, hooks, and local components. Shared UI pieces live in `components/layout` and `components/form`, while `routes/AppRoutes.tsx` wires navigation and `lib/api.ts` centralizes API access.

12. **Frontend state and API flow**
   `Zustand` is used only for authentication state: JWT token, current user, role checks, and logout. Forms use React Hook Form and Zod; data fetching is encapsulated in feature services that call the shared Axios client. The Axios layer injects the bearer token and forces logout on 401 responses, making auth the only global client-side concern.

13. **Deployment and runtime boundaries**
   The repo is designed for containerized deployment with Docker Compose. The frontend is built with Vite and served by Nginx in production; the backend runs as ASP.NET Core and exposes Swagger in non-production. Render is the target hosting platform, and the backend health endpoint exists for platform monitoring.

14. **Testing scope**
   Tests currently focus on **Application-layer handlers** with xUnit, Moq, and FluentAssertions. That means the core use-case logic is covered without booting the full API or database, reinforcing the architecture boundary between orchestration and infrastructure.

15. **What matters for a diagram**
   The key diagram should show: browser/SPA on one side, REST API in the middle, and PostgreSQL underneath Infrastructure; then separate the backend into API → Application → Domain, with Infrastructure implementing Domain interfaces. The most important domain flows to label are estoque movements, production/sales consumption, inventory reconciliation, and auth/token-based access control.

Main entry points: [`CasaDiAna/src/CasaDiAna.API/Controllers`](https://github.com/gustavoleal1194/projeto_gestao_casa_di_ana/blob/master/CasaDiAna/src/CasaDiAna.API/Controllers) (Controllers), [`CasaDiAna/src/CasaDiAna.Infrastructure/Persistence/AppDbContext.cs`](https://github.com/gustavoleal1194/projeto_gestao_casa_di_ana/blob/master/CasaDiAna/src/CasaDiAna.Infrastructure/Persistence/AppDbContext.cs) (DbContext), [`CasaDiAna/src/CasaDiAna.Domain/Entities/Produto.cs`](https://github.com/gustavoleal1194/projeto_gestao_casa_di_ana/blob/master/CasaDiAna/src/CasaDiAna.Domain/Entities/Produto.cs) (Core model), [`CasaDiAna/src/CasaDiAna.Domain/Interfaces/IProdutoRepository.cs`](https://github.com/gustavoleal1194/projeto_gestao_casa_di_ana/blob/master/CasaDiAna/src/CasaDiAna.Domain/Interfaces/IProdutoRepository.cs) (Contracts)

## Diagram (Mermaid)

```mermaid
flowchart TD

subgraph group_frontend["Frontend"]
  node_frontend_app["SPA shell<br/>React 18 app<br/>[main.tsx]"]
  node_frontend_routes["Routes<br/>navigation<br/>[AppRoutes.tsx]"]
  node_frontend_api["API client<br/>axios client<br/>[api.ts]"]
  node_frontend_auth["Auth store<br/>zustand state<br/>[authStore.ts]"]
  node_frontend_features["Features<br/>feature modules"]
end

subgraph group_backend_api["API"]
  node_api_host["Program<br/>ASP.NET host<br/>[Program.cs]"]
  node_api_controllers["Controllers<br/>REST endpoints<br/>[Controllers]"]
  node_api_middleware["Middleware<br/>exception pipeline"]
end

subgraph group_backend_app["Application"]
  node_app_auth["Auth use case<br/>CQRS handlers"]
  node_app_estoque["Stock flows<br/>CQRS handlers"]
  node_app_prod["Production flows<br/>CQRS handlers"]
  node_app_invrep["Inventories & reports<br/>CQRS handlers"]
  node_app_notif["Notifications<br/>service contract"]
end

subgraph group_backend_domain["Domain"]
  node_domain_core["Core model<br/>entities and enums<br/>[Produto.cs]"]
  node_domain_contracts["Contracts<br/>repository interfaces"]
end

subgraph group_backend_infra["Infrastructure"]
  node_infra_db["DbContext<br/>EF Core 8<br/>[AppDbContext.cs]"]
  node_infra_services["Infra services<br/>auth/stock services"]
  node_infra_repos["Repositories<br/>EF implementations<br/>[Repositories]"]
  node_postgres[("PostgreSQL<br/>database")]
end

subgraph group_delivery["Delivery"]
  node_compose["Docker Compose<br/>runtime orchestration<br/>[docker-compose.yml]"]
end

subgraph group_tests["Tests"]
  node_tests_app["Use-case tests<br/>xUnit suite"]
end

node_frontend_app -->|"uses"| node_frontend_routes
node_frontend_app -->|"reads auth"| node_frontend_auth
node_frontend_routes -->|"opens"| node_frontend_features
node_frontend_features -->|"calls"| node_frontend_api
node_frontend_api -->|"injects token"| node_frontend_auth
node_api_host -->|"hosts"| node_api_controllers
node_api_host -->|"pipes through"| node_api_middleware
node_api_controllers -->|"dispatches"| node_app_auth
node_api_controllers -->|"dispatches"| node_app_estoque
node_api_controllers -->|"dispatches"| node_app_prod
node_api_controllers -->|"dispatches"| node_app_invrep
node_app_auth -->|"depends on"| node_domain_contracts
node_app_estoque -->|"mutates"| node_domain_core
node_app_prod -->|"mutates"| node_domain_core
node_app_invrep -->|"reads"| node_domain_core
node_app_notif -->|"depends on"| node_domain_contracts
node_infra_repos -->|"implements"| node_domain_contracts
node_infra_services -->|"implements"| node_domain_contracts
node_infra_db -->|"maps"| node_domain_core
node_infra_db -->|"persists to"| node_postgres
node_infra_repos -->|"uses"| node_infra_db
node_infra_services -->|"reads/writes"| node_infra_db
node_compose -->|"runs"| node_api_host
node_compose -->|"serves"| node_frontend_app
node_tests_app -->|"verifies"| node_app_auth
node_tests_app -->|"verifies"| node_app_estoque
node_tests_app -->|"verifies"| node_app_invrep

click node_frontend_app "https://github.com/gustavoleal1194/projeto_gestao_casa_di_ana/blob/master/CasaDiAna/frontend/src/main.tsx"
click node_frontend_routes "https://github.com/gustavoleal1194/projeto_gestao_casa_di_ana/blob/master/CasaDiAna/frontend/src/routes/AppRoutes.tsx"
click node_frontend_api "https://github.com/gustavoleal1194/projeto_gestao_casa_di_ana/blob/master/CasaDiAna/frontend/src/lib/api.ts"
click node_frontend_auth "https://github.com/gustavoleal1194/projeto_gestao_casa_di_ana/blob/master/CasaDiAna/frontend/src/store/authStore.ts"
click node_frontend_features "https://github.com/gustavoleal1194/projeto_gestao_casa_di_ana/tree/master/CasaDiAna/frontend/src/features"
click node_api_host "https://github.com/gustavoleal1194/projeto_gestao_casa_di_ana/blob/master/CasaDiAna/src/CasaDiAna.API/Program.cs"
click node_api_controllers "https://github.com/gustavoleal1194/projeto_gestao_casa_di_ana/blob/master/CasaDiAna/src/CasaDiAna.API/Controllers"
click node_api_middleware "https://github.com/gustavoleal1194/projeto_gestao_casa_di_ana/blob/master/CasaDiAna/src/CasaDiAna.API/Middleware/ExceptionHandlingMiddleware.cs"
click node_app_auth "https://github.com/gustavoleal1194/projeto_gestao_casa_di_ana/blob/master/CasaDiAna/src/CasaDiAna.Application/Auth/Commands/Login/LoginCommandHandler.cs"
click node_app_estoque "https://github.com/gustavoleal1194/projeto_gestao_casa_di_ana/blob/master/CasaDiAna/src/CasaDiAna.Application/Entradas/Commands/RegistrarEntrada/RegistrarEntradaCommandHandler.cs"
click node_app_prod "https://github.com/gustavoleal1194/projeto_gestao_casa_di_ana/blob/master/CasaDiAna/src/CasaDiAna.Application/ProducaoDiaria/Commands/RegistrarProducao/RegistrarProducaoCommandHandler.cs"
click node_app_invrep "https://github.com/gustavoleal1194/projeto_gestao_casa_di_ana/blob/master/CasaDiAna/src/CasaDiAna.Application/Inventarios/Commands/IniciarInventario/IniciarInventarioCommandHandler.cs"
click node_app_notif "https://github.com/gustavoleal1194/projeto_gestao_casa_di_ana/blob/master/CasaDiAna/src/CasaDiAna.Application/Notificacoes/Services/INotificacaoEstoqueService.cs"
click node_domain_core "https://github.com/gustavoleal1194/projeto_gestao_casa_di_ana/blob/master/CasaDiAna/src/CasaDiAna.Domain/Entities/Produto.cs"
click node_domain_contracts "https://github.com/gustavoleal1194/projeto_gestao_casa_di_ana/blob/master/CasaDiAna/src/CasaDiAna.Domain/Interfaces/IProdutoRepository.cs"
click node_infra_db "https://github.com/gustavoleal1194/projeto_gestao_casa_di_ana/blob/master/CasaDiAna/src/CasaDiAna.Infrastructure/Persistence/AppDbContext.cs"
click node_infra_services "https://github.com/gustavoleal1194/projeto_gestao_casa_di_ana/blob/master/CasaDiAna/src/CasaDiAna.Infrastructure/Services/NotificacaoEstoqueService.cs"
click node_infra_repos "https://github.com/gustavoleal1194/projeto_gestao_casa_di_ana/blob/master/CasaDiAna/src/CasaDiAna.Infrastructure/Repositories"
click node_compose "https://github.com/gustavoleal1194/projeto_gestao_casa_di_ana/blob/master/CasaDiAna/docker-compose.yml"
click node_tests_app "https://github.com/gustavoleal1194/projeto_gestao_casa_di_ana/blob/master/CasaDiAna/tests/CasaDiAna.Application.Tests/CasaDiAna.Application.Tests.csproj"

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_routes,node_frontend_api,node_frontend_auth,node_frontend_features,node_tests_app toneBlue
class node_api_host,node_api_controllers,node_api_middleware toneAmber
class node_app_auth,node_app_estoque,node_app_prod,node_app_invrep,node_app_notif toneMint
class node_domain_core,node_domain_contracts toneRose
class node_infra_db,node_infra_services,node_infra_repos,node_postgres toneIndigo
class node_compose toneTeal
```

## Components

### Frontend

React SPA and client-side state/API flow

- [SPA shell](https://github.com/gustavoleal1194/projeto_gestao_casa_di_ana/blob/master/CasaDiAna/frontend/src/main.tsx): React 18 app · Bootstraps the client application and layout flow · `CasaDiAna/frontend/src/main.tsx`
- [Routes](https://github.com/gustavoleal1194/projeto_gestao_casa_di_ana/blob/master/CasaDiAna/frontend/src/routes/AppRoutes.tsx): navigation · Declares application navigation and feature entry points · `CasaDiAna/frontend/src/routes/AppRoutes.tsx`
- [API client](https://github.com/gustavoleal1194/projeto_gestao_casa_di_ana/blob/master/CasaDiAna/frontend/src/lib/api.ts): axios client · Central REST access with token injection and 401 handling · `CasaDiAna/frontend/src/lib/api.ts`
- [Auth store](https://github.com/gustavoleal1194/projeto_gestao_casa_di_ana/blob/master/CasaDiAna/frontend/src/store/authStore.ts): zustand state · Holds token, user, role checks, and logout state · `CasaDiAna/frontend/src/store/authStore.ts`
- [Features](https://github.com/gustavoleal1194/projeto_gestao_casa_di_ana/tree/master/CasaDiAna/frontend/src/features): feature modules · Feature-based pages, hooks, services, and local UI · `CasaDiAna/frontend/src/features`

### API

HTTP entrypoint, controllers, and middleware

- [Program](https://github.com/gustavoleal1194/projeto_gestao_casa_di_ana/blob/master/CasaDiAna/src/CasaDiAna.API/Program.cs): ASP.NET host · Wires DI, middleware, controllers, and runtime startup · `CasaDiAna/src/CasaDiAna.API/Program.cs`
- [Controllers](https://github.com/gustavoleal1194/projeto_gestao_casa_di_ana/blob/master/CasaDiAna/src/CasaDiAna.API/Controllers): REST endpoints · Thin HTTP adapters that dispatch MediatR requests · `CasaDiAna/src/CasaDiAna.API/Controllers`
- [Middleware](https://github.com/gustavoleal1194/projeto_gestao_casa_di_ana/blob/master/CasaDiAna/src/CasaDiAna.API/Middleware/ExceptionHandlingMiddleware.cs): exception pipeline · Normalizes validation, auth, domain, and unexpected errors · `CasaDiAna/src/CasaDiAna.API/Middleware/ExceptionHandlingMiddleware.cs`

### Application

CQRS use cases and orchestration

- [Auth use case](https://github.com/gustavoleal1194/projeto_gestao_casa_di_ana/blob/master/CasaDiAna/src/CasaDiAna.Application/Auth/Commands/Login/LoginCommandHandler.cs): CQRS handlers · Login command flow and token response shaping · `CasaDiAna/src/CasaDiAna.Application/Auth/Commands/Login/LoginCommandHandler.cs`
- [Stock flows](https://github.com/gustavoleal1194/projeto_gestao_casa_di_ana/blob/master/CasaDiAna/src/CasaDiAna.Application/Entradas/Commands/RegistrarEntrada/RegistrarEntradaCommandHandler.cs): CQRS handlers · Inventory and entrada orchestration around stock changes · `CasaDiAna/src/CasaDiAna.Application/Entradas/Commands/RegistrarEntrada/RegistrarEntradaCommandHandler.cs`
- [Production flows](https://github.com/gustavoleal1194/projeto_gestao_casa_di_ana/blob/master/CasaDiAna/src/CasaDiAna.Application/ProducaoDiaria/Commands/RegistrarProducao/RegistrarProducaoCommandHandler.cs): CQRS handlers · Production, sales, losses, and recipe-driven consumption · `CasaDiAna/src/CasaDiAna.Application/ProducaoDiaria/Commands/RegistrarProducao/RegistrarProducaoCommandHandler.cs`
- [Inventories & reports](https://github.com/gustavoleal1194/projeto_gestao_casa_di_ana/blob/master/CasaDiAna/src/CasaDiAna.Application/Inventarios/Commands/IniciarInventario/IniciarInventarioCommandHandler.cs): CQRS handlers · Inventory reconciliation and operational reporting queries · `CasaDiAna/src/CasaDiAna.Application/Inventarios/Commands/IniciarInventario/IniciarInventarioCommandHandler.cs`
- [Notifications](https://github.com/gustavoleal1194/projeto_gestao_casa_di_ana/blob/master/CasaDiAna/src/CasaDiAna.Application/Notificacoes/Services/INotificacaoEstoqueService.cs): service contract · Application-facing stock notification service contract and workflows · `CasaDiAna/src/CasaDiAna.Application/Notificacoes/Services/INotificacaoEstoqueService.cs`

### Domain

Business model and contracts

- [Core model](https://github.com/gustavoleal1194/projeto_gestao_casa_di_ana/blob/master/CasaDiAna/src/CasaDiAna.Domain/Entities/Produto.cs): entities and enums · Business entities, value concepts, and domain exceptions · `CasaDiAna/src/CasaDiAna.Domain/Entities/Produto.cs`
- [Contracts](https://github.com/gustavoleal1194/projeto_gestao_casa_di_ana/blob/master/CasaDiAna/src/CasaDiAna.Domain/Interfaces/IProdutoRepository.cs): repository interfaces · Repository and service abstractions consumed by application and infrastructure · `CasaDiAna/src/CasaDiAna.Domain/Interfaces/IProdutoRepository.cs`

### Infrastructure

EF Core persistence and cross-cutting services

- [DbContext](https://github.com/gustavoleal1194/projeto_gestao_casa_di_ana/blob/master/CasaDiAna/src/CasaDiAna.Infrastructure/Persistence/AppDbContext.cs): EF Core 8 · Database mapping, schema configuration, and migrations entrypoint · `CasaDiAna/src/CasaDiAna.Infrastructure/Persistence/AppDbContext.cs`
- [Infra services](https://github.com/gustavoleal1194/projeto_gestao_casa_di_ana/blob/master/CasaDiAna/src/CasaDiAna.Infrastructure/Services/NotificacaoEstoqueService.cs): auth/stock services · JWT, current-user access, and stock notification generation · `CasaDiAna/src/CasaDiAna.Infrastructure/Services/NotificacaoEstoqueService.cs`
- [Repositories](https://github.com/gustavoleal1194/projeto_gestao_casa_di_ana/blob/master/CasaDiAna/src/CasaDiAna.Infrastructure/Repositories): EF implementations · Concrete repository implementations for domain abstractions · `CasaDiAna/src/CasaDiAna.Infrastructure/Repositories`
- **PostgreSQL**: database · Primary relational store behind the ERP

### Delivery

Containerized runtime and hosting

- [Docker Compose](https://github.com/gustavoleal1194/projeto_gestao_casa_di_ana/blob/master/CasaDiAna/docker-compose.yml): runtime orchestration · Containerized local/runtime deployment wiring · `CasaDiAna/docker-compose.yml`

### Tests

Application-layer unit tests

- [Use-case tests](https://github.com/gustavoleal1194/projeto_gestao_casa_di_ana/blob/master/CasaDiAna/tests/CasaDiAna.Application.Tests/CasaDiAna.Application.Tests.csproj): xUnit suite · Application handler tests with mocked dependencies · `CasaDiAna/tests/CasaDiAna.Application.Tests/CasaDiAna.Application.Tests.csproj`

## Connections

- SPA shell → Routes: uses · Bootstraps navigation and feature screens
- SPA shell → Auth store: reads auth · Initializes client auth state and guards
- Routes → Features: opens · Routes resolve into feature-owned pages
- Features → API client: calls · Feature services use the shared REST client
- API client → Auth store: injects token · Bearer token is attached and 401 triggers logout
- Program → Controllers: hosts · Registers and serves REST controllers
- Program → Middleware: pipes through · Global exception handling sits in the request pipeline
- Controllers → Auth use case: dispatches · Controllers map HTTP auth requests to MediatR
- Controllers → Stock flows: dispatches · Stock endpoints forward to application commands/queries
- Controllers → Production flows: dispatches · Production endpoints forward use cases
- Controllers → Inventories & reports: dispatches · Inventory and report endpoints forward use cases
- Auth use case → Contracts: depends on · Use cases consume repository and service abstractions
- Stock flows → Core model: mutates · Entrada and stock logic operate on domain entities
- Production flows → Core model: mutates · Production and sales use the product and movement model
- Inventories & reports → Core model: reads · Inventory reconciliation and reports aggregate domain data
- Notifications → Contracts: depends on · Notification workflows depend on repository abstractions
- Repositories → Contracts: implements · EF repositories satisfy domain repository interfaces
- Infra services → Contracts: implements · Infrastructure services fulfill domain/application contracts
- DbContext → Core model: maps · EF Core maps entities and persistence shape
- DbContext → PostgreSQL: persists to · Application data is stored in PostgreSQL
- Repositories → DbContext: uses · Repositories are built on the DbContext
- Infra services → DbContext: reads/writes · Notification and auth services use persistence when needed
- Docker Compose → Program: runs · Compose orchestrates the ASP.NET backend container
- Docker Compose → SPA shell: serves · Compose/runtime setup includes the SPA delivery container
- Use-case tests → Auth use case: verifies · Tests focus on handler orchestration and validation behavior
- Use-case tests → Stock flows: verifies · Tests cover stock-related application flows
- Use-case tests → Inventories & reports: verifies · Tests cover inventory use-case boundaries
