How alsakkaf-dev/utm-borrow works
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: server.py Server root · tx_common.py Shared domain · App.js App shell · database.py Mongo store
Components 22
Frontend
React SPA with route-based screens, client state, and SSE subscriptions.
- App shell · Top-level SPA entry with route guards and layout composition.
frontend/src/App.js - Auth state · Holds login, JWT session, realtime connection state, and unread counts.
frontend/src/context/AuthContext.js - API client · Shared frontend client for backend calls and auth/session handling.
frontend/src/lib/api.js - SSE client · Subscribes to server-sent events and feeds live UI updates.
frontend/src/lib/realtime.js - Catalog UI · Browse, inspect, and manage items and listings.
frontend/src/pages/Resource/Catalog.js - Lend flow · Creates and tracks lending requests, approvals, and returns.
frontend/src/pages/Resource/Lend.js - Activity hub · Role-specific dashboard, history, chat, notifications, and scanner views.
frontend/src/pages/Transaction/Dashboard.js - Identity area · Login, profile, settings, security, and support flows.
frontend/src/pages/identity/SettingsHub.js - Admin area · 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 · Composition root that loads config, wires routers, sets CORS, and seeds dev data.
backend/server.py - Auth service · Registration, login, password reset, session listing, and revocation.
backend/routers/auth.py - Items service · Catalog browsing, item detail, owner CRUD, and visibility controls.
backend/routers/items.py - Transaction engine · Loan request lifecycle: request, approve, reject, cancel, return, complete.
backend/routers/transactions.py - QR exchange · Signed QR token verification for borrow and return handoff.
backend/routers/qr.py - Chat service · Transaction-scoped peer chat with encrypted message storage.
backend/routers/chat.py - Profile & notices · Profile data plus notifications and read/delete state.
backend/routers/profile.py - SSE events · Server-sent events endpoint backed by the in-process broadcaster.
backend/routers/events.py - Admin control · User management, audit logging, oversight, and sensitive action enforcement.
backend/routers/admin.py - Moderation queue · Reports, fraud handling, and moderation workflows for admins.
backend/routers/moderation.py - Support inbox · Help tickets and support intake feeding the admin workspace.
backend/routers/support.py - Shared domain · Cross-cutting logic for security, MFA, QR signing, encryption, realtime, and transaction enrichment.
backend/tx_common.py - Mongo store · MongoDB/Motor-backed document storage for users, items, transactions, chats, notifications, sessions, and audits.
backend/database.py
Connections 37
- 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.