# sortyxai/sortyx-rewards-app architecture

> Architecture diagram of the GitHub repository sortyxai/sortyx-rewards-app, generated by GitDiagram from its file tree, README and a sample of source files (last updated 2026-04-10). It is an AI-made overview: check details against the source.

- Interactive diagram: https://gitdiagram.com/sortyxai/sortyx-rewards-app
- Repository: https://github.com/sortyxai/sortyx-rewards-app

## Overview

1. **Overall shape**
   - This repo is a two-tier application: a **React single-page frontend** and a **Node/Express backend API**.
   - The frontend handles all user interaction and stateful UI; the backend owns authentication, business rules, and persistent data.

2. **Frontend boundary**
   - `frontend/src/App.js` and `frontend/src/index.js` are the SPA entry points.
   - `react-router-dom` drives page-level navigation across `pages/` such as Landing, Dashboard, Donate, Rewards, Scan, Profile, AdminPanel, and PointsDashboard.
   - This is not a server-rendered app; the browser talks to the backend over HTTP.

3. **Frontend feature layers**
   - `components/` contains reusable UI and feature widgets such as login/register forms, password reset, QR scanner, bottom navigation, reward/admin managers, and route guards.
   - `contexts/AuthContext.js` centralizes client auth state, so login status and user identity are shared across the app.
   - `ProtectedRoute.js` is the main access-control boundary on the client side, preventing unauthenticated or unauthorized route access.

4. **Frontend service layer**
   - `services/pointsService.js`, `co2Service.js`, and `firestoreService.js` isolate non-UI logic from components.
   - `firebase.js` indicates a Firebase/Firestore integration point for client-side real-time or cloud-backed data.
   - `axios` is the primary bridge to the backend API; this is where points, rewards, profile, and auth data likely flow in and out.

5. **Backend bootstrapping**
   - `backend/server.js` is the main process entry point; `app.js` likely assembles middleware and route registration.
   - `backend/config/database.js` is the persistence setup layer.
   - `backend/uploads/` is a dedicated filesystem boundary for uploaded assets, handled through Multer.

6. **Backend layered architecture**
   - The backend is organized in a classic **route → controller → model** pattern.
   - `routes/` defines API surfaces, `controllers/` contains request/business logic, and `models/` define the domain entities and persistence access.
   - `middleware/auth.js` is the enforcement layer for protected endpoints, most likely validating JWTs and attaching user context.

7. **Domain model**
   - The model set shows the core business objects: `User`, `Token`, `Reward`, `Redemption`, `Donation`, `Charity`, `Achievement`, and `UserActivity`.
   - This tells you the system is broader than simple login/points: it tracks user identity, token-based sessions, reward catalogs, redemptions, donation/charity flows, and user history/activity.
   - `Token` suggests server-side support for password reset or session/token lifecycle in addition to JWT auth.

8. **Primary backend API flows**
   - Auth flows are handled by `authController` and auth routes: register, login, and auth verification.
   - Reward flows go through `rewardController` and donation/profile/admin controllers depending on whether the user is scanning QR codes, redeeming rewards, or viewing history.
   - Admin flows are separated in `adminController` and `routes/admin.js`, keeping system-management actions distinct from normal user APIs.

9. **Data flow from scan to points**
   - The most important business path is likely: **frontend QR scanner → API call → backend validation → points/reward update → persisted user activity**.
   - The scan UI lives in `components/QRScanner.js` and `pages/Scan.js`, while points display is split into `PointsDashboard` and rewards views.
   - This path is the main transactional loop in the system and should be a prominent arrow in any architecture diagram.

10. **Authentication and authorization**
    - The app uses **JWT-based auth** on the backend, with `bcryptjs` for password hashing.
    - The frontend keeps auth state in context and uses protected routes to gate dashboards, profile pages, and admin screens.
    - Authorization is role-sensitive: normal users can scan/redeem/view profile, while admins can manage rewards, charities, and users.

11. **Admin subsystem**
    - `AdminPanel.js`, `AdminCharityManager.js`, and `AdminRewardManager.js` are the main admin UI surfaces.
    - Backend admin routes and controllers likely expose stats, reward management, user management, and QR generation.
    - This is a clearly separated operational boundary: administrative actions do not live inside the regular user flow.

12. **External/service integrations**
    - `firebase.js` and `firestoreService.js` show a cloud datastore or real-time sync integration on the frontend side.
    - The backend also uses standard infrastructure pieces: SQLite for persistence, CORS for cross-origin browser access, and Multer for uploads.
    - The README’s mention of real-time notifications aligns with Firebase being the likely external event/data layer, though it is isolated from the core REST API.

13. **Operational/deployment boundary**
    - In development, frontend and backend run as separate processes on different ports.
    - The backend is configured for API serving, while the frontend is a buildable static SPA; production may serve the built frontend separately or through the backend.
    - `backend/.env.example` and `frontend/.env.example` show that environment configuration is split per tier.

14. **Architecture summary for diagramming**
    - Draw two main boxes: **React client** and **Express API**.
    - Inside the API box, show **routes → controllers → models → SQLite/filesystem**, with auth middleware guarding protected routes.
    - Add a separate **Firebase/Firestore integration** on the frontend side if you want to capture real-time/cloud-backed features.
    - Emphasize the primary data paths: **login/auth**, **QR scan → points update**, **reward redemption**, and **admin management**.

Main entry points: [`backend/controllers`](https://github.com/sortyxai/sortyx-rewards-app/tree/main/backend/controllers) (Controllers), [`frontend/src/App.js`](https://github.com/sortyxai/sortyx-rewards-app/blob/main/frontend/src/App.js) (App shell), [`frontend/src/pages`](https://github.com/sortyxai/sortyx-rewards-app/tree/main/frontend/src/pages) (Pages), [`frontend/src/components`](https://github.com/sortyxai/sortyx-rewards-app/tree/main/frontend/src/components) (UI components)

## Diagram (Mermaid)

```mermaid
flowchart TD

subgraph group_frontend["React client"]
  node_frontend_entry["SPA entry<br/>React bootstrap<br/>[index.js]"]
  node_frontend_app["App shell<br/>Route shell<br/>[App.js]"]
  node_frontend_pages["Pages<br/>View layer"]
  node_frontend_components["UI components<br/>Reusable UI"]
  node_auth_context["Auth state<br/>Client context<br/>[AuthContext.js]"]
  node_protected_route{{"Route guard<br/>Access control<br/>[ProtectedRoute.js]"}}
  node_frontend_services["Service layer<br/>Client services"]
  node_firebase_integration[("Firebase<br/>Cloud integration<br/>[firebase.js]")]
  node_api_client["HTTP client<br/>Axios bridge"]
end

subgraph group_backend["Express API"]
  node_backend_entry["Server entry<br/>Node bootstrap<br/>[server.js]"]
  node_backend_app["API app<br/>Express app<br/>[app.js]"]
  node_backend_routes["Routes<br/>API surfaces"]
  node_backend_middleware{{"Auth middleware<br/>JWT guard<br/>[auth.js]"}}
  node_backend_controllers["Controllers<br/>Business logic"]
  node_backend_models["Domain models<br/>Persistence models"]
  node_auth_flow["Auth API<br/>Auth flow<br/>[auth.js]"]
  node_reward_flow["Rewards API<br/>Reward flow<br/>[rewards.js]"]
  node_scan_flow{{"Scan update<br/>Transactional flow"}}
  node_admin_flow["Admin API<br/>Admin flow<br/>[admin.js]"]
end

subgraph group_data["Storage"]
  node_backend_db[("SQLite config<br/>Persistence setup<br/>[database.js]")]
  node_uploads["Uploads<br/>File storage"]
end

node_frontend_entry -->|"boots"| node_frontend_app
node_frontend_app -->|"navigates"| node_frontend_pages
node_frontend_app -->|"composes"| node_frontend_components
node_frontend_app -->|"reads state"| node_auth_context
node_frontend_app -->|"guards"| node_protected_route
node_frontend_components -->|"calls"| node_api_client
node_frontend_services -->|"wraps"| node_api_client
node_frontend_services -->|"syncs"| node_firebase_integration
node_auth_context -->|"authenticates"| node_api_client
node_frontend_pages -->|"requests"| node_api_client
node_api_client -->|"HTTP"| node_backend_app
node_backend_entry -->|"starts"| node_backend_app
node_backend_app -->|"registers"| node_backend_routes
node_backend_routes -->|"protects"| node_backend_middleware
node_backend_routes -->|"dispatches"| node_backend_controllers
node_backend_controllers -->|"persists via"| node_backend_models
node_backend_models -->|"stores in"| node_backend_db
node_backend_controllers -->|"writes files"| node_uploads
node_auth_flow -->|"implements"| node_backend_controllers
node_reward_flow -->|"implements"| node_backend_controllers
node_scan_flow -->|"updates"| node_backend_models
node_admin_flow -->|"implements"| node_backend_controllers
node_frontend_pages -->|"uses"| node_frontend_services
node_frontend_components -->|"depends on"| node_protected_route

click node_frontend_entry "https://github.com/sortyxai/sortyx-rewards-app/blob/main/frontend/src/index.js"
click node_frontend_app "https://github.com/sortyxai/sortyx-rewards-app/blob/main/frontend/src/App.js"
click node_frontend_pages "https://github.com/sortyxai/sortyx-rewards-app/tree/main/frontend/src/pages"
click node_frontend_components "https://github.com/sortyxai/sortyx-rewards-app/tree/main/frontend/src/components"
click node_auth_context "https://github.com/sortyxai/sortyx-rewards-app/blob/main/frontend/src/contexts/AuthContext.js"
click node_protected_route "https://github.com/sortyxai/sortyx-rewards-app/blob/main/frontend/src/components/ProtectedRoute.js"
click node_frontend_services "https://github.com/sortyxai/sortyx-rewards-app/tree/main/frontend/src/services"
click node_firebase_integration "https://github.com/sortyxai/sortyx-rewards-app/blob/main/frontend/src/firebase.js"
click node_backend_entry "https://github.com/sortyxai/sortyx-rewards-app/blob/main/backend/server.js"
click node_backend_app "https://github.com/sortyxai/sortyx-rewards-app/blob/main/backend/app.js"
click node_backend_routes "https://github.com/sortyxai/sortyx-rewards-app/tree/main/backend/routes"
click node_backend_middleware "https://github.com/sortyxai/sortyx-rewards-app/blob/main/backend/middleware/auth.js"
click node_backend_controllers "https://github.com/sortyxai/sortyx-rewards-app/tree/main/backend/controllers"
click node_backend_models "https://github.com/sortyxai/sortyx-rewards-app/tree/main/backend/models"
click node_backend_db "https://github.com/sortyxai/sortyx-rewards-app/blob/main/backend/config/database.js"
click node_uploads "https://github.com/sortyxai/sortyx-rewards-app/tree/main/backend/uploads"
click node_auth_flow "https://github.com/sortyxai/sortyx-rewards-app/blob/main/backend/routes/auth.js"
click node_reward_flow "https://github.com/sortyxai/sortyx-rewards-app/blob/main/backend/routes/rewards.js"
click node_scan_flow "https://github.com/sortyxai/sortyx-rewards-app/blob/main/backend/controllers/rewardController.js"
click node_admin_flow "https://github.com/sortyxai/sortyx-rewards-app/blob/main/backend/routes/admin.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_frontend_entry,node_frontend_app,node_frontend_pages,node_frontend_components,node_auth_context,node_protected_route,node_frontend_services,node_firebase_integration,node_api_client toneBlue
class node_backend_entry,node_backend_app,node_backend_routes,node_backend_middleware,node_backend_controllers,node_backend_models,node_auth_flow,node_reward_flow,node_scan_flow,node_admin_flow toneAmber
class node_backend_db,node_uploads toneMint
```

## Components

### React client

Single-page app handling navigation, auth state, scanning, rewards, and admin UI.

- [SPA entry](https://github.com/sortyxai/sortyx-rewards-app/blob/main/frontend/src/index.js): React bootstrap · Browser entry points that mount the app and initialize client routing. · `frontend/src/index.js`
- [App shell](https://github.com/sortyxai/sortyx-rewards-app/blob/main/frontend/src/App.js): Route shell · Top-level client router and application composition. · `frontend/src/App.js`
- [Pages](https://github.com/sortyxai/sortyx-rewards-app/tree/main/frontend/src/pages): View layer · Primary route-level screens such as landing, dashboard, donate, rewards, scan, profile, and admin. · `frontend/src/pages`
- [UI components](https://github.com/sortyxai/sortyx-rewards-app/tree/main/frontend/src/components): Reusable UI · Shared widgets and feature components including auth forms, QR scanner, navigation, and admin managers. · `frontend/src/components`
- [Auth state](https://github.com/sortyxai/sortyx-rewards-app/blob/main/frontend/src/contexts/AuthContext.js): Client context · Centralized frontend authentication and user identity state. · `frontend/src/contexts/AuthContext.js`
- [Route guard](https://github.com/sortyxai/sortyx-rewards-app/blob/main/frontend/src/components/ProtectedRoute.js): Access control · Client-side protection for authenticated and role-gated routes. · `frontend/src/components/ProtectedRoute.js`
- [Service layer](https://github.com/sortyxai/sortyx-rewards-app/tree/main/frontend/src/services): Client services · Non-UI logic for points, CO2, and Firestore-backed data access. · `frontend/src/services`
- [Firebase](https://github.com/sortyxai/sortyx-rewards-app/blob/main/frontend/src/firebase.js): Cloud integration · Client integration point for realtime or cloud-backed features. · `frontend/src/firebase.js`
- **HTTP client**: Axios bridge · Browser-to-backend communication for auth, profile, rewards, points, and admin operations.

### Express API

REST backend organizing routes, controllers, middleware, and persistence.

- [Server entry](https://github.com/sortyxai/sortyx-rewards-app/blob/main/backend/server.js): Node bootstrap · Main backend process entry that starts the API server. · `backend/server.js`
- [API app](https://github.com/sortyxai/sortyx-rewards-app/blob/main/backend/app.js): Express app · Middleware and route assembly for the backend API. · `backend/app.js`
- [Routes](https://github.com/sortyxai/sortyx-rewards-app/tree/main/backend/routes): API surfaces · Endpoint definitions for auth, admin, donations, profile, and rewards. · `backend/routes`
- [Auth middleware](https://github.com/sortyxai/sortyx-rewards-app/blob/main/backend/middleware/auth.js): JWT guard · Protected-endpoint enforcement and user-context attachment. · `backend/middleware/auth.js`
- [Controllers](https://github.com/sortyxai/sortyx-rewards-app/tree/main/backend/controllers): Business logic · Request handling for auth, admin, donations, profile, and rewards. · `backend/controllers`
- [Domain models](https://github.com/sortyxai/sortyx-rewards-app/tree/main/backend/models): Persistence models · User, token, reward, redemption, donation, charity, achievement, and activity entities. · `backend/models`
- [Auth API](https://github.com/sortyxai/sortyx-rewards-app/blob/main/backend/routes/auth.js): Auth flow · Register, login, and auth verification path. · `backend/routes/auth.js`
- [Rewards API](https://github.com/sortyxai/sortyx-rewards-app/blob/main/backend/routes/rewards.js): Reward flow · Reward lookup, redemption, and related points operations. · `backend/routes/rewards.js`
- [Scan update](https://github.com/sortyxai/sortyx-rewards-app/blob/main/backend/controllers/rewardController.js): Transactional flow · QR scan path that validates input, updates points, and records user activity. · `backend/controllers/rewardController.js`
- [Admin API](https://github.com/sortyxai/sortyx-rewards-app/blob/main/backend/routes/admin.js): Admin flow · Operational management for users, rewards, charities, and stats. · `backend/routes/admin.js`

### Storage

Primary persistence and upload boundaries used by the backend and client integrations.

- [SQLite config](https://github.com/sortyxai/sortyx-rewards-app/blob/main/backend/config/database.js): Persistence setup · Database connection and storage configuration. · `backend/config/database.js`
- [Uploads](https://github.com/sortyxai/sortyx-rewards-app/tree/main/backend/uploads): File storage · Filesystem boundary for uploaded assets. · `backend/uploads`

## Connections

- SPA entry → App shell: boots · Mounts the SPA and initializes routing.
- App shell → Pages: navigates · Routes to page-level views.
- App shell → UI components: composes · Uses shared UI and feature widgets.
- App shell → Auth state: reads state · Consumes shared auth state across the app.
- App shell → Route guard: guards · Wraps privileged screens with client-side access control.
- UI components → HTTP client: calls · Feature components trigger backend requests through the HTTP bridge.
- Service layer → HTTP client: wraps · Service modules isolate request logic from UI components.
- Service layer → Firebase: syncs · Uses cloud-backed client data and realtime features where needed.
- Auth state → HTTP client: authenticates · Stores login state populated by API responses.
- Pages → HTTP client: requests · Pages fetch and submit data to the backend.
- HTTP client → API app: HTTP · Client traffic reaches the Express API.
- Server entry → API app: starts · Bootstraps the Express application.
- API app → Routes: registers · Mounts route modules.
- Routes → Auth middleware: protects · Applies JWT-based endpoint protection.
- Routes → Controllers: dispatches · Hands requests to controller logic.
- Controllers → Domain models: persists via · Reads and writes domain entities.
- Domain models → SQLite config: stores in · Backed by SQLite persistence.
- Controllers → Uploads: writes files · Handles uploaded assets through the filesystem boundary.
- Auth API → Controllers: implements · Authentication requests are handled in the auth controller.
- Rewards API → Controllers: implements · Reward and redemption requests are handled in reward logic.
- Scan update → Domain models: updates · Records points, redemptions, and user activity.
- Admin API → Controllers: implements · Admin operations are handled separately from user flows.
- Pages → Service layer: uses · Screens rely on client service modules for derived data and sync.
- UI components → Route guard: depends on · Privileged UI is composed through route protection.
