# yoursza1/paidly architecture

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

- Interactive diagram: https://gitdiagram.com/yoursza1/paidly
- Repository: https://github.com/yoursza1/paidly

## Overview

#### Purpose and entry points
Paidly is a multi-tenant business operations platform covering commercial documents, payments, subscriptions, inventory/POS, workforce/payroll, and platform administration. The browser app is a Vite/React SPA bootstrapped by `src/main.jsx` and routed from `src/App.jsx`. Production API requests are served either by Vercel function entrypoints under `api/` or the Express backend rooted at `server/src/index.js`.

#### Frontend application boundary
Pages compose domain UI, services, and Supabase-backed data access. Authentication, organization context, branding, and connectivity are global client concerns managed through contexts and protected shells:
- `src/contexts/AuthContext.jsx`
- `src/contexts/CompanyContext.jsx`
- `src/components/layout/AuthenticatedShell.jsx`

The client calls Supabase directly for much tenant CRUD and calls `/api` for privileged, payment, email, admin, and integration workflows. `src/api/customClient.js` is the principal direct data client; `src/lib/authedApiRequest.js` represents the authenticated API boundary.

#### Session, data, and realtime runtime
The frontend explicitly coordinates session recovery, request volume, persisted query data, mutations, and realtime rather than letting individual screens own connections. This is a cross-cutting runtime layer intended to prevent duplicate requests and reconnect storms:
- `src/core/auth/SessionCoordinator.ts`
- `src/core/runtime/RuntimeCoordinator.ts`
- `src/core/realtime/RealtimeManager.ts`

Supabase Auth establishes identity; query/cache and realtime reconciliation update UI state; session/wake recovery controls invalidation and retry behavior. Connection lifecycle state is exposed through `src/contexts/ConnectionLifecycleContext.jsx`.

#### Commercial document engine
Invoices, quotes, typed documents, expense claims, document sends, public sharing, PDF generation, reminders, and engagement events converge on a document-engine model. The frontend creates and transitions documents, while server routes perform authoritative event, reminder, send, and payment operations:
- `src/document-engine/documentCreateFlow.js`
- `src/document-engine/documentStateMachine.js`
- `server/src/documents/documentEventService.js`

Shared commercial calculations and status semantics prevent divergent browser/server behavior:
- `shared/commercial/calculateCommercialDocument.js`
- `shared/documents/documentEngine.js`
- `shared/documents/documentEvents.js`

Public viewers are separately exposed through `api/public-share.js` and corresponding public API handlers, avoiding authenticated UI paths.

#### Payments, billing, and entitlements
A payment-intent/payment-engine layer connects invoices and POS checkout to supported payment rails. Provider selection, validation, payment status transitions, and document-payment event bridging reside server-side:
- `server/src/payments/paymentEngine.js`
- `server/src/payments/paymentIntentService.js`
- `server/src/payments/documentPaymentEventBridge.js`

Subscription billing and feature authorization are separate but connected: PayFast recurring/ITN processing updates subscription state, then entitlements gate product surfaces:
- `server/src/billing/payfastItnPipeline.js`
- `server/src/billing/entitlements.js`
- `src/components/subscription/FeatureGate.jsx`

PayFast is the primary named billing provider; Ozow and card-terminal abstractions also exist.

#### POS, inventory, and workforce domains
POS is a dedicated operational subsystem with staff access, registers/sessions, checkout, returns, receipts, inventory synchronization, and optional Square/Yoco integrations:
- `server/src/pos/posApiRoutes.js`
- `server/src/pos/posSaleProcessor.js`
- `server/src/pos/posInventorySync.js`

Workforce includes employee provisioning, leave authorization, payroll pay runs, payslips, and public payslip access:
- `server/src/workforce/workforceRoutes.js`
- `server/src/payroll/payrollService.js`
- `shared/payroll/calculatePayroll.js`

These domains share tenant and entitlement boundaries but retain server-side authorization and transactional logic.

#### Persistence, infrastructure, and security
Supabase provides Postgres, Auth, Storage, Realtime, RLS, RPCs, migrations, and Edge Functions. Its evolving authoritative schema and tenant/security rules are captured in:
- `supabase/schema.postgres.sql`
- `supabase/migrations/20260619120000_multi_tenant_company_extension.sql`
- `supabase/migrations/20260906120000_commercial_document_engine.sql`

Vercel hosts the SPA and serverless API adapters such as `api/auth/[route].js` and `api/pos/[[...path]].js`; the Node backend can also run separately with CORS controls. External services include Supabase, PayFast, Resend/email delivery, Square, Yoco, and Sentry (`server/src/sentry.js`). Privileged server operations use service-role Supabase clients and enforce request validation, rate limiting, and authorization before database writes.

Main entry points: [`server/src/index.js`](https://github.com/yoursza1/paidly/blob/main/server/src/index.js) (Express backend), [`supabase/schema.postgres.sql`](https://github.com/yoursza1/paidly/blob/main/supabase/schema.postgres.sql) (Supabase), [`server/src/billing/payfastItnPipeline.js`](https://github.com/yoursza1/paidly/blob/main/server/src/billing/payfastItnPipeline.js) (Billing and entitlements), [`server/src/payments/paymentEngine.js`](https://github.com/yoursza1/paidly/blob/main/server/src/payments/paymentEngine.js) (Payment engine)

## Diagram (Mermaid)

```mermaid
flowchart TD

subgraph group_client["Browser SPA"]
  node_spa["React SPA<br/>Vite application<br/>[main.jsx]"]
  node_app_routes["App routes<br/>route composition<br/>[App.jsx]"]
  node_client_context["Tenant session context<br/>global client state<br/>[AuthContext.jsx]"]
  node_authenticated_shell["Authenticated shell<br/>protected layout"]
  node_runtime["Runtime coordination<br/>session and request runtime"]
  node_realtime["Realtime manager<br/>realtime reconciliation<br/>[RealtimeManager.ts]"]
  node_direct_data_client["Direct data client<br/>Supabase client<br/>[customClient.js]"]
  node_api_boundary["Authenticated API client<br/>privileged API boundary"]
  node_document_ui["Document engine<br/>commercial document flow"]
  node_feature_gate["Feature gate<br/>entitlement UI gate<br/>[FeatureGate.jsx]"]
end

subgraph group_server["Server APIs"]
  node_vercel_api{{"Vercel API adapters<br/>serverless entrypoints<br/>[&#91;route&#93;.js]"}}
  node_express_api{{"Express backend<br/>Node API runtime<br/>[index.js]"}}
  node_document_service["Document event service<br/>authoritative document operations"]
  node_payment_engine["Payment engine<br/>payment orchestration<br/>[paymentEngine.js]"]
  node_billing["Billing and entitlements<br/>subscription authorization"]
  node_pos["POS operations<br/>transactional POS subsystem"]
  node_workforce["Workforce and payroll<br/>workforce operations<br/>[payrollService.js]"]
end

subgraph group_platform["Supabase Platform"]
  node_supabase[("Supabase<br/>Postgres, Auth, Storage, Realtime")]
  node_payfast(("PayFast<br/>billing provider"))
  node_external_services["External integrations<br/>email, payments, observability<br/>[sentry.js]"]
end

node_spa -->|"boots"| node_app_routes
node_app_routes -->|"protects tenant pages"| node_authenticated_shell
node_client_context -->|"supplies identity and company"| node_authenticated_shell
node_spa -->|"coordinates lifecycle"| node_runtime
node_runtime -->|"controls reconnects"| node_realtime
node_runtime -->|"coordinates requests"| node_direct_data_client
node_direct_data_client -->|"tenant CRUD and auth"| node_supabase
node_realtime -->|"subscriptions"| node_supabase
node_document_ui -->|"authoritative operations"| node_api_boundary
node_api_boundary -->|"/api requests"| node_vercel_api
node_vercel_api -.->|"adapts routes"| node_express_api
node_express_api -->|"document routes"| node_document_service
node_express_api -->|"payment routes"| node_payment_engine
node_express_api -->|"billing webhooks"| node_billing
node_express_api -->|"POS routes"| node_pos
node_express_api -->|"workforce routes"| node_workforce
node_document_service -->|"events and documents"| node_supabase
node_payment_engine -->|"payment events"| node_document_service
node_payment_engine -->|"payment rails"| node_external_services
node_billing -->|"recurring payments and ITNs"| node_payfast
node_billing -->|"subscription state"| node_supabase
node_billing -->|"entitlements"| node_feature_gate
node_pos -->|"sales and inventory"| node_supabase
node_pos -->|"checkout payments"| node_payment_engine
node_workforce -->|"employees and pay runs"| node_supabase

click node_spa "https://github.com/yoursza1/paidly/blob/main/src/main.jsx"
click node_app_routes "https://github.com/yoursza1/paidly/blob/main/src/App.jsx"
click node_client_context "https://github.com/yoursza1/paidly/blob/main/src/contexts/AuthContext.jsx"
click node_authenticated_shell "https://github.com/yoursza1/paidly/blob/main/src/components/layout/AuthenticatedShell.jsx"
click node_runtime "https://github.com/yoursza1/paidly/blob/main/src/core/runtime/RuntimeCoordinator.ts"
click node_realtime "https://github.com/yoursza1/paidly/blob/main/src/core/realtime/RealtimeManager.ts"
click node_direct_data_client "https://github.com/yoursza1/paidly/blob/main/src/api/customClient.js"
click node_api_boundary "https://github.com/yoursza1/paidly/blob/main/src/lib/authedApiRequest.js"
click node_document_ui "https://github.com/yoursza1/paidly/blob/main/src/document-engine/documentCreateFlow.js"
click node_feature_gate "https://github.com/yoursza1/paidly/blob/main/src/components/subscription/FeatureGate.jsx"
click node_vercel_api "https://github.com/yoursza1/paidly/blob/main/api/auth/%5Broute%5D.js"
click node_express_api "https://github.com/yoursza1/paidly/blob/main/server/src/index.js"
click node_document_service "https://github.com/yoursza1/paidly/blob/main/server/src/documents/documentEventService.js"
click node_payment_engine "https://github.com/yoursza1/paidly/blob/main/server/src/payments/paymentEngine.js"
click node_billing "https://github.com/yoursza1/paidly/blob/main/server/src/billing/payfastItnPipeline.js"
click node_pos "https://github.com/yoursza1/paidly/blob/main/server/src/pos/posSaleProcessor.js"
click node_workforce "https://github.com/yoursza1/paidly/blob/main/server/src/payroll/payrollService.js"
click node_supabase "https://github.com/yoursza1/paidly/blob/main/supabase/schema.postgres.sql"
click node_external_services "https://github.com/yoursza1/paidly/blob/main/server/src/sentry.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_spa,node_app_routes,node_client_context,node_authenticated_shell,node_runtime,node_realtime,node_direct_data_client,node_api_boundary,node_document_ui,node_feature_gate toneBlue
class node_vercel_api,node_express_api,node_document_service,node_payment_engine,node_billing,node_pos,node_workforce toneAmber
class node_supabase,node_payfast,node_external_services toneMint
```

## Components

### Browser SPA

- [React SPA](https://github.com/yoursza1/paidly/blob/main/src/main.jsx): Vite application · Tenant business operations UI. · `src/main.jsx`
- [App routes](https://github.com/yoursza1/paidly/blob/main/src/App.jsx): route composition · `src/App.jsx`
- [Tenant session context](https://github.com/yoursza1/paidly/blob/main/src/contexts/AuthContext.jsx): global client state · Auth and company context drive protected UI. · `src/contexts/AuthContext.jsx`
- [Authenticated shell](https://github.com/yoursza1/paidly/blob/main/src/components/layout/AuthenticatedShell.jsx): protected layout · `src/components/layout/AuthenticatedShell.jsx`
- [Runtime coordination](https://github.com/yoursza1/paidly/blob/main/src/core/runtime/RuntimeCoordinator.ts): session and request runtime · Coordinates recovery, cache invalidation, and wake behavior. · `src/core/runtime/RuntimeCoordinator.ts`
- [Realtime manager](https://github.com/yoursza1/paidly/blob/main/src/core/realtime/RealtimeManager.ts): realtime reconciliation · `src/core/realtime/RealtimeManager.ts`
- [Direct data client](https://github.com/yoursza1/paidly/blob/main/src/api/customClient.js): Supabase client · Handles tenant CRUD from the browser. · `src/api/customClient.js`
- [Authenticated API client](https://github.com/yoursza1/paidly/blob/main/src/lib/authedApiRequest.js): privileged API boundary · `src/lib/authedApiRequest.js`
- [Document engine](https://github.com/yoursza1/paidly/blob/main/src/document-engine/documentCreateFlow.js): commercial document flow · Creates and transitions invoices, quotes, and related documents. · `src/document-engine/documentCreateFlow.js`
- [Feature gate](https://github.com/yoursza1/paidly/blob/main/src/components/subscription/FeatureGate.jsx): entitlement UI gate · `src/components/subscription/FeatureGate.jsx`

### Server APIs

- [Vercel API adapters](https://github.com/yoursza1/paidly/blob/main/api/auth/%5Broute%5D.js): serverless entrypoints · Hosts public and routed API adapters. · `api/auth/[route].js`
- [Express backend](https://github.com/yoursza1/paidly/blob/main/server/src/index.js): Node API runtime · Separately deployable authoritative backend. · `server/src/index.js`
- [Document event service](https://github.com/yoursza1/paidly/blob/main/server/src/documents/documentEventService.js): authoritative document operations · Processes sends, reminders, events, and payments. · `server/src/documents/documentEventService.js`
- [Payment engine](https://github.com/yoursza1/paidly/blob/main/server/src/payments/paymentEngine.js): payment orchestration · Selects rails and bridges payment status to documents. · `server/src/payments/paymentEngine.js`
- [Billing and entitlements](https://github.com/yoursza1/paidly/blob/main/server/src/billing/payfastItnPipeline.js): subscription authorization · Processes PayFast ITNs and derives feature access. · `server/src/billing/payfastItnPipeline.js`
- [POS operations](https://github.com/yoursza1/paidly/blob/main/server/src/pos/posSaleProcessor.js): transactional POS subsystem · Processes sales, returns, registers, and inventory sync. · `server/src/pos/posSaleProcessor.js`
- [Workforce and payroll](https://github.com/yoursza1/paidly/blob/main/server/src/payroll/payrollService.js): workforce operations · Authorizes workforce actions and runs payroll. · `server/src/payroll/payrollService.js`

### Supabase Platform

- [Supabase](https://github.com/yoursza1/paidly/blob/main/supabase/schema.postgres.sql): Postgres, Auth, Storage, Realtime · Tenant RLS, RPCs, schema, and managed runtime. · `supabase/schema.postgres.sql`
- **PayFast**: billing provider
- [External integrations](https://github.com/yoursza1/paidly/blob/main/server/src/sentry.js): email, payments, observability · Resend, Square, Yoco, and Sentry integrations. · `server/src/sentry.js`

## Connections

- React SPA → App routes: boots
- App routes → Authenticated shell: protects tenant pages
- Tenant session context → Authenticated shell: supplies identity and company
- React SPA → Runtime coordination: coordinates lifecycle
- Runtime coordination → Realtime manager: controls reconnects
- Runtime coordination → Direct data client: coordinates requests
- Direct data client → Supabase: tenant CRUD and auth
- Realtime manager → Supabase: subscriptions
- Document engine → Authenticated API client: authoritative operations
- Authenticated API client → Vercel API adapters: /api requests
- Vercel API adapters → Express backend: adapts routes
- Express backend → Document event service: document routes
- Express backend → Payment engine: payment routes
- Express backend → Billing and entitlements: billing webhooks
- Express backend → POS operations: POS routes
- Express backend → Workforce and payroll: workforce routes
- Document event service → Supabase: events and documents
- Payment engine → Document event service: payment events
- Payment engine → External integrations: payment rails
- Billing and entitlements → PayFast: recurring payments and ITNs
- Billing and entitlements → Supabase: subscription state
- Billing and entitlements → Feature gate: entitlements
- POS operations → Supabase: sales and inventory
- POS operations → Payment engine: checkout payments
- Workforce and payroll → Supabase: employees and pay runs
