# mik3si7va/baths architecture

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

- Interactive diagram: https://gitdiagram.com/mik3si7va/baths
- Repository: https://github.com/mik3si7va/baths

## Overview

1. **What this repo is**
   - The main system is a clinic/spa management platform for banhos e tosquias.
   - It is split into three cooperating runtimes: a **React frontend**, a **Node/Express + Prisma backend**, and a **Camunda 7 workflow layer** with external-task workers.
   - The operational focus is on **clientes/animais**, **agendamentos**, **faturação**, **salas**, **serviços**, **funcionários**, and **contas/auth**.

2. **Top-level runtime topology**
   - `docker-compose.yml` and the root npm scripts orchestrate the local stack.
   - The backend talks to **Postgres** through Prisma.
   - Camunda runs separately and is deployed with BPMN definitions from `camunda/workflows/`.
   - The frontend runs as a browser SPA and consumes the backend REST API.
   - The repo’s local flow is: **browser → frontend → backend/API → Postgres**, with **Camunda/workers** handling the process orchestration side path.

3. **Backend architecture**
   - The backend lives in `backend/` and is a classic **Express REST API**.
   - `backend/src/server.js` is the main entry point, exposing the HTTP routes and Swagger docs.
   - `backend/src/repositories/` is the main boundary to persistence; each repository encapsulates a business area such as agendamentos, clientes, contas, faturas, funcionários, salas, serviços, perfil, auth, and Camunda integration.
   - `backend/src/domain/` contains shared business concepts: entities and enums like `Sala`, `TipoServico`, `RegraPreco`, `DiaSemanaEnum`, `PorteEnum`, and `TipoFuncionarioEnum`.
   - `backend/src/services/emailService.js` is the outbound email boundary used for invitation/password-reset style communication.

4. **Persistence and database lifecycle**
   - Persistence is **Postgres + Prisma**.
   - `backend/prisma/schema.prisma`, migrations, and `seed.js` define the data model and bootstrap data.
   - The README and migration history show a mature schema that has evolved around:
     - users/funcionários/auth
     - clientes and animais
     - agendamentos and their state transitions
     - pagamentos and faturas
     - salas, serviços, and pricing rules
     - password reset tokens
   - Database setup is intentionally scripted (`db:full`, migrations, seed), which means the architecture expects a reproducible local/CI database state rather than ad hoc manual setup.

5. **Workflow orchestration with Camunda**
   - The critical business flows are not implemented only in the API; they are orchestrated in **Camunda BPMN**.
   - Main workflows are in `camunda/workflows/`, notably:
     - `agendamento.bpmn`
     - `gestao_agendamento.bpmn`
     - subprocesses for service preparation, solution generation, reservation handling, and invoicing
     - a DMN file for service ordering
   - This makes workflow state a first-class architectural concern: the system separates **business process orchestration** from pure CRUD persistence.

6. **Camunda worker layer**
   - `camunda/workers/` implements **external task workers**.
   - The `workers/` subfolders are organized by process area: `agendamento`, `reservas`, `fatura`, `email`, `solucoes`, `precoEServico`.
   - Each worker file is numbered to mirror BPMN execution order, which makes the diagramming relationship between BPMN steps and implementation explicit.
   - Worker services in `camunda/workers/services/` encapsulate domain logic and DB access; they are the bridge between process tasks and the application data model.
   - Supporting utilities handle Camunda client communication, DB access, logging, and scheduling/horário logic.

7. **Frontend architecture**
   - The root `frontend/` is a **React SPA** (Create React App style structure).
   - The UI is organized by feature pages under `frontend/src/pages/`:
     - home, login, perfil
     - clientes, contas, funcionários
     - agendamentos, calendar
     - faturas, salas, serviços, pesquisa, password recovery
   - Shared UI lives in `frontend/src/components/` and `frontend/src/layouts/`.
   - `frontend/src/routes.jsx` is the route map and contains the access-control boundaries.

8. **Authentication, session, and role boundaries**
   - The frontend has an `AuthContext` and guarded routes (`PrivateRoute` / admin guards in the route layer).
   - The README says the authenticated session is stored in **localStorage**.
   - There are distinct roles in the system, at least **administrador** and **funcionário**, and the frontend routes reflect that separation.
   - Account management is not just login/logout: `/contas` also supports invitation flows that create password-definition tokens and email them to staff.

9. **User-facing data flows**
   - CRUD-heavy flows such as clientes, animais, salas, serviços, funcionários, and faturas are driven from the React pages to the backend REST API.
   - Scheduling is the most workflow-heavy path: the UI initiates a booking, Camunda coordinates reservation/validation/solution steps, and the backend persists the resulting state.
   - Faturação has a similar split: the process layer coordinates invoice generation and payment registration, while the backend/database hold the persisted business record.
   - The frontend includes helper utilities for calendar presentation, client filtering, and Camunda-related UI support.

10. **Notifications and email**
   - Email is an outbound integration point from the backend and from workflow workers.
   - The README shows SMTP/Mailtrap-style configuration and a `DISABLE_EMAILS` switch for local development.
   - This is used especially for invitation emails and workflow notifications such as confirmations, cancellations, reschedules, no-shows, and invoice delivery.

11. **Testing and quality gates**
   - The backend uses **Jest** with both repository tests and API tests against a real database.
   - The frontend uses **Jest + Testing Library** for isolated component/page tests.
   - There is also **Cypress** coverage in the frontend for at least the salas flow.
   - CI is GitHub Actions with separate lint/test/build stages for backend and frontend, which means the repo treats both halves as independently verifiable deliverables.

12. **Secondary subtree**
   - `AppClient/` is a smaller, separate backend/frontend pair with its own minimal auth-focused React app and server.
   - It looks like a pared-down or legacy client stack rather than the main product architecture described by the README.
   - If you draw the architecture for the active system, keep the root-level `backend/`, `frontend/`, and `camunda/` as the primary diagram and treat `AppClient/` as an auxiliary/older subtree unless you specifically need to document it.

Main entry points: [`backend/src/server.js`](https://github.com/mik3si7va/baths/blob/main/backend/src/server.js) (API server), [`backend/src/repositories`](https://github.com/mik3si7va/baths/tree/main/backend/src/repositories) (Repositories), [`docker-compose.yml`](https://github.com/mik3si7va/baths/blob/main/docker-compose.yml) (Compose), [`frontend/src/routes.jsx`](https://github.com/mik3si7va/baths/blob/main/frontend/src/routes.jsx) (Route map)

## Diagram (Mermaid)

```mermaid
flowchart TD

subgraph group_root["Main stack"]
  node_compose["Compose<br/>orchestration<br/>[docker-compose.yml]"]
end

subgraph group_backend["Backend API"]
  node_backend_server["API server<br/>express entrypoint<br/>[server.js]"]
  node_backend_repos["Repositories<br/>persistence boundary"]
  node_backend_domain{{"Domain model<br/>business concepts"}}
  node_backend_db[("Prisma DB<br/>postgres/prisma")]
  node_email_service["Email service<br/>outbound integration<br/>[emailService.js]"]
end

subgraph group_camunda["Workflow layer"]
  node_camunda_bpmn["BPMN flows<br/>workflow definitions"]
  node_camunda_dmn["DMN rules<br/>decision rules"]
  node_camunda_workers_root["Workers<br/>[index.js]"]
  node_worker_agendamento["Booking tasks<br/>worker bundle"]
  node_worker_reservas["Reservation tasks<br/>worker bundle"]
  node_worker_fatura["Invoice tasks<br/>worker bundle"]
  node_worker_email["Email tasks<br/>worker bundle"]
  node_worker_solucoes["Solution tasks<br/>worker bundle"]
  node_worker_precos["Pricing tasks<br/>worker bundle"]
end

subgraph group_frontend["Frontend SPA"]
  node_frontend_routes["Route map<br/>spa routing<br/>[routes.jsx]"]
  node_frontend_auth["Auth context<br/>session/guards"]
  node_frontend_pages["Feature pages<br/>ui features"]
  node_frontend_components["Shared UI<br/>ui components"]
  node_frontend_utils["UI helpers<br/>presentation utils"]
end

subgraph group_aux["Auxiliary client"]
  node_appclient["AppClient<br/>auxiliary app"]
end

node_compose -->|"starts"| node_backend_server
node_compose -->|"starts"| node_camunda_workers_root
node_compose -->|"starts"| node_frontend_routes
node_backend_server -->|"uses"| node_backend_repos
node_backend_server -->|"shares model"| node_backend_domain
node_backend_server -->|"sends mail"| node_email_service
node_backend_repos -->|"persists to"| node_backend_db
node_backend_domain -.->|"migrates to"| node_backend_db
node_frontend_routes -->|"guards"| node_frontend_auth
node_frontend_routes -->|"routes to"| node_frontend_pages
node_frontend_pages -->|"composes"| node_frontend_components
node_frontend_pages -->|"uses"| node_frontend_utils
node_frontend_auth -->|"calls API"| node_backend_server
node_backend_server -.->|"integrates"| node_camunda_workers_root
node_camunda_workers_root -->|"executes"| node_camunda_bpmn
node_camunda_bpmn -->|"consults"| node_camunda_dmn
node_worker_agendamento -->|"writes agendamentos"| node_backend_repos
node_worker_reservas -->|"checks rooms"| node_backend_repos
node_worker_fatura -->|"writes faturas"| node_backend_repos
node_worker_email -->|"delivers"| node_email_service
node_worker_solucoes -->|"depends on"| node_worker_precos
node_worker_precos -->|"reads services"| node_backend_repos
node_appclient -.->|"separate stack"| node_backend_server

click node_compose "https://github.com/mik3si7va/baths/blob/main/docker-compose.yml"
click node_backend_server "https://github.com/mik3si7va/baths/blob/main/backend/src/server.js"
click node_backend_repos "https://github.com/mik3si7va/baths/tree/main/backend/src/repositories"
click node_backend_domain "https://github.com/mik3si7va/baths/tree/main/backend/src/domain"
click node_backend_db "https://github.com/mik3si7va/baths/tree/main/backend/prisma"
click node_email_service "https://github.com/mik3si7va/baths/blob/main/backend/src/services/emailService.js"
click node_camunda_bpmn "https://github.com/mik3si7va/baths/tree/main/camunda/workflows"
click node_camunda_dmn "https://github.com/mik3si7va/baths/blob/main/camunda/workflows/ordenar_servicos.dmn"
click node_camunda_workers_root "https://github.com/mik3si7va/baths/blob/main/camunda/workers/index.js"
click node_worker_agendamento "https://github.com/mik3si7va/baths/tree/main/camunda/workers/workers/agendamento"
click node_worker_reservas "https://github.com/mik3si7va/baths/tree/main/camunda/workers/workers/reservas"
click node_worker_fatura "https://github.com/mik3si7va/baths/tree/main/camunda/workers/workers/fatura"
click node_worker_email "https://github.com/mik3si7va/baths/tree/main/camunda/workers/workers/email"
click node_worker_solucoes "https://github.com/mik3si7va/baths/tree/main/camunda/workers/workers/solucoes"
click node_worker_precos "https://github.com/mik3si7va/baths/tree/main/camunda/workers/workers/precoEServico"
click node_frontend_routes "https://github.com/mik3si7va/baths/blob/main/frontend/src/routes.jsx"
click node_frontend_auth "https://github.com/mik3si7va/baths/tree/main/frontend/src/contexts"
click node_frontend_pages "https://github.com/mik3si7va/baths/tree/main/frontend/src/pages"
click node_frontend_components "https://github.com/mik3si7va/baths/tree/main/frontend/src/components"
click node_frontend_utils "https://github.com/mik3si7va/baths/tree/main/frontend/src/utils"
click node_appclient "https://github.com/mik3si7va/baths/tree/main/AppClient"

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_compose toneBlue
class node_backend_server,node_backend_repos,node_backend_domain,node_backend_db,node_email_service toneAmber
class node_camunda_bpmn,node_camunda_dmn,node_camunda_workers_root,node_worker_agendamento,node_worker_reservas,node_worker_fatura,node_worker_email,node_worker_solucoes,node_worker_precos toneMint
class node_frontend_routes,node_frontend_auth,node_frontend_pages,node_frontend_components,node_frontend_utils toneRose
class node_appclient toneIndigo
```

## Components

### Main stack

Primary clinic/spa management system

- [Compose](https://github.com/mik3si7va/baths/blob/main/docker-compose.yml): orchestration · Local stack and service startup · `docker-compose.yml`

### Backend API

Express + Prisma service

- [API server](https://github.com/mik3si7va/baths/blob/main/backend/src/server.js): express entrypoint · HTTP server and route wiring · `backend/src/server.js`
- [Repositories](https://github.com/mik3si7va/baths/tree/main/backend/src/repositories): persistence boundary · Business-area repositories for CRUD and integration · `backend/src/repositories`
- [Domain model](https://github.com/mik3si7va/baths/tree/main/backend/src/domain): business concepts · Shared entities and enums for core clinic concepts · `backend/src/domain`
- [Prisma DB](https://github.com/mik3si7va/baths/tree/main/backend/prisma): postgres/prisma · Schema, migrations, seed, and DB lifecycle · `backend/prisma`
- [Email service](https://github.com/mik3si7va/baths/blob/main/backend/src/services/emailService.js): outbound integration · SMTP-style email boundary for invites and notifications · `backend/src/services/emailService.js`

### Workflow layer

Camunda BPMN and external-task workers

- [BPMN flows](https://github.com/mik3si7va/baths/tree/main/camunda/workflows): workflow definitions · Scheduling and invoicing process models · `camunda/workflows`
- [DMN rules](https://github.com/mik3si7va/baths/blob/main/camunda/workflows/ordenar_servicos.dmn): decision rules · Service ordering and related workflow decisions · `camunda/workflows/ordenar_servicos.dmn`
- [Workers](https://github.com/mik3si7va/baths/blob/main/camunda/workers/index.js): external-task workers · Worker runtime entry and process subscriptions · `camunda/workers/index.js`
- [Booking tasks](https://github.com/mik3si7va/baths/tree/main/camunda/workers/workers/agendamento): worker bundle · Creates and updates agendamento state across process steps · `camunda/workers/workers/agendamento`
- [Reservation tasks](https://github.com/mik3si7va/baths/tree/main/camunda/workers/workers/reservas): worker bundle · Temporary and process reservations plus availability checks · `camunda/workers/workers/reservas`
- [Invoice tasks](https://github.com/mik3si7va/baths/tree/main/camunda/workers/workers/fatura): worker bundle · Invoice generation and payment registration · `camunda/workers/workers/fatura`
- [Email tasks](https://github.com/mik3si7va/baths/tree/main/camunda/workers/workers/email): worker bundle · Workflow-driven confirmation, cancellation, and invoice emails · `camunda/workers/workers/email`
- [Solution tasks](https://github.com/mik3si7va/baths/tree/main/camunda/workers/workers/solucoes): worker bundle · Generates and orders candidate scheduling solutions · `camunda/workers/workers/solucoes`
- [Pricing tasks](https://github.com/mik3si7va/baths/tree/main/camunda/workers/workers/precoEServico): worker bundle · Builds service lists and price/duration summaries · `camunda/workers/workers/precoEServico`

### Frontend SPA

React client application

- [Route map](https://github.com/mik3si7va/baths/blob/main/frontend/src/routes.jsx): spa routing · Access control and page navigation boundaries · `frontend/src/routes.jsx`
- [Auth context](https://github.com/mik3si7va/baths/tree/main/frontend/src/contexts): session/guards · LocalStorage-backed session and protected routes · `frontend/src/contexts`
- [Feature pages](https://github.com/mik3si7va/baths/tree/main/frontend/src/pages): ui features · Customers, appointments, invoices, rooms, services, accounts, profile · `frontend/src/pages`
- [Shared UI](https://github.com/mik3si7va/baths/tree/main/frontend/src/components): ui components · Reusable dialogs, cards, calendar and forms · `frontend/src/components`
- [UI helpers](https://github.com/mik3si7va/baths/tree/main/frontend/src/utils): presentation utils · Calendar, client filtering, and Camunda helper utilities · `frontend/src/utils`

### Auxiliary client

Smaller legacy or secondary app

- [AppClient](https://github.com/mik3si7va/baths/tree/main/AppClient): auxiliary app · Smaller secondary client stack with its own frontend/backend · `AppClient`

## Connections

- Compose → API server: starts · Launches backend service in the local stack
- Compose → Workers: starts · Launches workflow worker runtime
- Compose → Route map: starts · Launches browser SPA
- API server → Repositories: uses · Routes delegate persistence and business operations
- API server → Domain model: shares model · API uses common business types and enums
- API server → Email service: sends mail · Invitations and password/reset notifications
- Repositories → Prisma DB: persists to · Repositories read and write Prisma/Postgres data
- Domain model → Prisma DB: migrates to · Domain concepts are reflected in the database schema
- Route map → Auth context: guards · Route access is filtered by auth state and role
- Route map → Feature pages: routes to · Maps URLs to feature pages
- Feature pages → Shared UI: composes · Pages reuse shared UI primitives
- Feature pages → UI helpers: uses · Pages rely on calendar and filtering helpers
- Auth context → API server: calls API · Authenticated UI consumes REST endpoints
- API server → Workers: integrates · Backend exposes data and task integration points for workflows
- Workers → BPMN flows: executes · Workers subscribe to BPMN external tasks
- BPMN flows → DMN rules: consults · Workflow decisions use service-ordering rules
- Booking tasks → Repositories: writes agendamentos · Booking process persists scheduling state
- Reservation tasks → Repositories: checks rooms · Reservation tasks validate and release availability
- Invoice tasks → Repositories: writes faturas · Invoicing process persists billing records
- Email tasks → Email service: delivers · Workflow notifications use outbound mail integration
- Solution tasks → Pricing tasks: depends on · Solution generation uses pricing/duration summaries
- Pricing tasks → Repositories: reads services · Price and service workers consult persisted catalog data
- AppClient → API server: separate stack · Secondary app is distinct from the main product architecture
