Commit 0ea88fa
Backend API Core: OpenAI Integration, Rewards System & Analytics (#2)
* Implement Backend API Core with Authentication for CivicPulse
This commit implements the complete backend API infrastructure for the CivicPulse hackathon project, including:
Features implemented:
- Fastify server with JWT authentication
- User registration and login endpoints
- Protected route middleware
- User profile management (GET, PATCH)
- Health check endpoint with database connectivity test
- Prisma ORM integration with PostgreSQL
Technical stack:
- Node.js + TypeScript
- Fastify web framework
- Prisma ORM
- bcryptjs for password hashing
- JWT for authentication
- CORS enabled for frontend integration
Authentication flow:
- POST /api/auth/register - Create new user with hashed password
- POST /api/auth/login - Authenticate and receive JWT token
- GET /api/auth/me - Get current user (requires authentication)
- JWT tokens valid for 7 days
User management:
- GET /api/users/:id - Get any user's public profile
- PATCH /api/users/:id - Update own profile (authenticated)
- Authorization checks prevent cross-user updates
Testing:
- Comprehensive integration test suite (10 tests, all passing)
- Tests cover: registration, login, authentication, authorization, profile updates
- Health check verifies database connectivity
Integration ready:
- CORS configured for localhost:5173, localhost:5174
- Environment variables documented in .env.example
- Prisma schema shared with database worktree
- All endpoints tested and functional
🤖 Generated with [Claude Code](https://claude.com/claude-code)
Co-Authored-By: Claude <noreply@anthropic.com>
* Add comprehensive backend API documentation
* Add all remaining backend API endpoints with AWS integration
Implemented complete REST API for CivicPulse platform:
Issues Endpoints:
- GET /api/issues - List with filters (status, priority, category)
- GET /api/issues/:id - Get single issue
- POST /api/issues - Create with photo upload to S3
- PATCH /api/issues/:id/status - Update status
- Integrated AWS Bedrock AI for automatic categorization
Activities Endpoints:
- GET /api/activities - List with filters
- POST /api/activities - Log activity with carbon calculations
- GET /api/activities/user/:id - User activity history
- Automatic points calculation (10 pts per kg CO2)
Challenges Endpoints:
- GET /api/challenges - All challenges
- GET /api/challenges/active - Active only
- POST /api/challenges/join/:id - Join challenge
- POST /api/challenges/complete/:id - Complete and award points
- GET /api/challenges/ai-suggestions - AI-generated suggestions
Neighborhoods Endpoints:
- GET /api/neighborhoods - List all
- GET /api/neighborhoods/:id/stats - Detailed statistics
Leaderboard Endpoints:
- GET /api/leaderboard/users - Top users by points
- GET /api/leaderboard/neighborhoods - Top by avg points
Analytics Endpoints:
- GET /api/analytics/patterns - Issue patterns
- GET /api/analytics/carbon - Carbon savings data
- GET /api/analytics/engagement - User engagement metrics
Technical Implementation:
- AWS S3 integration for photo uploads
- AWS Bedrock (Claude 4.5) for AI features
- Multipart form data support
- Carbon calculation algorithms
- Proper error handling and validation
- TypeScript type safety throughout
Documentation:
- Complete API endpoint documentation
- Request/response examples
- Error handling guide
All endpoints tested and functional.
🤖 Generated with [Claude Code](https://claude.com/claude-code)
Co-Authored-By: Claude <noreply@anthropic.com>
* Add comprehensive location validation for Calgary-based civic issues
Implemented robust coordinate validation and Calgary bounds checking to ensure all reported issues have accurate geographic data for map visualization.
Features:
- Enforces lat,lng coordinate format for all issue submissions
- Validates coordinates are within Calgary city bounds (50.842-51.247 lat, -114.271--113.873 lng)
- Stores locationLat, locationLng, and optional locationAddress
- Returns clear error messages for out-of-bounds or invalid coordinates
- Added detailed logging for location data debugging
Integration points:
- Works with frontend geolocation and map selection
- Ensures all issues can appear on admin map visualization
- Supports optional descriptive address field
Generated with [Claude Code](https://claude.com/claude-code)
Co-Authored-By: Claude <noreply@anthropic.com>
* Add AI-Powered Recommendations API endpoint
Implement GET /api/analytics/recommendations endpoint that returns 5 detailed
AI-generated insights with comprehensive data justifications.
Features:
- Returns 5 pre-analyzed recommendations from Calgary real data
- Each recommendation includes:
- title, description, impact, metric, type
- dataJustification: detailed explanation of analysis
- affectedItems: array of issue IDs or affected entities
- Types: carbon, efficiency, engagement, infrastructure
- Graceful fallback if data file not found
- Fast performance (~0.3ms response time)
Perfect for demo - showcases AI capabilities with real data-driven insights.
🤖 Generated with [Claude Code](https://claude.com/claude-code)
Co-Authored-By: Claude <noreply@anthropic.com>
* Implement geographic clustering API with AI-powered route optimization
Added two new endpoints for CivicPulse civic issue clustering:
1. GET /api/analytics/clusters
- Groups open issues within 500m radius using Haversine distance
- Generates AI-powered route optimization analysis via AWS Bedrock
- Calculates estimated completion time and carbon savings
- Returns nearest-neighbor route order prioritizing safety-critical issues
2. POST /api/analytics/accept-route
- Accepts optimized route for a cluster
- Updates all issues to "in_progress" status
- Assigns issues to Public Works department
- Supports optional issue_ids array for targeted updates
Technical implementation:
- Created new routes/clusters.ts with geographic clustering logic
- Registered clustersRoutes in main server index
- Uses existing Bedrock AI integration for route analysis
- Fallback handling for AI failures with default estimations
- Comprehensive error handling and logging
Clustering algorithm:
- Simple distance-based approach (CLUSTER_RADIUS_KM = 0.5)
- Haversine formula for accurate geographic distance
- Center calculation using average lat/lng
- Neighborhood name extraction from address data
Route optimization:
- Nearest-neighbor algorithm for route sequencing
- Priority-based starting point (critical/high issues first)
- Carbon savings estimation (2.5kg per bundled issue)
- Time estimation from issue resolution hours or 30min default
Generated with Claude Code
Co-Authored-By: Claude <noreply@anthropic.com>
* Add comprehensive backend features for CivicPulse
- Implemented OpenAI GPT-4o-mini integration for AI-powered analytics
- Added cluster analysis with real-time AI recommendations
- Created rewards redemption system with points management
- Built weekly activity tracking and progress monitoring
- Added AI-powered challenge generation for admins
- Implemented issue reporting tracking and carbon savings calculations
- Enhanced analytics routes with comprehensive logging
- Added user activity history endpoints
- Created challenge creation and management endpoints
🤖 Generated with [Claude Code](https://claude.com/claude-code)
Co-Authored-By: Claude <noreply@anthropic.com>
---------
Co-authored-by: Claude <noreply@anthropic.com>1 parent f7b0dc9 commit 0ea88fa
File tree
17,456 files changed
+3053062
-0
lines changed- backend
- node_modules
- .bin
- .prisma/client
- @aws-crypto
- crc32c
- build
- main
- module
- src
- crc32
- build
- main
- module
- src
- sha1-browser
- build
- main
- module
- node_modules/@smithy
- is-array-buffer
- dist-cjs
- dist-es
- dist-types
- ts3.4
- util-buffer-from
- dist-cjs
- dist-es
- dist-types
- ts3.4
- util-utf8
- dist-cjs
- dist-es
- dist-types
- ts3.4
- src
- sha256-browser
- build
- main
- module
- node_modules/@smithy
- is-array-buffer
- dist-cjs
- dist-es
- dist-types
- ts3.4
- util-buffer-from
- dist-cjs
- dist-es
- dist-types
- ts3.4
- util-utf8
- dist-cjs
- dist-es
- dist-types
- ts3.4
- src
- sha256-js
- build
- main
- module
- src
- supports-web-crypto
- build
- main
- module
- src
- util
- build
- main
- module
- node_modules/@smithy
- is-array-buffer
- dist-cjs
- dist-es
- dist-types
- ts3.4
- util-buffer-from
- dist-cjs
- dist-es
- dist-types
- ts3.4
- util-utf8
- dist-cjs
- dist-es
- dist-types
- ts3.4
- src
- @aws-sdk
- client-bedrock-runtime
- dist-cjs
- auth
- endpoint
- dist-es
- auth
- commands
- endpoint
- models
- pagination
- protocols
- dist-types
- auth
- commands
- endpoint
- models
- pagination
- protocols
- ts3.4
- auth
- commands
- endpoint
- models
- pagination
- protocols
- client-s3
- dist-cjs
- auth
- endpoint
- dist-es
- auth
- commands
- endpoint
- models
- pagination
- protocols
- waiters
- dist-types
- auth
- commands
- endpoint
- models
- pagination
- protocols
- ts3.4
- auth
- commands
- endpoint
- models
- pagination
- protocols
- waiters
- waiters
- client-sso
- dist-cjs
- auth
- endpoint
- dist-es
- auth
- commands
- endpoint
- models
- pagination
- protocols
- dist-types
- auth
- commands
- endpoint
- models
- pagination
- protocols
- ts3.4
- auth
- commands
- endpoint
- models
- pagination
- protocols
- core
- dist-cjs
- submodules
- account-id-endpoint
- client
- httpAuthSchemes
- protocols
- dist-es
- submodules
- account-id-endpoint
- client
- httpAuthSchemes
- aws_sdk
- utils
- protocols
- cbor
- json
- experimental
- query
- xml
- dist-types
- submodules
- account-id-endpoint
- client
- httpAuthSchemes
- aws_sdk
- utils
- protocols
- cbor
- json
- experimental
- query
- xml
- ts3.4
- submodules
- account-id-endpoint
- client
- httpAuthSchemes
- aws_sdk
- utils
- protocols
- cbor
- json
- experimental
- query
- xml
- credential-provider-env
- dist-cjs
- dist-es
- dist-types
- ts3.4
- credential-provider-http
- dist-cjs
- fromHttp
- dist-es
- fromHttp
- dist-types
- fromHttp
- ts3.4
- fromHttp
- credential-provider-ini
- dist-cjs
- dist-es
- dist-types
- ts3.4
- credential-provider-node
- dist-cjs
- dist-es
- runtime
- dist-types
- runtime
- ts3.4
- runtime
- credential-provider-process
- dist-cjs
- dist-es
- dist-types
- ts3.4
- credential-provider-sso
- dist-cjs
- dist-es
- dist-types
- ts3.4
- credential-provider-web-identity
- dist-cjs
- dist-es
- dist-types
- ts3.4
- eventstream-handler-node
- dist-cjs
- dist-es
- dist-types
- ts3.4
- middleware-bucket-endpoint
- dist-cjs
- dist-es
- dist-types
- ts3.4
- middleware-eventstream
- dist-cjs
- dist-es
- dist-types
- ts3.4
- middleware-expect-continue
- dist-cjs
- dist-es
- dist-types
- ts3.4
- middleware-flexible-checksums
- dist-cjs
- dist-es
- dist-types
- ts3.4
- middleware-host-header
- dist-cjs
- dist-es
- dist-types
- ts3.4
- middleware-location-constraint
- dist-cjs
- dist-es
- dist-types
- ts3.4
- middleware-logger
- dist-cjs
- dist-es
- dist-types
- ts3.4
- middleware-recursion-detection
- dist-cjs
- dist-es
- dist-types
- ts3.4
- middleware-sdk-s3
- dist-cjs
- dist-es
- s3-express
- classes
- functions
- interfaces
- dist-types
- s3-express
- classes
- functions
- interfaces
- ts3.4
- s3-express
- classes
- functions
- interfaces
- middleware-ssec
- dist-cjs
- dist-es
- dist-types
- ts3.4
- middleware-user-agent
- dist-cjs
- dist-es
- dist-types
- ts3.4
- middleware-websocket
- dist-cjs
- dist-es
- dist-types
- ts3.4
- nested-clients
- dist-cjs
- submodules
- sso-oidc
- auth
- endpoint
- sts
- auth
- endpoint
- dist-es
- submodules
- sso-oidc
- auth
- commands
- endpoint
- models
- protocols
- sts
- auth
- commands
- endpoint
- models
- protocols
- dist-types
- submodules
- sso-oidc
- auth
- commands
- endpoint
- models
- protocols
- sts
- auth
- commands
- endpoint
- models
- protocols
- ts3.4
- submodules
- sso-oidc
- auth
- commands
- endpoint
- models
- protocols
- sts
- auth
- commands
- endpoint
- models
- protocols
- region-config-resolver
- dist-cjs
- regionConfig
- dist-es
- extensions
- regionConfig
- dist-types
- extensions
- regionConfig
- ts3.4
- extensions
- regionConfig
- s3-request-presigner
- dist-cjs
- dist-es
- dist-types
- ts3.4
- signature-v4-multi-region
- dist-cjs
- dist-es
- dist-types
- ts3.4
- token-providers
- dist-cjs
- dist-es
- dist-types
- ts3.4
- types
- dist-cjs
- dist-es
- blob
- extensions
- identity
- dist-types
- blob
- extensions
- identity
- ts3.4
- blob
- extensions
- identity
- util-arn-parser
- dist-cjs
- dist-es
- dist-types
- ts3.4
- util-endpoints
- dist-cjs
- lib/aws
- dist-es
- lib
- aws
- types
- dist-types
- lib
- aws
- ts3.4
- lib
- aws
- types
- types
- util-format-url
- dist-cjs
- dist-es
- dist-types
- ts3.4
- util-locate-window
- dist-cjs
- dist-es
- dist-types
- ts3.4
- util-user-agent-browser
- dist-cjs
- dist-es
- dist-types
- ts3.4
- util-user-agent-node
- dist-cjs
- dist-es
- dist-types
- ts3.4
- xml-builder
- dist-cjs
- dist-es
- dist-types
- ts3.4
- @aws/lambda-invoke-store
- dist-cjs
- dist-es
- dist-types
- @esbuild/darwin-arm64
- bin
- @fastify
- ajv-compiler
- .github
- workflows
- benchmark
- lib
- test
- types
- busboy
- deps
- dicer
- lib
- streamsearch
- lib
- types
- utils
- cors
- .github
- workflows
- benchmark
- test
- types
- deepmerge
- types
- error
- .github
- workflows
- benchmarks
- test
- types
- fast-json-stringify-compiler
- .github
- workflows
- test
- types
- forwarded
- types
- jwt
- .github
- workflows
- example
- test
- types
- merge-json-schemas
- .github
- workflows
- lib
- test
- types
- multipart
- .github
- workflows
- benchmark
- examples
- lib
- test
- types
- proxy-addr
- .github
- workflows
- benchmark
- test
- types
- @lukeed/ms
- dist
- @pinojs/redact
- .github
- workflows
- benchmarks
- scripts
- test
- @prisma
- client
- generator-build
- runtime
- scripts
- config
- dist
- debug
- dist
- engines-version
- engines
- dist
- scripts
- scripts
- fetch-engine
- dist
- get-platform
- dist
- test-utils
- @smithy
- abort-controller
- dist-cjs
- dist-es
- dist-types
- ts3.4
- chunked-blob-reader-native
- dist-cjs
- dist-es
- dist-types
- ts3.4
- chunked-blob-reader
- dist-cjs
- dist-es
- dist-types
- ts3.4
- config-resolver
- dist-cjs
- dist-es
- endpointsConfig
- utils
- regionConfig
- regionInfo
- dist-types
- endpointsConfig
- utils
- regionConfig
- regionInfo
- ts3.4
- endpointsConfig
- utils
- regionConfig
- regionInfo
- core
- dist-cjs
- submodules
- cbor
- event-streams
- protocols
- schema
- serde
- dist-es
- middleware-http-auth-scheme
- middleware-http-signing
- pagination
- request-builder
- submodules
- cbor
- event-streams
- protocols
- serde
- schema
- middleware
- schemas
- serde
- schema-serde-lib
- value
- util-identity-and-auth
- httpAuthSchemes
- dist-types
- middleware-http-auth-scheme
- middleware-http-signing
- pagination
- request-builder
- submodules
- cbor
- event-streams
- protocols
- serde
- schema
- middleware
- schemas
- serde
- schema-serde-lib
- value
- ts3.4
- middleware-http-auth-scheme
- middleware-http-signing
- pagination
- request-builder
- submodules
- cbor
- event-streams
- protocols
- serde
- schema
- middleware
- schemas
- serde
- schema-serde-lib
- value
- util-identity-and-auth
- httpAuthSchemes
- util-identity-and-auth
- httpAuthSchemes
- credential-provider-imds
Some content is hidden
Large Commits have some content hidden by default. Use the searchbox below for content that may be hidden.
17,456 files changed
+3053062
-0
lines changedLarge diffs are not rendered by default.
| Original file line number | Diff line number | Diff line change | |
|---|---|---|---|
| |||
| 1 | + | |
| 2 | + | |
| 3 | + | |
| 4 | + | |
| 5 | + | |
| 6 | + | |
| 7 | + | |
| 8 | + | |
| 9 | + | |
| 10 | + | |
| 11 | + | |
| 12 | + | |
| 13 | + | |
| 14 | + | |
| 15 | + | |
| 16 | + | |
| 17 | + | |
| 18 | + | |
| 19 | + | |
| 20 | + | |
| 21 | + | |
| 22 | + | |
| 23 | + | |
| 24 | + | |
| 25 | + | |
| 26 | + | |
| 27 | + | |
| 28 | + | |
| 29 | + | |
| 30 | + | |
| 31 | + | |
| 32 | + | |
| 33 | + | |
| 34 | + | |
| 35 | + | |
| 36 | + | |
| 37 | + | |
| 38 | + | |
| 39 | + | |
| 40 | + | |
| 41 | + | |
| 42 | + | |
| 43 | + | |
| 44 | + | |
| 45 | + | |
| 46 | + | |
| 47 | + | |
| 48 | + | |
| 49 | + | |
| 50 | + | |
| 51 | + | |
| 52 | + | |
| 53 | + | |
| 54 | + | |
| 55 | + | |
| 56 | + | |
| 57 | + | |
| 58 | + | |
| 59 | + | |
| 60 | + | |
| 61 | + | |
| 62 | + | |
| 63 | + | |
| 64 | + | |
| 65 | + | |
| 66 | + | |
| 67 | + | |
| 68 | + | |
| 69 | + | |
| 70 | + | |
| 71 | + | |
| 72 | + | |
| 73 | + | |
| 74 | + | |
| 75 | + | |
| 76 | + | |
| 77 | + | |
| 78 | + | |
| 79 | + | |
| 80 | + | |
| 81 | + | |
| 82 | + | |
| 83 | + | |
| 84 | + | |
| 85 | + | |
| 86 | + | |
| 87 | + | |
| 88 | + | |
| 89 | + | |
| 90 | + | |
| 91 | + | |
| 92 | + | |
| 93 | + | |
| 94 | + | |
| 95 | + | |
| 96 | + | |
| 97 | + | |
| 98 | + | |
| 99 | + | |
| 100 | + | |
| 101 | + | |
| 102 | + | |
| 103 | + | |
| 104 | + | |
| 105 | + | |
| 106 | + | |
| 107 | + | |
| 108 | + | |
| 109 | + | |
| 110 | + | |
| 111 | + | |
| 112 | + | |
| 113 | + | |
| 114 | + | |
| 115 | + | |
| 116 | + | |
| 117 | + | |
| 118 | + | |
| 119 | + | |
| 120 | + | |
| 121 | + | |
| 122 | + | |
| 123 | + | |
| 124 | + | |
| 125 | + | |
| 126 | + | |
| 127 | + | |
| 128 | + | |
| 129 | + | |
| 130 | + | |
| 131 | + | |
| 132 | + | |
| 133 | + | |
| 134 | + | |
| 135 | + | |
| 136 | + | |
| 137 | + | |
| 138 | + | |
| 139 | + | |
| 140 | + | |
| 141 | + | |
| 142 | + | |
| 143 | + | |
| 144 | + | |
| 145 | + | |
| 146 | + | |
| 147 | + | |
| 148 | + | |
| 149 | + | |
| 150 | + | |
| 151 | + | |
| 152 | + | |
| 153 | + | |
| 154 | + | |
| 155 | + | |
| 156 | + | |
| 157 | + | |
| 158 | + | |
| 159 | + | |
| 160 | + | |
| 161 | + | |
| 162 | + | |
| 163 | + | |
| 164 | + | |
| 165 | + | |
| 166 | + | |
| 167 | + | |
| 168 | + | |
| 169 | + | |
| 170 | + | |
| 171 | + | |
| 172 | + | |
| 173 | + | |
| 174 | + | |
| 175 | + | |
| 176 | + | |
| 177 | + | |
| 178 | + | |
| 179 | + | |
| 180 | + | |
| 181 | + | |
| 182 | + | |
| 183 | + | |
| 184 | + | |
| 185 | + | |
| 186 | + | |
| 187 | + | |
| 188 | + | |
| 189 | + | |
| 190 | + | |
| 191 | + | |
| 192 | + | |
| 193 | + | |
| 194 | + | |
| 195 | + | |
| 196 | + | |
| 197 | + | |
| 198 | + | |
| 199 | + | |
| 200 | + | |
| 201 | + | |
| 202 | + | |
| 203 | + | |
| 204 | + | |
| 205 | + | |
| 206 | + | |
| 207 | + | |
| 208 | + | |
| 209 | + | |
| 210 | + | |
| 211 | + | |
| 212 | + | |
| 213 | + | |
| 214 | + | |
| 215 | + | |
| 216 | + | |
| 217 | + | |
| 218 | + | |
| 219 | + | |
| 220 | + | |
| 221 | + | |
| 222 | + | |
| 223 | + | |
| 224 | + | |
| 225 | + | |
| 226 | + | |
| 227 | + | |
| 228 | + | |
| 229 | + | |
| 230 | + | |
| 231 | + | |
| 232 | + | |
| 233 | + | |
| 234 | + | |
| 235 | + | |
| 236 | + | |
| 237 | + | |
| 238 | + | |
| 239 | + | |
| 240 | + | |
| 241 | + | |
| 242 | + | |
| 243 | + | |
| 244 | + | |
| 245 | + | |
| 246 | + | |
| 247 | + | |
| 248 | + | |
| 249 | + | |
| 250 | + | |
| 251 | + | |
| 252 | + | |
| 253 | + | |
| 254 | + | |
| 255 | + | |
| 256 | + | |
| 257 | + | |
| 258 | + | |
| 259 | + | |
| 260 | + | |
| 261 | + | |
| 262 | + | |
| 263 | + | |
| 264 | + | |
| 265 | + | |
| 266 | + | |
| 267 | + | |
| 268 | + | |
| 269 | + | |
| 270 | + | |
| 271 | + | |
| 272 | + | |
| 273 | + | |
| 274 | + | |
| 275 | + | |
| 276 | + | |
| 277 | + | |
| 278 | + | |
| 279 | + | |
| 280 | + | |
| 281 | + | |
| 282 | + | |
| 283 | + | |
| 284 | + | |
| 285 | + | |
| 286 | + | |
| 287 | + | |
| 288 | + | |
| 289 | + | |
| 290 | + | |
| 291 | + | |
| 292 | + | |
| 293 | + | |
| 294 | + | |
| 295 | + | |
| 296 | + | |
| 297 | + | |
| 298 | + | |
| 299 | + | |
| 300 | + | |
| 301 | + | |
| 302 | + | |
| 303 | + | |
| 304 | + | |
| 305 | + | |
| 306 | + | |
| 307 | + | |
| 308 | + | |
| 309 | + | |
| 310 | + | |
| 311 | + | |
| 312 | + | |
| 313 | + | |
| 314 | + | |
| 315 | + | |
| 316 | + | |
| 317 | + | |
| 318 | + | |
| 319 | + | |
| 320 | + | |
| 321 | + | |
| 322 | + | |
| 323 | + | |
| 324 | + | |
| 325 | + | |
| 326 | + | |
| 327 | + | |
| 328 | + | |
| 329 | + | |
| 330 | + | |
| 331 | + | |
| 332 | + | |
| 333 | + | |
| 334 | + | |
| 335 | + | |
| 336 | + | |
| 337 | + | |
| 338 | + | |
| 339 | + | |
| 340 | + | |
| 341 | + | |
| 342 | + | |
| 343 | + | |
| 344 | + | |
| 345 | + | |
| 346 | + | |
| 347 | + | |
| 348 | + | |
| 349 | + | |
| 350 | + | |
| 351 | + | |
| 352 | + | |
| 353 | + | |
| 354 | + | |
| 355 | + | |
| 356 | + | |
| 357 | + | |
| 358 | + | |
| 359 | + | |
| 360 | + | |
| 361 | + | |
| 362 | + | |
| 363 | + | |
| 364 | + | |
| 365 | + | |
| 366 | + | |
| 367 | + | |
| 368 | + | |
0 commit comments