# kratospidey/sasta_dmart architecture

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

- Interactive diagram: https://gitdiagram.com/kratospidey/sasta_dmart
- Repository: https://github.com/kratospidey/sasta_dmart

## Overview

1. **Repository shape**
   - This repo is a three-part checkout system built around Firebase Realtime Database:
     - a **Raspberry Pi checkout kiosk**
     - a **laptop operator dashboard**
     - a **public hosted claim page** for phone-based Google sign-in
   - Shared Python code lives under `sasta_dmart/`; the hosted phone flow lives in `public_claim/`; docs and RTDB rules are in `docs/`.

2. **Primary runtime boundaries**
   - The **Pi** owns checkout, camera/barcode scanning, session creation, and transaction writing.
   - The **public claim site** owns authentication and one-time claim of a pending checkout session.
   - The **laptop dashboard** is monitoring/diagnostics only; it is explicitly not part of the phone auth path.
   - Firebase is the integration point between all three runtimes.

3. **Pi checkout application**
   - `pi_checkout_gui_firebase.py` is the main Pi entrypoint.
   - It depends on Pi-specific packages like Picamera2, Tk, PIL, and pyzbar, which indicates a local GUI app that previews camera input and scans barcodes.
   - Its backend responsibilities are session lifecycle, QR generation, Firebase polling for claim status, and final bill/transaction submission.

4. **Laptop dashboard**
   - `laptop_firebase_portal.py` is the operator-facing entrypoint.
   - The `sasta_dmart/portal/` package includes Python templates and static assets, so this is a server-rendered dashboard rather than a client-heavy SPA.
   - Its role is to display transactions, session state, and diagnostics from Firebase; it does not participate in login or claim actions.

5. **Public claim page**
   - `public_claim/` is a static web app served from a canonical HTTPS origin such as `claim.yourdomain.com`.
   - It contains the customer flow for QR-based session claim and a separate purchase history page (`history.html` / `history.js`).
   - The build step (`public_claim/build_config.py`) injects Firebase web config into a generated `config.js`, so the hosted site is deployment-configured rather than server-rendered.

6. **Shared application logic**
   - `sasta_dmart/firebase.py` centralizes Firebase access, likely separating Admin SDK use on Pi/laptop from web-client Firebase usage.
   - `sasta_dmart/sessions.py` encapsulates session token creation and lifecycle transitions.
   - `sasta_dmart/transactions.py` encapsulates bill/transaction creation and persistence.
   - `sasta_dmart/config.py` holds runtime configuration sourced from environment variables.
   - These modules form the core domain layer shared by the Pi and laptop apps.

7. **Product and barcode subsystem**
   - `sasta_dmart/products.json` is the product catalog source of truth.
   - `sasta_dmart/product_catalog.py` loads and validates catalog data.
   - `sasta_dmart/barcodes.py` and `generate_barcodes.py` handle barcode generation and catalog-upsert workflows.
   - This subsystem feeds the Pi scanner and keeps product identity, pricing, and barcode representation consistent.

8. **Session and claim data flow**
   - The Pi creates a high-entropy token and writes a `login_sessions/<token>` record to Firebase RTDB.
   - The QR code points to the public claim domain with that token.
   - After Google sign-in on the hosted page, the phone client verifies the token and performs an atomic `pending -> claimed` transition.
   - The Pi polls Firebase for that state change and then associates the checkout session with the claimed user.

9. **Transaction and history flow**
   - Once checkout is complete, the Pi writes a final transaction record to Firebase.
   - The README shows a dual-branch model: a closed `transactions` branch and a customer-readable `customer_transactions/<uid>` branch.
   - `public_claim/history.html` reads purchase history for the signed-in customer from the customer-facing branch.

10. **Security and access control**
   - RTDB rules in `docs/firebase-rtdb-rules.json` are a central part of the architecture.
   - Client-side claim flows require authentication, session claims are atomic, and transaction storage is not broadly client-writable.
   - The Pi and laptop use the Firebase Admin SDK via a service account, so they bypass client RTDB restrictions while staying trusted.

11. **Deployment/configuration model**
   - Runtime settings are environment-driven: Firebase DB URL, service account path, public claim base URL, laptop dashboard URL, and Pi node name.
   - The hosted claim page has its own Firebase web-app config variables and must be rebuilt/redeployed when those values change.
   - The Pi QR codes must always use the canonical public claim origin, which is a key architectural boundary.

12. **Tooling and validation**
   - `requirements-pi.txt`, `requirements-laptop.txt`, and `requirements-dev.txt` split dependencies by runtime.
   - The `tests/` directory covers catalog, barcode generation, sessions, transactions, portal behavior, public claim state/history, and build/config generation.
   - This suggests the repo treats each subsystem as independently testable, with Firebase state and generated artifacts being important integration points.

Main entry points: [`pi_checkout_gui_firebase.py`](https://github.com/kratospidey/sasta_dmart/blob/main/pi_checkout_gui_firebase.py) (Checkout app), [`sasta_dmart/firebase.py`](https://github.com/kratospidey/sasta_dmart/blob/main/sasta_dmart/firebase.py) (Firebase RTDB), [`public_claim/index.html`](https://github.com/kratospidey/sasta_dmart/blob/main/public_claim/index.html) (Claim site), [`sasta_dmart/config.py`](https://github.com/kratospidey/sasta_dmart/blob/main/sasta_dmart/config.py) (Runtime config)

## Diagram (Mermaid)

```mermaid
flowchart TD

subgraph group_runtime_pi["Pi kiosk"]
  node_pi_app["Checkout app<br/>entrypoint"]
  node_pi_camera(("Camera scan<br/>device-ui"))
  node_pi_session{{"Session lifecycle<br/>workflow<br/>[sessions.py]"}}
  node_pi_txn[("Bill writer<br/>workflow<br/>[transactions.py]")]
  node_pi_qr["QR claim link<br/>integration"]
end

subgraph group_runtime_laptop["Laptop portal"]
  node_laptop_app["Portal app<br/>entrypoint"]
  node_portal_pkg["Portal UI<br/>web-app<br/>[__init__.py]"]
  node_portal_template["Templates<br/>view-layer<br/>[dashboard.html]"]
  node_portal_static["Portal assets<br/>static-ui<br/>[portal.js]"]
end

subgraph group_runtime_public["Public claim"]
  node_claim_app["Claim site<br/>static-web<br/>[index.html]"]
  node_claim_logic{{"Claim state<br/>client-state<br/>[claim_state.mjs]"}}
  node_claim_auth(("Google sign-in<br/>auth-flow<br/>[app.js]"))
  node_history_page["History page<br/>customer-ui<br/>[history.html]"]
  node_history_logic{{"History state<br/>client-state<br/>[history_state.mjs]"}}
end

subgraph group_shared_core["Shared core"]
  node_firebase_core[("Firebase RTDB<br/>integration-backend<br/>[firebase.py]")]
  node_catalog[("Product catalog<br/>domain-data<br/>[products.json]")]
  node_catalog_logic{{"Catalog loader<br/>domain-service<br/>[product_catalog.py]"}}
  node_barcode_tools["Barcode tools<br/>tooling<br/>[barcodes.py]"]
  node_config["Runtime config<br/>[config.py]"]
end

subgraph group_ops_docs["Ops and rules"]
  node_rules["RTDB rules<br/>security"]
  node_build_cfg["Web build<br/>deployment<br/>[build_config.py]"]
end

node_pi_app -->|"scan items"| node_pi_camera
node_pi_app -->|"create claim"| node_pi_session
node_pi_app -->|"show QR"| node_pi_qr
node_pi_session -->|"write session"| node_firebase_core
node_pi_txn -->|"write txn"| node_firebase_core
node_pi_qr -->|"open link"| node_claim_app
node_laptop_app -->|"serve dashboard"| node_portal_pkg
node_portal_pkg -->|"render view"| node_portal_template
node_portal_pkg -->|"deliver assets"| node_portal_static
node_laptop_app -->|"read state"| node_firebase_core
node_claim_app -->|"sign in"| node_claim_auth
node_claim_app -->|"run state"| node_claim_logic
node_claim_logic -->|"claim session"| node_firebase_core
node_claim_app -->|"show history"| node_history_page
node_history_page -->|"load history"| node_history_logic
node_history_logic -->|"read purchases"| node_firebase_core
node_catalog_logic -->|"load data"| node_catalog
node_barcode_tools -->|"use catalog"| node_catalog_logic
node_pi_app -->|"price items"| node_catalog_logic
node_claim_app -->|"web config"| node_config
node_build_cfg -->|"generate config"| node_claim_app
node_rules -->|"guard access"| node_firebase_core
node_config -->|"env settings"| node_pi_app
node_config -->|"env settings"| node_laptop_app
node_config -->|"db settings"| node_firebase_core
node_pi_app -.->|"trust boundary"| node_rules
node_laptop_app -.->|"trust boundary"| node_rules

click node_pi_app "https://github.com/kratospidey/sasta_dmart/blob/main/pi_checkout_gui_firebase.py"
click node_pi_session "https://github.com/kratospidey/sasta_dmart/blob/main/sasta_dmart/sessions.py"
click node_pi_txn "https://github.com/kratospidey/sasta_dmart/blob/main/sasta_dmart/transactions.py"
click node_laptop_app "https://github.com/kratospidey/sasta_dmart/blob/main/laptop_firebase_portal.py"
click node_portal_pkg "https://github.com/kratospidey/sasta_dmart/blob/main/sasta_dmart/portal/__init__.py"
click node_portal_template "https://github.com/kratospidey/sasta_dmart/blob/main/sasta_dmart/portal/templates/dashboard.html"
click node_portal_static "https://github.com/kratospidey/sasta_dmart/blob/main/sasta_dmart/portal/static/portal.js"
click node_claim_app "https://github.com/kratospidey/sasta_dmart/blob/main/public_claim/index.html"
click node_claim_logic "https://github.com/kratospidey/sasta_dmart/blob/main/public_claim/claim_state.mjs"
click node_claim_auth "https://github.com/kratospidey/sasta_dmart/blob/main/public_claim/app.js"
click node_history_page "https://github.com/kratospidey/sasta_dmart/blob/main/public_claim/history.html"
click node_history_logic "https://github.com/kratospidey/sasta_dmart/blob/main/public_claim/history_state.mjs"
click node_firebase_core "https://github.com/kratospidey/sasta_dmart/blob/main/sasta_dmart/firebase.py"
click node_catalog "https://github.com/kratospidey/sasta_dmart/blob/main/sasta_dmart/products.json"
click node_catalog_logic "https://github.com/kratospidey/sasta_dmart/blob/main/sasta_dmart/product_catalog.py"
click node_barcode_tools "https://github.com/kratospidey/sasta_dmart/blob/main/sasta_dmart/barcodes.py"
click node_config "https://github.com/kratospidey/sasta_dmart/blob/main/sasta_dmart/config.py"
click node_rules "https://github.com/kratospidey/sasta_dmart/blob/main/docs/firebase-rtdb-rules.json"
click node_build_cfg "https://github.com/kratospidey/sasta_dmart/blob/main/public_claim/build_config.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_pi_app,node_pi_camera,node_pi_session,node_pi_txn,node_pi_qr toneBlue
class node_laptop_app,node_portal_pkg,node_portal_template,node_portal_static toneAmber
class node_claim_app,node_claim_logic,node_claim_auth,node_history_page,node_history_logic toneMint
class node_firebase_core,node_catalog,node_catalog_logic,node_barcode_tools,node_config toneRose
class node_rules,node_build_cfg toneIndigo
```

## Components

### Pi kiosk

Local checkout application running on the Raspberry Pi.

- [Checkout app](https://github.com/kratospidey/sasta_dmart/blob/main/pi_checkout_gui_firebase.py): entrypoint · Main Pi GUI entrypoint for scanning, session creation, and checkout flow. · `pi_checkout_gui_firebase.py`
- **Camera scan**: device-ui · Local camera preview and barcode scanning on the Pi.
- [Session lifecycle](https://github.com/kratospidey/sasta_dmart/blob/main/sasta_dmart/sessions.py): workflow · Creates checkout sessions, tracks claim state, and coordinates the QR token flow. · `sasta_dmart/sessions.py`
- [Bill writer](https://github.com/kratospidey/sasta_dmart/blob/main/sasta_dmart/transactions.py): workflow · Builds and submits final checkout transactions to Firebase. · `sasta_dmart/transactions.py`
- **QR claim link**: integration · Generates the public claim URL that encodes the pending session token.

### Laptop portal

Operator dashboard runtime for monitoring and diagnostics.

- [Portal app](https://github.com/kratospidey/sasta_dmart/blob/main/laptop_firebase_portal.py): entrypoint · Operator-facing dashboard entrypoint for monitoring and diagnostics. · `laptop_firebase_portal.py`
- [Portal UI](https://github.com/kratospidey/sasta_dmart/blob/main/sasta_dmart/portal/__init__.py): web-app · Server-rendered dashboard package with templates and static assets. · `sasta_dmart/portal/__init__.py`
- [Templates](https://github.com/kratospidey/sasta_dmart/blob/main/sasta_dmart/portal/templates/dashboard.html): view-layer · HTML templates for the dashboard shell and main view. · `sasta_dmart/portal/templates/dashboard.html`
- [Portal assets](https://github.com/kratospidey/sasta_dmart/blob/main/sasta_dmart/portal/static/portal.js): static-ui · Dashboard stylesheet and client script for the operator UI. · `sasta_dmart/portal/static/portal.js`

### Public claim

Hosted customer claim site for phone-based Google sign-in.

- [Claim site](https://github.com/kratospidey/sasta_dmart/blob/main/public_claim/index.html): static-web · Hosted customer-facing claim and history pages. · `public_claim/index.html`
- [Claim state](https://github.com/kratospidey/sasta_dmart/blob/main/public_claim/claim_state.mjs): client-state · Browser-side state machine for QR claim and session polling. · `public_claim/claim_state.mjs`
- [Google sign-in](https://github.com/kratospidey/sasta_dmart/blob/main/public_claim/app.js): auth-flow · Customer authentication and claim authorization on the hosted site. · `public_claim/app.js`
- [History page](https://github.com/kratospidey/sasta_dmart/blob/main/public_claim/history.html): customer-ui · Signed-in purchase history view for the customer branch. · `public_claim/history.html`
- [History state](https://github.com/kratospidey/sasta_dmart/blob/main/public_claim/history_state.mjs): client-state · Browser-side history loading and rendering logic. · `public_claim/history_state.mjs`

### Shared core

Reusable domain logic, Firebase access, and catalog data.

- [Firebase RTDB](https://github.com/kratospidey/sasta_dmart/blob/main/sasta_dmart/firebase.py): integration-backend · Shared realtime database boundary for sessions, claims, and transactions. · `sasta_dmart/firebase.py`
- [Product catalog](https://github.com/kratospidey/sasta_dmart/blob/main/sasta_dmart/products.json): domain-data · Source of truth for products, pricing, and barcode identity. · `sasta_dmart/products.json`
- [Catalog loader](https://github.com/kratospidey/sasta_dmart/blob/main/sasta_dmart/product_catalog.py): domain-service · Loads and validates catalog data used by checkout and barcode tooling. · `sasta_dmart/product_catalog.py`
- [Barcode tools](https://github.com/kratospidey/sasta_dmart/blob/main/sasta_dmart/barcodes.py): tooling · Barcode generation and catalog-upsert workflow. · `sasta_dmart/barcodes.py`
- [Runtime config](https://github.com/kratospidey/sasta_dmart/blob/main/sasta_dmart/config.py): config · Environment-driven settings for Firebase, origins, and node identity. · `sasta_dmart/config.py`

### Ops and rules

Rules, requirements, and deployment support artifacts.

- [RTDB rules](https://github.com/kratospidey/sasta_dmart/blob/main/docs/firebase-rtdb-rules.json): security · Realtime Database access policy for client claims and transaction visibility. · `docs/firebase-rtdb-rules.json`
- [Web build](https://github.com/kratospidey/sasta_dmart/blob/main/public_claim/build_config.py): deployment · Injects Firebase web config into the hosted claim site. · `public_claim/build_config.py`

## Connections

- Checkout app → Camera scan: scan items · The kiosk previews the camera and decodes barcodes during checkout.
- Checkout app → Session lifecycle: create claim · The kiosk starts pending sessions and monitors their lifecycle.
- Checkout app → QR claim link: show QR · The kiosk renders the public claim link for the pending session.
- Session lifecycle → Firebase RTDB: write session · Session tokens and claim state are stored in realtime database.
- Bill writer → Firebase RTDB: write txn · Completed bills are persisted as transaction records.
- QR claim link → Claim site: open link · The QR code directs the customer to the hosted claim origin.
- Portal app → Portal UI: serve dashboard · The laptop entrypoint renders the operator dashboard package.
- Portal UI → Templates: render view · The dashboard uses server-side templates for its main screens.
- Portal UI → Portal assets: deliver assets · Static browser assets power the operator UI behavior.
- Portal app → Firebase RTDB: read state · The dashboard reads sessions, transactions, and diagnostics from Firebase.
- Claim site → Google sign-in: sign in · The customer claim flow begins with Google authentication.
- Claim site → Claim state: run state · The browser app drives QR claim state transitions.
- Claim state → Firebase RTDB: claim session · The hosted page verifies the token and atomically claims the pending session.
- Claim site → History page: show history · The hosted site includes a separate customer purchase history page.
- History page → History state: load history · The history page uses client logic to fetch and render customer purchases.
- History state → Firebase RTDB: read purchases · History data is read from the customer-facing transaction branch.
- Catalog loader → Product catalog: load data · Catalog logic reads the product source of truth.
- Barcode tools → Catalog loader: use catalog · Barcode generation depends on validated product data and identity mapping.
- Checkout app → Catalog loader: price items · Checkout uses the shared catalog for item lookup and pricing.
- Claim site → Runtime config: web config · The hosted site is built with generated Firebase web configuration.
- Web build → Claim site: generate config · Build tooling injects deployment-specific configuration for the claim site.
- RTDB rules → Firebase RTDB: guard access · RTDB rules constrain client writes and customer-readable branches.
- Runtime config → Checkout app: env settings · The kiosk runtime is configured via environment variables.
- Runtime config → Portal app: env settings · The portal runtime is configured via environment variables.
- Runtime config → Firebase RTDB: db settings · Firebase URL and service-account settings are shared runtime inputs.
- Checkout app → RTDB rules: trust boundary · The Pi relies on Admin SDK access while client flows remain constrained by rules.
- Portal app → RTDB rules: trust boundary · The dashboard also uses trusted Firebase access outside client rules.
