# alsakkaf-dev/utm-borrow architecture

> Architecture diagram of the GitHub repository alsakkaf-dev/utm-borrow, generated by GitDiagram from its file tree, README and a sample of source files (last updated 2026-06-02). It is an AI-made overview: check details against the source.

- Interactive diagram: https://gitdiagram.com/alsakkaf-dev/utm-borrow
- Repository: https://github.com/alsakkaf-dev/utm-borrow

## Overview

1. **Overall shape**
   - This is a two-part application: a **React frontend** and a **FastAPI backend** connected over HTTP, with **Server-Sent Events (SSE)** used for live updates.
   - Data is stored in **MongoDB** via Motor, and the backend acts as the sole business-logic boundary for auth, lending workflow, chat, moderation, and admin actions.

2. **Frontend architecture**
   - The frontend is a **single-page app** with route-based screens grouped into four domains: **resource/catalog**, **transaction flow**, **identity/settings**, and **admin**.
   - `App.js` applies route guards for **protected student routes** and **admin-only routes**.
   - `AuthContext` holds login state, JWT session info, realtime connection state, and unread counts; it is the main client-side state boundary.

3. **Backend architecture**
   - `server.py` is the FastAPI composition root: it loads configuration, sets CORS, includes routers, and seeds initial data in development.
   - The backend is split by concern into routers such as `auth`, `items`, `transactions`, `qr`, `ratings`, `chat`, `profile`, `events`, `admin`, `moderation`, and `support`.
   - Shared domain logic lives in helper modules like `tx_common.py`, `security.py`, `mfa.py`, `qr_engine.py`, `crypto_box.py`, and `realtime.py`.

4. **Core domain model**
   - The main business entity is the **item lending transaction**, which follows a strict lifecycle: request → approve → QR handover → borrowed → QR return → completed.
   - Items, users, transactions, ratings, notifications, reports, sessions, and audit records are all persisted as MongoDB documents, typically keyed by string UUIDs.
   - The app treats the **borrower**, **lender**, and **admin** as distinct behavioral roles, not just UI labels.

5. **Catalog and inventory flow**
   - `items.py` powers the public catalog, item detail pages, item CRUD for owners, and visibility toggling between public/private.
   - The frontend’s catalog and lend screens are built around item browsing, listing management, and owner-only inventory controls.
   - A key boundary is owner isolation: the backend filters item ownership server-side, and the frontend only renders what the API returns.

6. **Transaction workflow**
   - `transactions.py` owns the state machine for requesting, approving, rejecting, canceling, and returning loans.
   - `dashboard.py` and the transaction-related frontend pages present role-specific activity summaries rather than a single merged list.
   - `tx_common.py` centralizes transaction enrichment, lease flags, and trust-score recomputation so the same logic is reused across endpoints.

7. **QR handover and return**
   - `qr.py` and `qr_engine.py` implement the physical exchange boundary: a QR code contains a **signed, time-limited token** that must verify before status advances.
   - The handover does not count as “borrowed” until QR verification succeeds, which is what triggers the active lease state.
   - This is the app’s main trust anchor between digital workflow and real-world exchange.

8. **Chat and privacy**
   - `chat.py` provides transaction-scoped peer chat, and `crypto_box.py` encrypts messages at rest before they are stored.
   - Chat history is intentionally ephemeral: it is cleared when a transaction is canceled or rejected.
   - Access is constrained to the two transaction participants, with additional admin transcript access tied to moderation context.

9. **Realtime updates**
   - `realtime.py` is an in-process pub/sub broadcaster that pushes SSE events to the frontend.
   - `events.py` exposes the SSE endpoint, and the frontend subscribes through `frontend/src/lib/realtime.js`.
   - Event-driven updates are used for transaction changes, catalog changes, notifications, chat messages, moderation changes, and admin audit changes.

10. **Identity, sessions, and security**
   - `auth.py` covers registration, login, password reset, password change, active session listing, and revocation.
   - `security.py` handles password hashing, JWT creation/validation, and user/admin session checks.
   - Sessions are tracked by `jti`, enabling per-device session visibility and revocation from the UI.

11. **2FA and admin step-up**
   - `mfa.py` provides TOTP-based two-factor authentication for user accounts.
   - The admin portal uses the same MFA engine to mint a short-lived **admin session** before sensitive actions.
   - This creates a separate trust boundary between ordinary user login and privileged admin operations.

12. **Notifications, profile, and support**
   - `profile.py` manages profile data plus notification listing, read state, and deletion.
   - `notifications.py` and the realtime layer work together so notifications can appear immediately and then be marked read when the page opens.
   - `support.py` and `moderation.py` feed a unified admin inbox for help tickets and fraud/report handling.

13. **Admin subsystem**
   - The admin area is a distinct frontend section with its own layout, data views, and API client token path.
   - `admin.py` handles user management, transaction oversight, listing visibility control, audit logging, and admin session enforcement.
   - `admin.changed` events broadcast any audited change, making the admin UI event-driven rather than poll-driven.

14. **Operational and deployment boundaries**
   - The frontend is configured for static deployment on **Vercel**, while the backend is deployed separately on **Render** with **MongoDB Atlas** or local MongoDB.
   - Tests are backend integration tests that exercise the live API against a seeded server, so the backend is the primary source of truth for behavior.
   - The project includes Windows launch scripts for running the full stack locally, but the architecture itself is still a clean client/server split.

Main entry points: [`backend/server.py`](https://github.com/alsakkaf-dev/utm-borrow/blob/main/backend/server.py) (Server root), [`backend/tx_common.py`](https://github.com/alsakkaf-dev/utm-borrow/blob/main/backend/tx_common.py) (Shared domain), [`frontend/src/App.js`](https://github.com/alsakkaf-dev/utm-borrow/blob/main/frontend/src/App.js) (App shell), [`backend/database.py`](https://github.com/alsakkaf-dev/utm-borrow/blob/main/backend/database.py) (Mongo store)

## Diagram (Mermaid)

```mermaid
flowchart TD

subgraph group_frontend["Frontend"]
  node_frontend_app["App shell<br/>React routing<br/>[App.js]"]
  node_frontend_auth["Auth state<br/>client state<br/>[AuthContext.js]"]
  node_frontend_api["API client<br/>HTTP client<br/>[api.js]"]
  node_frontend_realtime["SSE client<br/>realtime client<br/>[realtime.js]"]
  node_frontend_catalog["Catalog UI<br/>resource screens<br/>[Catalog.js]"]
  node_frontend_lend["Lend flow<br/>transaction screens<br/>[Lend.js]"]
  node_frontend_tx["Activity hub<br/>transaction UI<br/>[Dashboard.js]"]
  node_frontend_identity["Identity area<br/>account screens<br/>[SettingsHub.js]"]
  node_frontend_admin["Admin area<br/>admin UI<br/>[AdminLayout.js]"]
end

subgraph group_backend["Backend"]
  node_backend_server["Server root<br/>FastAPI app<br/>[server.py]"]
  node_backend_auth["Auth service<br/>identity router<br/>[auth.py]"]
  node_backend_items["Items service<br/>catalog router<br/>[items.py]"]
  node_backend_tx["Transaction engine<br/>workflow router<br/>[transactions.py]"]
  node_backend_qr["QR exchange<br/>handover router<br/>[qr.py]"]
  node_backend_chat["Chat service<br/>conversation router<br/>[chat.py]"]
  node_backend_profile["Profile & notices<br/>user router<br/>[profile.py]"]
  node_backend_events["SSE events<br/>event router<br/>[events.py]"]
  node_backend_admin["Admin control<br/>admin router<br/>[admin.py]"]
  node_backend_moderation["Moderation queue<br/>review router<br/>[moderation.py]"]
  node_backend_support["Support inbox<br/>support router<br/>[support.py]"]
  node_backend_common["Shared domain<br/>core modules<br/>[tx_common.py]"]
  node_backend_store[("Mongo store<br/>persistence<br/>[database.py]")]
end

node_frontend_app -->|"uses"| node_frontend_auth
node_frontend_app -->|"routes to"| node_frontend_catalog
node_frontend_app -->|"routes to"| node_frontend_lend
node_frontend_app -->|"routes to"| node_frontend_tx
node_frontend_app -->|"routes to"| node_frontend_identity
node_frontend_app -->|"routes to"| node_frontend_admin
node_frontend_catalog -->|"calls"| node_frontend_api
node_frontend_lend -->|"calls"| node_frontend_api
node_frontend_tx -->|"calls"| node_frontend_api
node_frontend_identity -->|"calls"| node_frontend_api
node_frontend_admin -->|"calls"| node_frontend_api
node_frontend_realtime -->|"updates"| node_frontend_auth
node_frontend_realtime -->|"pushes"| node_frontend_tx
node_frontend_realtime -->|"pushes"| node_frontend_admin
node_backend_server -->|"includes"| node_backend_auth
node_backend_server -->|"includes"| node_backend_items
node_backend_server -->|"includes"| node_backend_tx
node_backend_server -->|"includes"| node_backend_qr
node_backend_server -->|"includes"| node_backend_chat
node_backend_server -->|"includes"| node_backend_profile
node_backend_server -->|"includes"| node_backend_events
node_backend_server -->|"includes"| node_backend_admin
node_backend_server -->|"includes"| node_backend_moderation
node_backend_server -->|"includes"| node_backend_support
node_backend_auth -->|"uses"| node_backend_common
node_backend_items -->|"uses"| node_backend_common
node_backend_tx -->|"uses"| node_backend_common
node_backend_qr -->|"uses"| node_backend_common
node_backend_chat -->|"uses"| node_backend_common
node_backend_admin -->|"uses"| node_backend_common
node_backend_events -->|"uses"| node_backend_common
node_backend_common -->|"persists to"| node_backend_store
node_backend_items -->|"persists to"| node_backend_store
node_backend_tx -->|"persists to"| node_backend_store
node_backend_chat -->|"persists to"| node_backend_store
node_backend_profile -->|"persists to"| node_backend_store
node_backend_admin -->|"persists to"| node_backend_store

click node_frontend_app "https://github.com/alsakkaf-dev/utm-borrow/blob/main/frontend/src/App.js"
click node_frontend_auth "https://github.com/alsakkaf-dev/utm-borrow/blob/main/frontend/src/context/AuthContext.js"
click node_frontend_api "https://github.com/alsakkaf-dev/utm-borrow/blob/main/frontend/src/lib/api.js"
click node_frontend_realtime "https://github.com/alsakkaf-dev/utm-borrow/blob/main/frontend/src/lib/realtime.js"
click node_frontend_catalog "https://github.com/alsakkaf-dev/utm-borrow/blob/main/frontend/src/pages/Resource/Catalog.js"
click node_frontend_lend "https://github.com/alsakkaf-dev/utm-borrow/blob/main/frontend/src/pages/Resource/Lend.js"
click node_frontend_tx "https://github.com/alsakkaf-dev/utm-borrow/blob/main/frontend/src/pages/Transaction/Dashboard.js"
click node_frontend_identity "https://github.com/alsakkaf-dev/utm-borrow/blob/main/frontend/src/pages/identity/SettingsHub.js"
click node_frontend_admin "https://github.com/alsakkaf-dev/utm-borrow/blob/main/frontend/src/pages/admin/AdminLayout.js"
click node_backend_server "https://github.com/alsakkaf-dev/utm-borrow/blob/main/backend/server.py"
click node_backend_auth "https://github.com/alsakkaf-dev/utm-borrow/blob/main/backend/routers/auth.py"
click node_backend_items "https://github.com/alsakkaf-dev/utm-borrow/blob/main/backend/routers/items.py"
click node_backend_tx "https://github.com/alsakkaf-dev/utm-borrow/blob/main/backend/routers/transactions.py"
click node_backend_qr "https://github.com/alsakkaf-dev/utm-borrow/blob/main/backend/routers/qr.py"
click node_backend_chat "https://github.com/alsakkaf-dev/utm-borrow/blob/main/backend/routers/chat.py"
click node_backend_profile "https://github.com/alsakkaf-dev/utm-borrow/blob/main/backend/routers/profile.py"
click node_backend_events "https://github.com/alsakkaf-dev/utm-borrow/blob/main/backend/routers/events.py"
click node_backend_admin "https://github.com/alsakkaf-dev/utm-borrow/blob/main/backend/routers/admin.py"
click node_backend_moderation "https://github.com/alsakkaf-dev/utm-borrow/blob/main/backend/routers/moderation.py"
click node_backend_support "https://github.com/alsakkaf-dev/utm-borrow/blob/main/backend/routers/support.py"
click node_backend_common "https://github.com/alsakkaf-dev/utm-borrow/blob/main/backend/tx_common.py"
click node_backend_store "https://github.com/alsakkaf-dev/utm-borrow/blob/main/backend/database.py"

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_app,node_frontend_auth,node_frontend_api,node_frontend_realtime,node_frontend_catalog,node_frontend_lend,node_frontend_tx,node_frontend_identity,node_frontend_admin toneBlue
class node_backend_server,node_backend_auth,node_backend_items,node_backend_tx,node_backend_qr,node_backend_chat,node_backend_profile,node_backend_events,node_backend_admin,node_backend_moderation,node_backend_support,node_backend_common,node_backend_store toneAmber
```

## Components

### Frontend

React SPA with route-based screens, client state, and SSE subscriptions.

- [App shell](https://github.com/alsakkaf-dev/utm-borrow/blob/main/frontend/src/App.js): React routing · Top-level SPA entry with route guards and layout composition. · `frontend/src/App.js`
- [Auth state](https://github.com/alsakkaf-dev/utm-borrow/blob/main/frontend/src/context/AuthContext.js): client state · Holds login, JWT session, realtime connection state, and unread counts. · `frontend/src/context/AuthContext.js`
- [API client](https://github.com/alsakkaf-dev/utm-borrow/blob/main/frontend/src/lib/api.js): HTTP client · Shared frontend client for backend calls and auth/session handling. · `frontend/src/lib/api.js`
- [SSE client](https://github.com/alsakkaf-dev/utm-borrow/blob/main/frontend/src/lib/realtime.js): realtime client · Subscribes to server-sent events and feeds live UI updates. · `frontend/src/lib/realtime.js`
- [Catalog UI](https://github.com/alsakkaf-dev/utm-borrow/blob/main/frontend/src/pages/Resource/Catalog.js): resource screens · Browse, inspect, and manage items and listings. · `frontend/src/pages/Resource/Catalog.js`
- [Lend flow](https://github.com/alsakkaf-dev/utm-borrow/blob/main/frontend/src/pages/Resource/Lend.js): transaction screens · Creates and tracks lending requests, approvals, and returns. · `frontend/src/pages/Resource/Lend.js`
- [Activity hub](https://github.com/alsakkaf-dev/utm-borrow/blob/main/frontend/src/pages/Transaction/Dashboard.js): transaction UI · Role-specific dashboard, history, chat, notifications, and scanner views. · `frontend/src/pages/Transaction/Dashboard.js`
- [Identity area](https://github.com/alsakkaf-dev/utm-borrow/blob/main/frontend/src/pages/identity/SettingsHub.js): account screens · Login, profile, settings, security, and support flows. · `frontend/src/pages/identity/SettingsHub.js`
- [Admin area](https://github.com/alsakkaf-dev/utm-borrow/blob/main/frontend/src/pages/admin/AdminLayout.js): admin UI · Privileged screens for oversight, reports, audits, and user management. · `frontend/src/pages/admin/AdminLayout.js`

### Backend

FastAPI API, domain routers, shared workflow logic, and persistence.

- [Server root](https://github.com/alsakkaf-dev/utm-borrow/blob/main/backend/server.py): FastAPI app · Composition root that loads config, wires routers, sets CORS, and seeds dev data. · `backend/server.py`
- [Auth service](https://github.com/alsakkaf-dev/utm-borrow/blob/main/backend/routers/auth.py): identity router · Registration, login, password reset, session listing, and revocation. · `backend/routers/auth.py`
- [Items service](https://github.com/alsakkaf-dev/utm-borrow/blob/main/backend/routers/items.py): catalog router · Catalog browsing, item detail, owner CRUD, and visibility controls. · `backend/routers/items.py`
- [Transaction engine](https://github.com/alsakkaf-dev/utm-borrow/blob/main/backend/routers/transactions.py): workflow router · Loan request lifecycle: request, approve, reject, cancel, return, complete. · `backend/routers/transactions.py`
- [QR exchange](https://github.com/alsakkaf-dev/utm-borrow/blob/main/backend/routers/qr.py): handover router · Signed QR token verification for borrow and return handoff. · `backend/routers/qr.py`
- [Chat service](https://github.com/alsakkaf-dev/utm-borrow/blob/main/backend/routers/chat.py): conversation router · Transaction-scoped peer chat with encrypted message storage. · `backend/routers/chat.py`
- [Profile & notices](https://github.com/alsakkaf-dev/utm-borrow/blob/main/backend/routers/profile.py): user router · Profile data plus notifications and read/delete state. · `backend/routers/profile.py`
- [SSE events](https://github.com/alsakkaf-dev/utm-borrow/blob/main/backend/routers/events.py): event router · Server-sent events endpoint backed by the in-process broadcaster. · `backend/routers/events.py`
- [Admin control](https://github.com/alsakkaf-dev/utm-borrow/blob/main/backend/routers/admin.py): admin router · User management, audit logging, oversight, and sensitive action enforcement. · `backend/routers/admin.py`
- [Moderation queue](https://github.com/alsakkaf-dev/utm-borrow/blob/main/backend/routers/moderation.py): review router · Reports, fraud handling, and moderation workflows for admins. · `backend/routers/moderation.py`
- [Support inbox](https://github.com/alsakkaf-dev/utm-borrow/blob/main/backend/routers/support.py): support router · Help tickets and support intake feeding the admin workspace. · `backend/routers/support.py`
- [Shared domain](https://github.com/alsakkaf-dev/utm-borrow/blob/main/backend/tx_common.py): core modules · Cross-cutting logic for security, MFA, QR signing, encryption, realtime, and transaction enrichment. · `backend/tx_common.py`
- [Mongo store](https://github.com/alsakkaf-dev/utm-borrow/blob/main/backend/database.py): persistence · MongoDB/Motor-backed document storage for users, items, transactions, chats, notifications, sessions, and audits. · `backend/database.py`

## Connections

- App shell → Auth state: uses · App shell gates routes based on session and role state.
- App shell → Catalog UI: routes to · Public resource browsing and item pages.
- App shell → Lend flow: routes to · Borrowing and item listing flows.
- App shell → Activity hub: routes to · Transaction-centered dashboards and tools.
- App shell → Identity area: routes to · Authentication, settings, and profile screens.
- App shell → Admin area: routes to · Admin-only section protected by elevated access.
- Catalog UI → API client: calls · Fetches catalog and item data from the backend.
- Lend flow → API client: calls · Submits lending workflow actions.
- Activity hub → API client: calls · Loads activity, chat, notifications, and scanner data.
- Identity area → API client: calls · Reads and updates account and security state.
- Admin area → API client: calls · Uses privileged admin endpoints.
- SSE client → Auth state: updates · Maintains live connection and unread state in client context.
- SSE client → Activity hub: pushes · Feeds live transaction, chat, and notification updates.
- SSE client → Admin area: pushes · Feeds admin audit and oversight events.
- Server root → Auth service: includes · Registers auth routes on the FastAPI app.
- Server root → Items service: includes · Registers catalog and item routes.
- Server root → Transaction engine: includes · Registers lending workflow routes.
- Server root → QR exchange: includes · Registers QR verification routes.
- Server root → Chat service: includes · Registers chat routes.
- Server root → Profile & notices: includes · Registers profile and notification routes.
- Server root → SSE events: includes · Registers SSE event stream routes.
- Server root → Admin control: includes · Registers admin routes.
- Server root → Moderation queue: includes · Registers moderation routes.
- Server root → Support inbox: includes · Registers support routes.
- Auth service → Shared domain: uses · Relies on shared security and session logic.
- Items service → Shared domain: uses · Shares ownership and visibility rules.
- Transaction engine → Shared domain: uses · Reuses transaction enrichment and lifecycle helpers.
- QR exchange → Shared domain: uses · Uses QR signing and verification helpers.
- Chat service → Shared domain: uses · Encrypts and protects chat payloads.
- Admin control → Shared domain: uses · Applies MFA and privileged session checks.
- SSE events → Shared domain: uses · Streams broadcaster events to clients.
- Shared domain → Mongo store: persists to · All major documents and sessions are stored in MongoDB.
- Items service → Mongo store: persists to · Stores item catalog and ownership data.
- Transaction engine → Mongo store: persists to · Stores transaction state and history.
- Chat service → Mongo store: persists to · Stores encrypted chat messages.
- Profile & notices → Mongo store: persists to · Stores profiles, notifications, and read state.
- Admin control → Mongo store: persists to · Stores audits and admin-managed records.
