How amilcar1129/agrosmart2.0-backend works
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: index.js Model registry · auth.js Auth guard · server.js Server entry · auth.controller.js Auth controller
Components 19
HTTP API
- Server entry · Mounts domain route groups.
server.js - Auth routes
routes/auth.routes.js - Admin routes
routes/admin.routes.js - Community routes
routes/community.routes.js - Public routes
routes/public.routes.js - Survey routes
routes/survey.routes.js
Application Control
- Auth guard · Gates restricted endpoints before controllers.
middleware/auth.js - Auth controller
controllers/auth.controller.js - Admin controller
controllers/admin.controller.js - Community controller
controllers/community.controller.js - Public controller
controllers/public.controller.js - Survey controller
controllers/survey.controller.js - Password hashing
hash-password.js
Domain & Persistence
- Model registry · Collects domain entities for controller access.
models/index.js - Users, communities & families · Identity, community membership, and household records.
models/usuario.model.js - Survey & socioeconomic records · Household survey and socioeconomic data.
models/encuesta.model.js - Crop & animal records · Agricultural data supplementing household records.
models/cultivo.model.js
Infrastructure
- Database connection
config/database.js - Upload handling · General and gallery upload parsing/storage.
config/multer.js
Connections 24
- 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