How saadhaq70/nearhelp002 works
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: prisma.js Prisma data layer · page.tsx Emergency dashboard · server.js Express API server · location-tracker.tsx Live maps & tracking
Components 20
Next.js client
- Public & auth routes
frontend/app/anonymous/page.tsx - Session & route guard
frontend/context/AuthContext.tsx - API adapter
frontend/lib/api.js - Socket adapter
frontend/lib/socket.js - Emergency dashboard · Authenticated SOS, feed, maps, responder, and notification surfaces.
frontend/app/dashboard/page.tsx - Anonymous SOS map
frontend/app/anonymous/anon-map.tsx - Live maps & tracking
frontend/components/dashboard/location-tracker.tsx - AI assistant
frontend/components/dashboard/ai-assistant-card.tsx
API service
- Express API server · Composes REST routes and Socket.io.
backend/server.js - Auth & access control · Authentication plus separate admin authorization.
backend/middleware/auth.js - User & location API
backend/controllers/userController.js - SOS lifecycle API · Creates, responds to, retrieves, and resolves incidents.
backend/controllers/sosController.js - Realtime event handlers · Distributes SOS, location, and chat events.
backend/sockets/index.js - Guidance & map services · AI guidance, resources, geocoding, and routing enrichment.
backend/services/aiService.js - Welfare check job · Scheduled operational welfare workflow.
backend/jobs/welfareCheck.js
Data & external
- Prisma data layer
backend/config/prisma.js - Incident & user schema · Defines persisted SOS, user, rating, and welfare data.
backend/prisma/schema.prisma - NeonDB PostgreSQL
- Mistral AI
- Leaflet maps
Connections 23
- 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