# amilcar1129/agrosmart2.0-backend architecture

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

- Interactive diagram: https://gitdiagram.com/amilcar1129/agrosmart2.0-backend
- Repository: https://github.com/amilcar1129/agrosmart2.0-backend

## Overview

#### Purpose and entry point
This is a Node.js backend organized as an MVC-style API for public, authentication, administration, community, and survey workflows. The process starts at `server.js`, which mounts route groups and connects request handling to controllers.

#### HTTP/API boundary
Route modules divide the API by functional domain:
- Authentication: `routes/auth.routes.js`
- Administrative operations: `routes/admin.routes.js`
- Community operations: `routes/community.routes.js`
- Public-facing access: `routes/public.routes.js`
- Survey capture/querying: `routes/survey.routes.js`

Requests flow from these route definitions into their paired controllers rather than directly accessing persistence.

#### Application control layer
Controllers implement domain-oriented request orchestration, validation/response behavior, and model access:
- `controllers/auth.controller.js`
- `controllers/admin.controller.js`
- `controllers/community.controller.js`
- `controllers/public.controller.js`
- `controllers/survey.controller.js`

The main control boundary is route → controller → model/database. Controller separation suggests public reads, privileged administration, community management, and survey handling have distinct API policies.

#### Authentication and authorization
Authentication logic is centralized in `controllers/auth.controller.js`, while protected-route enforcement is separated into `middleware/auth.js`. Password hashing is isolated in `hash-password.js`.

This creates an important security boundary: authentication establishes user identity, and route middleware should gate administrative or otherwise restricted controller actions before model access.

#### Domain and persistence model
The persistence layer is collected through `models/index.js` and contains entities for users, communities, households/families, surveys, socioeconomic records, crops, and animals:
- `models/usuario.model.js`, `models/comunidad.model.js`, `models/familia.model.js`
- `models/encuesta.model.js`, `models/socioeconomico.model.js`
- `models/cultivo.model.js`, `models/animal.model.js`

These models likely represent the core data graph: users administer or participate in communities; families have survey and socioeconomic records; agricultural and animal records supplement household/community survey data.

#### Database and uploads infrastructure
Database connection/configuration is isolated at `config/database.js`, keeping persistence infrastructure outside request handlers. File-upload handling is configured separately for general uploads and gallery-oriented uploads:
- `config/multer.js`
- `config/multer-gallery.js`

Multer is the material upload integration implied by these modules. Upload parsing/storage is therefore an infrastructure boundary that controllers or routes can invoke before persisting associated domain records.

#### Primary flow
A typical request enters `server.js`, matches a domain router, passes through `middleware/auth.js` when restricted, optionally uses Multer upload configuration, then reaches a controller. The controller coordinates the relevant models via `models/index.js` and returns an HTTP response. Database configuration remains behind `config/database.js`, while password handling remains behind `hash-password.js`.

Main entry points: [`models/index.js`](https://github.com/amilcar1129/agrosmart2.0-backend/blob/master/models/index.js) (Model registry), [`middleware/auth.js`](https://github.com/amilcar1129/agrosmart2.0-backend/blob/master/middleware/auth.js) (Auth guard), [`server.js`](https://github.com/amilcar1129/agrosmart2.0-backend/blob/master/server.js) (Server entry), [`controllers/auth.controller.js`](https://github.com/amilcar1129/agrosmart2.0-backend/blob/master/controllers/auth.controller.js) (Auth controller)

## Diagram (Mermaid)

```mermaid
flowchart TD

subgraph group_api["HTTP API"]
  node_server["Server entry<br/>Node.js server<br/>[server.js]"]
  node_auth_routes["Auth routes<br/>router<br/>[auth.routes.js]"]
  node_admin_routes["Admin routes<br/>router<br/>[admin.routes.js]"]
  node_community_routes["Community routes<br/>router"]
  node_public_routes["Public routes<br/>router<br/>[public.routes.js]"]
  node_survey_routes["Survey routes<br/>router<br/>[survey.routes.js]"]
end

subgraph group_control["Application Control"]
  node_auth_middleware{{"Auth guard<br/>middleware<br/>[auth.js]"}}
  node_auth_controller["Auth controller<br/>[auth.controller.js]"]
  node_admin_controller["Admin controller"]
  node_community_controller["Community controller"]
  node_public_controller["Public controller"]
  node_survey_controller["Survey controller"]
  node_password_hashing["Password hashing<br/>security utility<br/>[hash-password.js]"]
end

subgraph group_domain["Domain &amp; Persistence"]
  node_models["Model registry<br/>model layer<br/>[index.js]"]
  node_core_entities["Users, communities &amp; families<br/>domain entities<br/>[usuario.model.js]"]
  node_survey_entities["Survey &amp; socioeconomic records<br/>domain entities<br/>[encuesta.model.js]"]
  node_agriculture_entities["Crop &amp; animal records<br/>domain entities<br/>[cultivo.model.js]"]
end

subgraph group_infra["Infrastructure"]
  node_database[("Database connection<br/>database config<br/>[database.js]")]
  node_uploads["Upload handling<br/>Multer configuration<br/>[multer.js]"]
end

node_server -->|"mounts"| node_auth_routes
node_server -->|"mounts"| node_admin_routes
node_server -->|"mounts"| node_community_routes
node_server -->|"mounts"| node_public_routes
node_server -->|"mounts"| node_survey_routes
node_auth_routes -->|"dispatches"| node_auth_controller
node_admin_routes -->|"protected by"| node_auth_middleware
node_community_routes -->|"protected by"| node_auth_middleware
node_survey_routes -->|"protected by"| node_auth_middleware
node_auth_middleware -.->|"permits"| node_admin_controller
node_auth_middleware -.->|"permits"| node_community_controller
node_auth_middleware -.->|"permits"| node_survey_controller
node_public_routes -->|"dispatches"| node_public_controller
node_auth_controller -->|"hashes passwords"| node_password_hashing
node_auth_controller -->|"accesses"| node_models
node_admin_controller -->|"accesses"| node_models
node_community_controller -->|"accesses"| node_models
node_public_controller -->|"reads"| node_models
node_survey_controller -.->|"accepts uploads"| node_uploads
node_survey_controller -->|"accesses"| node_models
node_models -->|"includes"| node_core_entities
node_models -->|"includes"| node_survey_entities
node_models -->|"includes"| node_agriculture_entities
node_models -->|"connects through"| node_database

click node_server "https://github.com/amilcar1129/agrosmart2.0-backend/blob/master/server.js"
click node_auth_routes "https://github.com/amilcar1129/agrosmart2.0-backend/blob/master/routes/auth.routes.js"
click node_admin_routes "https://github.com/amilcar1129/agrosmart2.0-backend/blob/master/routes/admin.routes.js"
click node_community_routes "https://github.com/amilcar1129/agrosmart2.0-backend/blob/master/routes/community.routes.js"
click node_public_routes "https://github.com/amilcar1129/agrosmart2.0-backend/blob/master/routes/public.routes.js"
click node_survey_routes "https://github.com/amilcar1129/agrosmart2.0-backend/blob/master/routes/survey.routes.js"
click node_auth_middleware "https://github.com/amilcar1129/agrosmart2.0-backend/blob/master/middleware/auth.js"
click node_auth_controller "https://github.com/amilcar1129/agrosmart2.0-backend/blob/master/controllers/auth.controller.js"
click node_admin_controller "https://github.com/amilcar1129/agrosmart2.0-backend/blob/master/controllers/admin.controller.js"
click node_community_controller "https://github.com/amilcar1129/agrosmart2.0-backend/blob/master/controllers/community.controller.js"
click node_public_controller "https://github.com/amilcar1129/agrosmart2.0-backend/blob/master/controllers/public.controller.js"
click node_survey_controller "https://github.com/amilcar1129/agrosmart2.0-backend/blob/master/controllers/survey.controller.js"
click node_password_hashing "https://github.com/amilcar1129/agrosmart2.0-backend/blob/master/hash-password.js"
click node_models "https://github.com/amilcar1129/agrosmart2.0-backend/blob/master/models/index.js"
click node_core_entities "https://github.com/amilcar1129/agrosmart2.0-backend/blob/master/models/usuario.model.js"
click node_survey_entities "https://github.com/amilcar1129/agrosmart2.0-backend/blob/master/models/encuesta.model.js"
click node_agriculture_entities "https://github.com/amilcar1129/agrosmart2.0-backend/blob/master/models/cultivo.model.js"
click node_database "https://github.com/amilcar1129/agrosmart2.0-backend/blob/master/config/database.js"
click node_uploads "https://github.com/amilcar1129/agrosmart2.0-backend/blob/master/config/multer.js"

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_server,node_auth_routes,node_admin_routes,node_community_routes,node_public_routes,node_survey_routes toneBlue
class node_auth_middleware,node_auth_controller,node_admin_controller,node_community_controller,node_public_controller,node_survey_controller,node_password_hashing toneAmber
class node_models,node_core_entities,node_survey_entities,node_agriculture_entities toneMint
class node_database,node_uploads toneRose
```

## Components

### HTTP API

- [Server entry](https://github.com/amilcar1129/agrosmart2.0-backend/blob/master/server.js): Node.js server · Mounts domain route groups. · `server.js`
- [Auth routes](https://github.com/amilcar1129/agrosmart2.0-backend/blob/master/routes/auth.routes.js): router · `routes/auth.routes.js`
- [Admin routes](https://github.com/amilcar1129/agrosmart2.0-backend/blob/master/routes/admin.routes.js): router · `routes/admin.routes.js`
- [Community routes](https://github.com/amilcar1129/agrosmart2.0-backend/blob/master/routes/community.routes.js): router · `routes/community.routes.js`
- [Public routes](https://github.com/amilcar1129/agrosmart2.0-backend/blob/master/routes/public.routes.js): router · `routes/public.routes.js`
- [Survey routes](https://github.com/amilcar1129/agrosmart2.0-backend/blob/master/routes/survey.routes.js): router · `routes/survey.routes.js`

### Application Control

- [Auth guard](https://github.com/amilcar1129/agrosmart2.0-backend/blob/master/middleware/auth.js): middleware · Gates restricted endpoints before controllers. · `middleware/auth.js`
- [Auth controller](https://github.com/amilcar1129/agrosmart2.0-backend/blob/master/controllers/auth.controller.js): controller · `controllers/auth.controller.js`
- [Admin controller](https://github.com/amilcar1129/agrosmart2.0-backend/blob/master/controllers/admin.controller.js): controller · `controllers/admin.controller.js`
- [Community controller](https://github.com/amilcar1129/agrosmart2.0-backend/blob/master/controllers/community.controller.js): controller · `controllers/community.controller.js`
- [Public controller](https://github.com/amilcar1129/agrosmart2.0-backend/blob/master/controllers/public.controller.js): controller · `controllers/public.controller.js`
- [Survey controller](https://github.com/amilcar1129/agrosmart2.0-backend/blob/master/controllers/survey.controller.js): controller · `controllers/survey.controller.js`
- [Password hashing](https://github.com/amilcar1129/agrosmart2.0-backend/blob/master/hash-password.js): security utility · `hash-password.js`

### Domain & Persistence

- [Model registry](https://github.com/amilcar1129/agrosmart2.0-backend/blob/master/models/index.js): model layer · Collects domain entities for controller access. · `models/index.js`
- [Users, communities & families](https://github.com/amilcar1129/agrosmart2.0-backend/blob/master/models/usuario.model.js): domain entities · Identity, community membership, and household records. · `models/usuario.model.js`
- [Survey & socioeconomic records](https://github.com/amilcar1129/agrosmart2.0-backend/blob/master/models/encuesta.model.js): domain entities · Household survey and socioeconomic data. · `models/encuesta.model.js`
- [Crop & animal records](https://github.com/amilcar1129/agrosmart2.0-backend/blob/master/models/cultivo.model.js): domain entities · Agricultural data supplementing household records. · `models/cultivo.model.js`

### Infrastructure

- [Database connection](https://github.com/amilcar1129/agrosmart2.0-backend/blob/master/config/database.js): database config · `config/database.js`
- [Upload handling](https://github.com/amilcar1129/agrosmart2.0-backend/blob/master/config/multer.js): Multer configuration · General and gallery upload parsing/storage. · `config/multer.js`

## Connections

- Server entry → Auth routes: mounts
- Server entry → Admin routes: mounts
- Server entry → Community routes: mounts
- Server entry → Public routes: mounts
- Server entry → Survey routes: mounts
- Auth routes → Auth controller: dispatches
- Admin routes → Auth guard: protected by
- Community routes → Auth guard: protected by
- Survey routes → Auth guard: protected by
- Auth guard → Admin controller: permits
- Auth guard → Community controller: permits
- Auth guard → Survey controller: permits
- Public routes → Public controller: dispatches
- Auth controller → Password hashing: hashes passwords
- Auth controller → Model registry: accesses
- Admin controller → Model registry: accesses
- Community controller → Model registry: accesses
- Public controller → Model registry: reads
- Survey controller → Upload handling: accepts uploads
- Survey controller → Model registry: accesses
- Model registry → Users, communities & families: includes
- Model registry → Survey & socioeconomic records: includes
- Model registry → Crop & animal records: includes
- Model registry → Database connection: connects through
