# arcay322/sumaq_uywa architecture

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

- Interactive diagram: https://gitdiagram.com/arcay322/sumaq_uywa
- Repository: https://github.com/arcay322/sumaq_uywa

## Overview

1. **Repository shape**
   - This is a monorepo with two primary runtime boundaries: a **React/Vite frontend** at the repo root and a **Node.js/Express backend** in `backend/`.
   - The backend owns persistence, business rules, auth, file generation, and scheduled/background work. The frontend owns user interaction, navigation, and API consumption.

2. **Frontend application boundary**
   - The frontend is a TypeScript React app built with **Vite**, **Material UI**, **React Router**, and an API layer over **Axios**.
   - `src/pages` represents the main screens for domain areas like cuyes, galpones, gastos, ventas, salud, reproducción, dashboard, and login/register.
   - `src/components` contains reusable domain widgets, tables, dialogs, and specialized modules for alerts, calendar, reports, and dashboards.

3. **Frontend composition and state**
   - `src/layouts` defines app shell structure such as sidebar/main layout.
   - `src/contexts`, `src/hooks`, and `src/services` provide cross-cutting client concerns: notifications, error handling, delete confirmation, and WebSocket integration.
   - The frontend also has its own theme system in `src/theme` and UI utilities in `src/utils`, indicating a strong separation between domain pages and shared presentation behavior.

4. **Backend API boundary**
   - The backend is an **Express + TypeScript** API under `backend/src`.
   - The classic controller-route-service layering is explicit: `routes` define HTTP endpoints, `controllers` adapt requests/responses, and `services` contain business logic.
   - This structure is repeated per domain, which makes each functional area diagrammable as an independent vertical slice.

5. **Core domain subsystems**
   - The main backend domains are:
     - **Inventario/Cuyes/Galpones/Jaulas**
     - **Alimentación**
     - **Salud**
     - **Ventas**
     - **Gastos**
     - **Reproducción**
     - **Dashboard**
     - **Alerts/Notifications**
     - **Reports/Exports**
     - **Calendar**
     - **Auth**
   - Each domain has its own controller, route, schema, and service set, so the architecture is modular rather than centralized around one large service.

6. **Data access and persistence**
   - Persistence is handled by **Prisma ORM** against **PostgreSQL**.
   - Database structure evolves through `backend/prisma/migrations`, with a shared `schema.prisma` and a separate `schema.test.prisma` for test isolation.
   - `backend/src/utils/prisma.ts` and `backend/src/utils/database.ts` indicate shared database access helpers, likely used to enforce a stable client lifecycle and avoid connection churn.

7. **Authentication and request protection**
   - Authentication is JWT-based, with password hashing via bcrypt as documented in the README.
   - Protected routes are guarded by middleware in `backend/src/middlewares/auth.ts`, with validation middleware layered in for domain-specific constraints.
   - The frontend includes `PrivateRoute`, so access control exists on both the UI and API sides.

8. **Reporting and file generation subsystem**
   - Reports are a major standalone subsystem in `backend/src/services/reports`.
   - It includes generation of **CSV, Excel, and PDF** outputs, plus file storage, download, cleanup, job lifecycle, and job monitoring.
   - There is a separate reports job controller/route pair, so report creation is treated as an asynchronous workflow rather than a simple request/response action.
   - Generated artifacts are stored under `backend/uploads/reports`, which is a persistent file boundary distinct from the database.

9. **Background jobs, alerts, and real-time communication**
   - The backend has scheduled and operational background work in `backend/src/jobs`, especially alert scheduling.
   - Real-time behavior is supported by a WebSocket layer in `backend/src/services/websocket` and related middleware, used by the frontend notification widgets and dashboard components.
   - Alerts and notifications are split into their own domain, reflecting user-facing eventing separate from core CRUD modules.

10. **Dashboard and analytics flow**
   - Dashboard functionality aggregates data from several modules through dedicated services such as metrics, charts, realtime, and error handling.
   - This is a read-heavy integration layer: it pulls from cuyes, reproducción, ventas, gastos, salud, and alerts to present operational status and KPIs.
   - The dashboard is therefore an orchestration boundary rather than a data owner.

11. **Reproduction and lifecycle management**
   - Reproduction is one of the richer business domains, with entities like preñez, camadas, jala/jaula capacity, and reproductive integration services.
   - On the frontend, this domain has specialized managers, analytics, recommendations, compatibility widgets, and calendar integration.
   - The backend includes dedicated validation and dual naming variants (`preñez` / `prenez`), showing an evolved domain with compatibility and cleanup work around it.

12. **Calendar and scheduling**
   - The calendar subsystem exists on both sides: backend calendar services manage events and recovery, while the frontend provides reproductive calendar UI and event creation/details.
   - This suggests the calendar is used as an operational planner for reproduction and alerts, not as a general-purpose personal calendar.

13. **Testing and quality boundaries**
   - The backend has an extensive Jest suite, including unit, integration, and end-to-end tests for reports, jobs, cache, database utilities, API endpoints, and reproductive optimizations.
   - The frontend also includes tests for shared UI components and reporting progress components.
   - Several root-level `.kiro/specs` documents and stabilization notes indicate the repo has undergone structured cleanup and iterative hardening, especially around TypeScript, reporting, and reproduction.

14. **Deployment and environment**
   - The project is designed for separate frontend/backend environments, with `.env` examples for root and backend.
   - Deployment is oriented around **Render**, with scripts and configuration for automated build, migration, and service startup.
   - The backend depends on PostgreSQL, while the frontend is a static build served separately or behind a platform pipeline.

15. **How data moves through the system**
   - Typical flow: user action in a React page/component → frontend service/API call → Express route/controller → domain service → Prisma/PostgreSQL → response back to UI.
   - For reporting and alerts, the flow extends to background jobs, file generation/storage, WebSocket broadcasts, and then frontend notification/report history views.
   - This makes the architecture a combination of synchronous CRUD APIs and asynchronous operational subsystems, with the database as the central source of truth.

Main entry points: [`src/pages`](https://github.com/arcay322/sumaq_uywa/tree/main/src/pages) (Pages), [`src/components`](https://github.com/arcay322/sumaq_uywa/tree/main/src/components) (Widgets), [`backend/src/routes`](https://github.com/arcay322/sumaq_uywa/tree/main/backend/src/routes) (Routes), [`backend/src/services`](https://github.com/arcay322/sumaq_uywa/tree/main/backend/src/services) (Services)

## Diagram (Mermaid)

```mermaid
flowchart TD

subgraph group_frontend["Frontend"]
  node_frontend_app["App shell<br/>react-vite app<br/>[main.tsx]"]
  node_frontend_pages["Pages<br/>screens"]
  node_frontend_components["Widgets<br/>ui components"]
  node_frontend_layouts["Layouts<br/>shell"]
  node_frontend_state["Client state<br/>contexts-hooks"]
  node_frontend_api["API client<br/>axios services<br/>[api.ts]"]
  node_frontend_theme["Theme<br/>ui system"]
  node_frontend_access["Access control<br/>route guard<br/>[PrivateRoute.tsx]"]
end

subgraph group_backend["Backend"]
  node_backend_entry["Server entry<br/>express app<br/>[index.ts]"]
  node_backend_routes["Routes"]
  node_backend_controllers["Controllers"]
  node_backend_services["Services"]
  node_backend_middleware["Middleware<br/>request guards"]
  node_backend_jobs["Jobs<br/>background work"]
  node_backend_reports["Reports stack<br/>async exports"]
  node_backend_dashboard["Dashboard<br/>analytics layer"]
  node_backend_repro["Reproduction<br/>business domain"]
  node_backend_alerts["Alerts<br/>eventing domain"]
  node_backend_auth["Auth<br/>jwt security<br/>[auth.ts]"]
  node_backend_calendars["Calendar<br/>scheduling domain"]
end

subgraph group_data["Data"]
  node_backend_prisma[("Prisma<br/>orm persistence<br/>[schema.prisma]")]
  node_backend_uploads["Report files<br/>artifact storage"]
end

node_frontend_app -->|"navigates"| node_frontend_pages
node_frontend_app -->|"wraps"| node_frontend_layouts
node_frontend_pages -->|"composes"| node_frontend_components
node_frontend_pages -->|"calls"| node_frontend_api
node_frontend_components -->|"uses"| node_frontend_state
node_frontend_state -->|"observes"| node_frontend_api
node_frontend_access -->|"protects"| node_frontend_pages
node_frontend_theme -->|"styles"| node_frontend_components
node_backend_entry -->|"mounts"| node_backend_routes
node_backend_routes -->|"dispatches"| node_backend_controllers
node_backend_controllers -->|"delegates"| node_backend_services
node_backend_services -->|"persists"| node_backend_prisma
node_backend_middleware -->|"guards"| node_backend_routes
node_backend_jobs -->|"triggers"| node_backend_alerts
node_backend_reports -->|"stores"| node_backend_uploads
node_backend_reports -->|"reads"| node_backend_prisma
node_backend_dashboard -->|"aggregates"| node_backend_services
node_backend_repro -->|"coordinates"| node_backend_prisma
node_backend_alerts -->|"broadcasts"| node_backend_services
node_backend_calendars -->|"supports"| node_backend_repro
node_frontend_components -.->|"listens to"| node_backend_alerts
node_frontend_components -.->|"visualizes"| node_backend_dashboard
node_frontend_pages -.->|"requests"| node_backend_reports
node_frontend_pages -.->|"operates on"| node_backend_repro
node_frontend_pages -.->|"plans with"| node_backend_calendars
node_backend_auth -->|"secures"| node_backend_routes

click node_frontend_app "https://github.com/arcay322/sumaq_uywa/blob/main/src/main.tsx"
click node_frontend_pages "https://github.com/arcay322/sumaq_uywa/tree/main/src/pages"
click node_frontend_components "https://github.com/arcay322/sumaq_uywa/tree/main/src/components"
click node_frontend_layouts "https://github.com/arcay322/sumaq_uywa/tree/main/src/layouts"
click node_frontend_state "https://github.com/arcay322/sumaq_uywa/tree/main/src/contexts"
click node_frontend_api "https://github.com/arcay322/sumaq_uywa/blob/main/src/services/api.ts"
click node_frontend_theme "https://github.com/arcay322/sumaq_uywa/tree/main/src/theme"
click node_frontend_access "https://github.com/arcay322/sumaq_uywa/blob/main/src/components/PrivateRoute.tsx"
click node_backend_entry "https://github.com/arcay322/sumaq_uywa/blob/main/backend/src/index.ts"
click node_backend_routes "https://github.com/arcay322/sumaq_uywa/tree/main/backend/src/routes"
click node_backend_controllers "https://github.com/arcay322/sumaq_uywa/tree/main/backend/src/controllers"
click node_backend_services "https://github.com/arcay322/sumaq_uywa/tree/main/backend/src/services"
click node_backend_middleware "https://github.com/arcay322/sumaq_uywa/tree/main/backend/src/middlewares"
click node_backend_jobs "https://github.com/arcay322/sumaq_uywa/blob/main/backend/src/jobs/alertsScheduler.job.ts"
click node_backend_reports "https://github.com/arcay322/sumaq_uywa/tree/main/backend/src/services/reports"
click node_backend_dashboard "https://github.com/arcay322/sumaq_uywa/tree/main/backend/src/services/dashboard"
click node_backend_repro "https://github.com/arcay322/sumaq_uywa/tree/main/backend/src/services/reproduccion"
click node_backend_alerts "https://github.com/arcay322/sumaq_uywa/tree/main/backend/src/services/alerts"
click node_backend_auth "https://github.com/arcay322/sumaq_uywa/blob/main/backend/src/middlewares/auth.ts"
click node_backend_prisma "https://github.com/arcay322/sumaq_uywa/blob/main/backend/prisma/schema.prisma"
click node_backend_uploads "https://github.com/arcay322/sumaq_uywa/tree/main/backend/uploads/reports"
click node_backend_calendars "https://github.com/arcay322/sumaq_uywa/tree/main/backend/src/services/calendar"

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_pages,node_frontend_components,node_frontend_layouts,node_frontend_state,node_frontend_api,node_frontend_theme,node_frontend_access toneBlue
class node_backend_entry,node_backend_routes,node_backend_controllers,node_backend_services,node_backend_middleware,node_backend_jobs,node_backend_reports,node_backend_dashboard,node_backend_repro,node_backend_alerts,node_backend_auth,node_backend_calendars toneAmber
class node_backend_prisma,node_backend_uploads toneMint
```

## Components

### Frontend

React/Vite client boundary

- [App shell](https://github.com/arcay322/sumaq_uywa/blob/main/src/main.tsx): react-vite app · Top-level client entry and routing composition · `src/main.tsx`
- [Pages](https://github.com/arcay322/sumaq_uywa/tree/main/src/pages): screens · Domain screens for inventory, reproduction, reports, dashboard, auth · `src/pages`
- [Widgets](https://github.com/arcay322/sumaq_uywa/tree/main/src/components): ui components · Reusable tables, dialogs, dashboards, alerts, reports, and calendar widgets · `src/components`
- [Layouts](https://github.com/arcay322/sumaq_uywa/tree/main/src/layouts): shell · Main and sidebar layout structure · `src/layouts`
- [Client state](https://github.com/arcay322/sumaq_uywa/tree/main/src/contexts): contexts-hooks · Notifications, error handling, WebSocket, and delete confirmation hooks · `src/contexts`
- [API client](https://github.com/arcay322/sumaq_uywa/blob/main/src/services/api.ts): axios services · HTTP and notification service layer · `src/services/api.ts`
- [Theme](https://github.com/arcay322/sumaq_uywa/tree/main/src/theme): ui system · Shared presentation styles and Material UI theming · `src/theme`
- [Access control](https://github.com/arcay322/sumaq_uywa/blob/main/src/components/PrivateRoute.tsx): route guard · Private route protection in the UI · `src/components/PrivateRoute.tsx`

### Backend

Express API and domain services

- [Server entry](https://github.com/arcay322/sumaq_uywa/blob/main/backend/src/index.ts): express app · Backend bootstrap and server wiring · `backend/src/index.ts`
- [Routes](https://github.com/arcay322/sumaq_uywa/tree/main/backend/src/routes): http routes · Endpoint definitions across domain areas · `backend/src/routes`
- [Controllers](https://github.com/arcay322/sumaq_uywa/tree/main/backend/src/controllers): http controllers · Request/response adapters for each domain · `backend/src/controllers`
- [Services](https://github.com/arcay322/sumaq_uywa/tree/main/backend/src/services): domain services · Business logic for inventory, reproduction, sales, health, dashboard, alerts, and reports · `backend/src/services`
- [Middleware](https://github.com/arcay322/sumaq_uywa/tree/main/backend/src/middlewares): request guards · Auth, validation, cache, error handling, and websocket middleware · `backend/src/middlewares`
- [Jobs](https://github.com/arcay322/sumaq_uywa/blob/main/backend/src/jobs/alertsScheduler.job.ts): background work · Scheduled alert processing and operational jobs · `backend/src/jobs/alertsScheduler.job.ts`
- [Reports stack](https://github.com/arcay322/sumaq_uywa/tree/main/backend/src/services/reports): async exports · CSV, Excel, PDF generation, storage, lifecycle, monitoring, and downloads · `backend/src/services/reports`
- [Dashboard](https://github.com/arcay322/sumaq_uywa/tree/main/backend/src/services/dashboard): analytics layer · Metrics, charts, realtime aggregation, and dashboard orchestration · `backend/src/services/dashboard`
- [Reproduction](https://github.com/arcay322/sumaq_uywa/tree/main/backend/src/services/reproduccion): business domain · Preñez, camadas, compatibility, jaula capacity, and integration services · `backend/src/services/reproduccion`
- [Alerts](https://github.com/arcay322/sumaq_uywa/tree/main/backend/src/services/alerts): eventing domain · Alerts, notifications, and realtime broadcast services · `backend/src/services/alerts`
- [Auth](https://github.com/arcay322/sumaq_uywa/blob/main/backend/src/middlewares/auth.ts): jwt security · Authentication endpoint and guarded request flow · `backend/src/middlewares/auth.ts`
- [Calendar](https://github.com/arcay322/sumaq_uywa/tree/main/backend/src/services/calendar): scheduling domain · Operational events and recovery handling for reproduction planning · `backend/src/services/calendar`

### Data

Persistence and generated artifacts

- [Prisma](https://github.com/arcay322/sumaq_uywa/blob/main/backend/prisma/schema.prisma): orm persistence · Database client and schema layer over PostgreSQL · `backend/prisma/schema.prisma`
- [Report files](https://github.com/arcay322/sumaq_uywa/tree/main/backend/uploads/reports): artifact storage · Persistent generated report outputs · `backend/uploads/reports`

## Connections

- App shell → Pages: navigates · Routes render domain screens
- App shell → Layouts: wraps · Layouts provide the application shell
- Pages → Widgets: composes · Pages use shared widgets and tables
- Pages → API client: calls · Screens fetch and mutate through the API layer
- Widgets → Client state: uses · Widgets consume notification and websocket hooks
- Client state → API client: observes · Hooks coordinate API-facing client behavior
- Access control → Pages: protects · Private routes gate authenticated screens
- Theme → Widgets: styles · Shared theming shapes UI presentation
- Server entry → Routes: mounts · Server registers HTTP routes
- Routes → Controllers: dispatches · Routes forward requests to controllers
- Controllers → Services: delegates · Controllers hand off business logic
- Services → Prisma: persists · Services read and write via Prisma
- Middleware → Routes: guards · Auth and validation sit in front of endpoints
- Jobs → Alerts: triggers · Scheduled work drives alert processing
- Reports stack → Report files: stores · Generated exports land in file storage
- Reports stack → Prisma: reads · Report data is assembled from the database
- Dashboard → Services: aggregates · Dashboard pulls from operational domains
- Reproduction → Prisma: coordinates · Reproduction logic depends on persisted lifecycle data
- Alerts → Services: broadcasts · Notification flows are exposed through services
- Calendar → Reproduction: supports · Calendar events are tied to reproduction planning
- Widgets → Alerts: listens to · Notification widgets consume realtime updates
- Widgets → Dashboard: visualizes · Dashboard widgets present aggregated metrics
- Pages → Reports stack: requests · Report screens trigger export and history workflows
- Pages → Reproduction: operates on · Reproduction screens mirror the reproductive domain
- Pages → Calendar: plans with · Calendar UI coordinates operational events
- Auth → Routes: secures · JWT protection applies to protected API slices
