# saadhaq70/nearhelp002 architecture

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

- Interactive diagram: https://gitdiagram.com/saadhaq70/nearhelp002
- Repository: https://github.com/saadhaq70/nearhelp002

## Overview

#### 1. Purpose and deployment shape
NearHelp is a community emergency-response platform: users create or respond to SOS incidents, share live location, coordinate through chat, and receive AI guidance. It is a split deployment: a Next.js client and Express/Socket.io API service backed by managed PostgreSQL.
- Backend entry point: `backend/server.js`
- Frontend entry/layout: `frontend/app/page.tsx`, `frontend/app/layout.tsx`
- Deployment targets: Railway backend, Vercel frontend; NeonDB PostgreSQL.

#### 2. Client application and session boundary
The Next.js App Router client provides public, anonymous, authentication, and authenticated dashboard experiences. `AuthContext` and frontend middleware establish the browser-side session/routing boundary; API and socket adapters isolate calls to the backend.
- Routes: `frontend/app/anonymous/page.tsx`, `frontend/app/auth/login/page.tsx`, `frontend/app/dashboard/page.tsx`
- Session/network adapters: `frontend/context/AuthContext.tsx`, `frontend/lib/api.js`, `frontend/lib/socket.js`
- Route guard: `frontend/middleware.ts`

#### 3. Emergency command and dashboard UI
The dashboard is the main operational console: SOS creation, active-incident feed, responder views, maps, notifications, guardian/trust information, and AI assistance. Anonymous users have a separate map-driven SOS path, avoiding the authenticated dashboard workflow.
- SOS/dashboard surfaces: `frontend/components/dashboard/sos-button.tsx`, `frontend/components/dashboard/sos-feed-card.tsx`, `frontend/components/dashboard/live-map-card.tsx`
- Anonymous flow: `frontend/app/anonymous/anon-map.tsx`
- AI UI: `frontend/components/dashboard/ai-assistant-card.tsx`

#### 4. REST API, identity, and authorization
Express routes delegate to controllers, with JWT authentication and a distinct admin guard controlling protected mutations and administration. Auth manages signup/login/refresh/logout; user endpoints manage profile and location; admin endpoints govern users and SOS records.
- Route composition: `backend/server.js`
- Identity: `backend/routes/auth.js`, `backend/controllers/authController.js`, `backend/middleware/auth.js`
- Privileged access: `backend/routes/admin.js`, `backend/middleware/adminGuard.js`
- User/location API: `backend/routes/users.js`, `backend/controllers/userController.js`

#### 5. SOS incident lifecycle
SOS is the core domain flow: authenticated or anonymous creation produces an active incident, nearby users retrieve/respond, and resolution closes the incident. Controllers persist lifecycle state, while real-time handlers distribute incident changes to connected clients. Schema migrations explicitly address SOS schema, resolution fields, and stale/active cleanup.
- HTTP lifecycle: `backend/routes/sos.js`, `backend/controllers/sosController.js`
- Domain persistence: `backend/models/SOS.js`, `backend/prisma/schema.prisma`
- Lifecycle migrations: `backend/migrations/002_add_resolution_columns.sql`, `backend/migrations/003_cleanup_stale_sos.sql`

#### 6. Real-time coordination and location flow
Socket.io carries low-latency coordination distinct from request/response REST: chat messages, user location updates, and SOS events. Browser map/tracker components consume this live state; REST remains the authoritative persistence/control boundary.
- Socket registration: `backend/sockets/index.js`
- Event handlers: `backend/sockets/sosHandler.js`, `backend/sockets/locationHandler.js`, `backend/sockets/chatHandler.js`
- Client live location/map: `frontend/components/dashboard/location-tracker.tsx`, `frontend/components/dashboard/responder-live-view.tsx`

#### 7. Decision support, maps, and background operations
AI, keyword/resource lookup, geocoding, and routing services enrich emergency guidance and map/resource presentation. A scheduled welfare-check job is a separate asynchronous operational path. Leaflet renders maps client-side; Mistral is the external AI dependency.
- Services: `backend/services/aiService.js`, `backend/services/resourceService.js`, `backend/utils/routingEngine.js`
- Map/data integration: `backend/routes/map.js`, `backend/data/pointsOfInterest.js`, `frontend/components/dashboard/resource-leaflet-map.tsx`
- Background welfare flow: `backend/jobs/welfareCheck.js`, `backend/controllers/welfareController.js`

#### 8. Persistence and operational infrastructure
Prisma is the primary database abstraction over PostgreSQL/NeonDB, with legacy/adjacent model files for User, SOS, Rating, and WelfareCheck. Environment configuration supplies database, JWT, Mistral, CORS client URL, and port values. Backend is Node.js 20/Express; frontend is Next.js/React/TypeScript with Tailwind and Leaflet.
- Prisma/database setup: `backend/config/prisma.js`, `backend/config/db.js`, `backend/prisma/schema.prisma`
- Core entities: `backend/models/User.js`, `backend/models/Rating.js`, `backend/models/WelfareCheck.js`
- Runtime configuration template: `backend/.env.example`

Main entry points: [`backend/config/prisma.js`](https://github.com/saadhaq70/nearhelp002/blob/main/backend/config/prisma.js) (Prisma data layer), [`frontend/app/dashboard/page.tsx`](https://github.com/saadhaq70/nearhelp002/blob/main/frontend/app/dashboard/page.tsx) (Emergency dashboard), [`backend/server.js`](https://github.com/saadhaq70/nearhelp002/blob/main/backend/server.js) (Express API server), [`frontend/components/dashboard/location-tracker.tsx`](https://github.com/saadhaq70/nearhelp002/blob/main/frontend/components/dashboard/location-tracker.tsx) (Live maps & tracking)

## Diagram (Mermaid)

```mermaid
flowchart TD

subgraph group_client["Next.js client"]
  node_public["Public &amp; auth routes<br/>Next.js routes<br/>[page.tsx]"]
  node_session["Session &amp; route guard<br/>Auth boundary<br/>[AuthContext.tsx]"]
  node_api_adapter["API adapter<br/>HTTP client<br/>[api.js]"]
  node_socket_adapter["Socket adapter<br/>Socket client<br/>[socket.js]"]
  node_dashboard["Emergency dashboard<br/>Operational UI<br/>[page.tsx]"]
  node_anon_map["Anonymous SOS map<br/>Public UI<br/>[anon-map.tsx]"]
  node_live_ui["Live maps &amp; tracking<br/>Realtime UI"]
  node_ai_ui["AI assistant<br/>Dashboard UI"]
end

subgraph group_api["API service"]
  node_server{{"Express API server<br/>Node.js entry<br/>[server.js]"}}
  node_identity["Auth &amp; access control<br/>JWT middleware<br/>[auth.js]"]
  node_user_api["User &amp; location API<br/>REST controllers<br/>[userController.js]"]
  node_sos_api["SOS lifecycle API<br/>REST controller<br/>[sosController.js]"]
  node_realtime["Realtime event handlers<br/>Socket.io handlers<br/>[index.js]"]
  node_enrichment["Guidance &amp; map services<br/>Domain services<br/>[aiService.js]"]
  node_welfare_job["Welfare check job<br/>Background job<br/>[welfareCheck.js]"]
end

subgraph group_data["Data &amp; external"]
  node_prisma["Prisma data layer<br/>Database access<br/>[prisma.js]"]
  node_schema["Incident &amp; user schema<br/>Prisma schema<br/>[schema.prisma]"]
  node_postgres[("NeonDB PostgreSQL<br/>Managed database")]
  node_mistral["Mistral AI<br/>External AI"]
  node_leaflet["Leaflet maps<br/>Client map library"]
end

node_public -->|"establishes session"| node_session
node_session -->|"permits protected route"| node_dashboard
node_dashboard -->|"REST operations"| node_api_adapter
node_anon_map -->|"anonymous SOS"| node_api_adapter
node_dashboard -->|"subscribes live state"| node_socket_adapter
node_live_ui -->|"location updates"| node_socket_adapter
node_dashboard -->|"renders"| node_live_ui
node_dashboard -->|"hosts"| node_ai_ui
node_live_ui -->|"renders maps"| node_leaflet
node_api_adapter -->|"HTTP"| node_server
node_socket_adapter -->|"Socket.io"| node_realtime
node_server -->|"protects routes"| node_identity
node_server -->|"user routes"| node_user_api
node_server -->|"SOS routes"| node_sos_api
node_server -->|"AI and map routes"| node_enrichment
node_sos_api -->|"persists lifecycle"| node_prisma
node_user_api -->|"persists profile/location"| node_prisma
node_welfare_job -->|"checks welfare state"| node_prisma
node_sos_api -->|"publishes incidents"| node_realtime
node_realtime -.->|"persists live events"| node_prisma
node_enrichment -->|"AI guidance"| node_mistral
node_prisma -->|"uses models"| node_schema
node_prisma -->|"queries"| node_postgres

click node_public "https://github.com/saadhaq70/nearhelp002/blob/main/frontend/app/anonymous/page.tsx"
click node_session "https://github.com/saadhaq70/nearhelp002/blob/main/frontend/context/AuthContext.tsx"
click node_api_adapter "https://github.com/saadhaq70/nearhelp002/blob/main/frontend/lib/api.js"
click node_socket_adapter "https://github.com/saadhaq70/nearhelp002/blob/main/frontend/lib/socket.js"
click node_dashboard "https://github.com/saadhaq70/nearhelp002/blob/main/frontend/app/dashboard/page.tsx"
click node_anon_map "https://github.com/saadhaq70/nearhelp002/blob/main/frontend/app/anonymous/anon-map.tsx"
click node_live_ui "https://github.com/saadhaq70/nearhelp002/blob/main/frontend/components/dashboard/location-tracker.tsx"
click node_ai_ui "https://github.com/saadhaq70/nearhelp002/blob/main/frontend/components/dashboard/ai-assistant-card.tsx"
click node_server "https://github.com/saadhaq70/nearhelp002/blob/main/backend/server.js"
click node_identity "https://github.com/saadhaq70/nearhelp002/blob/main/backend/middleware/auth.js"
click node_user_api "https://github.com/saadhaq70/nearhelp002/blob/main/backend/controllers/userController.js"
click node_sos_api "https://github.com/saadhaq70/nearhelp002/blob/main/backend/controllers/sosController.js"
click node_realtime "https://github.com/saadhaq70/nearhelp002/blob/main/backend/sockets/index.js"
click node_enrichment "https://github.com/saadhaq70/nearhelp002/blob/main/backend/services/aiService.js"
click node_welfare_job "https://github.com/saadhaq70/nearhelp002/blob/main/backend/jobs/welfareCheck.js"
click node_prisma "https://github.com/saadhaq70/nearhelp002/blob/main/backend/config/prisma.js"
click node_schema "https://github.com/saadhaq70/nearhelp002/blob/main/backend/prisma/schema.prisma"

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_public,node_session,node_api_adapter,node_socket_adapter,node_dashboard,node_anon_map,node_live_ui,node_ai_ui toneBlue
class node_server,node_identity,node_user_api,node_sos_api,node_realtime,node_enrichment,node_welfare_job toneAmber
class node_prisma,node_schema,node_postgres,node_mistral,node_leaflet toneMint
```

## Components

### Next.js client

- [Public & auth routes](https://github.com/saadhaq70/nearhelp002/blob/main/frontend/app/anonymous/page.tsx): Next.js routes · `frontend/app/anonymous/page.tsx`
- [Session & route guard](https://github.com/saadhaq70/nearhelp002/blob/main/frontend/context/AuthContext.tsx): Auth boundary · `frontend/context/AuthContext.tsx`
- [API adapter](https://github.com/saadhaq70/nearhelp002/blob/main/frontend/lib/api.js): HTTP client · `frontend/lib/api.js`
- [Socket adapter](https://github.com/saadhaq70/nearhelp002/blob/main/frontend/lib/socket.js): Socket client · `frontend/lib/socket.js`
- [Emergency dashboard](https://github.com/saadhaq70/nearhelp002/blob/main/frontend/app/dashboard/page.tsx): Operational UI · Authenticated SOS, feed, maps, responder, and notification surfaces. · `frontend/app/dashboard/page.tsx`
- [Anonymous SOS map](https://github.com/saadhaq70/nearhelp002/blob/main/frontend/app/anonymous/anon-map.tsx): Public UI · `frontend/app/anonymous/anon-map.tsx`
- [Live maps & tracking](https://github.com/saadhaq70/nearhelp002/blob/main/frontend/components/dashboard/location-tracker.tsx): Realtime UI · `frontend/components/dashboard/location-tracker.tsx`
- [AI assistant](https://github.com/saadhaq70/nearhelp002/blob/main/frontend/components/dashboard/ai-assistant-card.tsx): Dashboard UI · `frontend/components/dashboard/ai-assistant-card.tsx`

### API service

- [Express API server](https://github.com/saadhaq70/nearhelp002/blob/main/backend/server.js): Node.js entry · Composes REST routes and Socket.io. · `backend/server.js`
- [Auth & access control](https://github.com/saadhaq70/nearhelp002/blob/main/backend/middleware/auth.js): JWT middleware · Authentication plus separate admin authorization. · `backend/middleware/auth.js`
- [User & location API](https://github.com/saadhaq70/nearhelp002/blob/main/backend/controllers/userController.js): REST controllers · `backend/controllers/userController.js`
- [SOS lifecycle API](https://github.com/saadhaq70/nearhelp002/blob/main/backend/controllers/sosController.js): REST controller · Creates, responds to, retrieves, and resolves incidents. · `backend/controllers/sosController.js`
- [Realtime event handlers](https://github.com/saadhaq70/nearhelp002/blob/main/backend/sockets/index.js): Socket.io handlers · Distributes SOS, location, and chat events. · `backend/sockets/index.js`
- [Guidance & map services](https://github.com/saadhaq70/nearhelp002/blob/main/backend/services/aiService.js): Domain services · AI guidance, resources, geocoding, and routing enrichment. · `backend/services/aiService.js`
- [Welfare check job](https://github.com/saadhaq70/nearhelp002/blob/main/backend/jobs/welfareCheck.js): Background job · Scheduled operational welfare workflow. · `backend/jobs/welfareCheck.js`

### Data & external

- [Prisma data layer](https://github.com/saadhaq70/nearhelp002/blob/main/backend/config/prisma.js): Database access · `backend/config/prisma.js`
- [Incident & user schema](https://github.com/saadhaq70/nearhelp002/blob/main/backend/prisma/schema.prisma): Prisma schema · Defines persisted SOS, user, rating, and welfare data. · `backend/prisma/schema.prisma`
- **NeonDB PostgreSQL**: Managed database
- **Mistral AI**: External AI
- **Leaflet maps**: Client map library

## Connections

- Public & auth routes → Session & route guard: establishes session
- Session & route guard → Emergency dashboard: permits protected route
- Emergency dashboard → API adapter: REST operations
- Anonymous SOS map → API adapter: anonymous SOS
- Emergency dashboard → Socket adapter: subscribes live state
- Live maps & tracking → Socket adapter: location updates
- Emergency dashboard → Live maps & tracking: renders
- Emergency dashboard → AI assistant: hosts
- Live maps & tracking → Leaflet maps: renders maps
- API adapter → Express API server: HTTP
- Socket adapter → Realtime event handlers: Socket.io
- Express API server → Auth & access control: protects routes
- Express API server → User & location API: user routes
- Express API server → SOS lifecycle API: SOS routes
- Express API server → Guidance & map services: AI and map routes
- SOS lifecycle API → Prisma data layer: persists lifecycle
- User & location API → Prisma data layer: persists profile/location
- Welfare check job → Prisma data layer: checks welfare state
- SOS lifecycle API → Realtime event handlers: publishes incidents
- Realtime event handlers → Prisma data layer: persists live events
- Guidance & map services → Mistral AI: AI guidance
- Prisma data layer → Incident & user schema: uses models
- Prisma data layer → NeonDB PostgreSQL: queries
