# juvialski/invoiceapp architecture

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

- Interactive diagram: https://gitdiagram.com/juvialski/invoiceapp
- Repository: https://github.com/juvialski/invoiceapp

## Overview

#### Purpose and Entry Points
ENGORYX is a multi-tenant AEC operations platform spanning project cost control, invoices, cash settlement, payroll, engineering records, and field workflows. The browser client starts at `src/main.tsx`, composes providers/shell in `src/app/AppProviders.tsx` and `src/app/AppShell.tsx`, and resolves product areas through `src/app/routes/AppRouter.tsx`. `server.ts` hosts server-side AI/assistant capabilities.

#### Application Shell, Routing, and Modes
Route components separate major workspaces—dashboard, projects, invoices, expenses, payroll, cash/banking, reports, settings, and platform company administration. Navigation is modeled independently, allowing consistent route/menu/tour behavior. A demo runtime is a parallel, seeded experience rather than the production persistence path.
- `src/app/routes/AppRouter.tsx`
- `src/navigation/navigationModel.ts`
- `src/demo/DemoRoot.tsx`

#### Company Tenancy, Access, and Persistence Boundary
The frontend accesses Supabase through a shared client and domain persistence modules. Company context/access state gates UI behavior, while PostgreSQL migrations establish company tenancy, RBAC/RLS, storage policy, and RPC contracts. Database policy—not client checks alone—is the authority for isolation and privileged actions.
- `src/lib/supabase.ts`
- `src/context/CompanyAccessContext.tsx`
- `supabase/migrations/20260824093000_company_tenancy_rls_and_admin_rpcs.sql`

#### Operational Financial Domains
Invoices, project costs, expenses, payroll, and banking are separate source domains connected through controlled allocations and settlement evidence. Cash/banking imports and reconciles transactions; settlement can evidence invoice, payroll, and eligible expense payment without creating or rewriting project cost/payroll history. Domain libraries encapsulate calculations, workflows, and persistence behind UI workspaces.
- `src/lib/cashBanking.ts`
- `src/lib/financialSettlement.ts`
- `src/lib/payrollWorkflow.ts`

#### Project and Engineering Record Lifecycle
Project workspaces aggregate budgets, invoice allocations, labor, direct expenses, reports, documents, coordination, and site logs. Engineering documents support revision lineage, annotations, and storage controls; RFIs/submittals and daily logs are project-scoped lifecycle records with controller/access layers separating screen behavior from persistence rules.
- `src/components/projects/ProjectWorkspace.tsx`
- `src/features/engineering/useEngineeringDocumentsController.ts`
- `src/features/engineering/useDailySiteLogsController.ts`

#### Workspace Synchronization and Realtime Control Flow
Typical flow is route/UI → domain library/controller → Supabase tables/RPCs; cached workspace loading and synchronization manage refresh, sequencing, conflict handling, and realtime updates. The database includes realtime-oriented migrations for workspace, payroll, and banking changes, making server state the shared source of truth.
- `src/lib/workspaceSync.ts`
- `src/lib/workspaceLoadCache.ts`
- `supabase/migrations/20260823180000_workspace_sync_realtime.sql`

#### Guarded AI Assistant
The client assistant panel sends contextual requests and attachments to server handlers. Server code resolves company-scoped AI credentials, runs Gemini-compatible model/tool loops, validates and authorizes tools, and returns prepared actions. UI policy and confirmation gates require explicit human confirmation before writes; encrypted per-company credentials and optional global fallback are controlled by environment variables.
- `src/assistant/AssistantProvider.tsx`
- `src/server/assistant/assistantHandler.ts`
- `src/server/ai/companyAiRuntime.ts`

#### Runtime and Infrastructure
The stack is TypeScript/React with Vite for the client, Node-based server execution, and Supabase (PostgreSQL, RLS, RPCs, storage, realtime) as backend infrastructure. Gemini is the external AI integration. Engineering drawing functionality is represented in the UI layer and documented as PDF.js/Konva-based; database migrations are versioned under `supabase/migrations`.
- `vite.config.ts`
- `server.ts`
- `supabase/config.toml`

Main entry points: [`src/lib/workspaceSync.ts`](https://github.com/juvialski/invoiceapp/blob/main/src/lib/workspaceSync.ts) (Workspace sync), [`src/app/routes/AppRouter.tsx`](https://github.com/juvialski/invoiceapp/blob/main/src/app/routes/AppRouter.tsx) (Product router), [`src/components/projects/ProjectWorkspace.tsx`](https://github.com/juvialski/invoiceapp/blob/main/src/components/projects/ProjectWorkspace.tsx) (Project workspace), [`src/lib/financialSettlement.ts`](https://github.com/juvialski/invoiceapp/blob/main/src/lib/financialSettlement.ts) (Financial workflows)

## Diagram (Mermaid)

```mermaid
flowchart TD

subgraph group_client["Browser application"]
  node_entry["Client entry<br/>React bootstrap<br/>[main.tsx]"]
  node_providers["App providers<br/>context composition<br/>[AppProviders.tsx]"]
  node_shell["Application shell<br/>workspace chrome<br/>[AppShell.tsx]"]
  node_router["Product router<br/>route resolver<br/>[AppRouter.tsx]"]
  node_navigation["Navigation model<br/>navigation policy<br/>[navigationModel.ts]"]
  node_demo["Demo runtime<br/>seeded parallel mode<br/>[DemoRoot.tsx]"]
  node_company_access["Company access context<br/>tenant UI state"]
end

subgraph group_domains["Operational domains"]
  node_projects["Project workspace<br/>project aggregate UI"]
  node_engineering_docs["Engineering documents<br/>document controller"]
  node_site_logs["Daily site logs<br/>lifecycle controller"]
  node_financial_domains["Financial workflows<br/>domain libraries"]
  node_cash_banking["Cash and banking<br/>banking workflow<br/>[cashBanking.ts]"]
  node_payroll["Payroll workflow<br/>payroll domain<br/>[payrollWorkflow.ts]"]
end

subgraph group_backend["Supabase backend"]
  node_sync["Workspace sync<br/>cache and realtime coordinator<br/>[workspaceSync.ts]"]
  node_load_cache["Workspace load cache<br/>workspace cache"]
  node_supabase_client["Supabase client<br/>persistence gateway<br/>[supabase.ts]"]
  node_database[("PostgreSQL, RLS and RPCs<br/>shared system of record")]
  node_realtime_schema["Workspace realtime schema<br/>realtime migration"]
end

subgraph group_ai["Guarded AI assistant"]
  node_assistant_ui["Assistant provider<br/>client assistant policy"]
  node_server["Node server<br/>server runtime<br/>[server.ts]"]
  node_assistant_handler["Assistant handler<br/>request handler"]
  node_ai_runtime["Company AI runtime<br/>credential and tool runtime"]
  node_gemini{{"Gemini-compatible models<br/>external AI service"}}
end

node_entry --> node_providers
node_providers --> node_shell
node_shell --> node_router
node_shell -->|"menu and tour behavior"| node_navigation
node_providers -->|"tenant-aware UI"| node_company_access
node_router -.->|"parallel demo mode"| node_demo
node_router -->|"project routes"| node_projects
node_router -->|"financial routes"| node_financial_domains
node_projects -->|"project records"| node_engineering_docs
node_projects -->|"project records"| node_site_logs
node_projects -->|"load and synchronize"| node_sync
node_financial_domains -->|"settlement evidence"| node_cash_banking
node_financial_domains -->|"payroll settlement"| node_payroll
node_financial_domains -->|"persist workflows"| node_sync
node_sync -->|"cached loading"| node_load_cache
node_sync -->|"tables and RPCs"| node_supabase_client
node_supabase_client -->|"tenant-scoped persistence"| node_database
node_realtime_schema -->|"realtime changes"| node_database
node_database -.->|"shared state updates"| node_sync
node_company_access -.->|"RLS-enforced access"| node_database
node_assistant_ui -->|"context and attachments"| node_server
node_server -->|"assistant requests"| node_assistant_handler
node_assistant_handler -->|"authorized tool loop"| node_ai_runtime
node_ai_runtime -->|"model calls"| node_gemini
node_ai_runtime -->|"company-scoped actions"| node_supabase_client
node_assistant_handler -->|"prepared actions"| node_assistant_ui

click node_entry "https://github.com/juvialski/invoiceapp/blob/main/src/main.tsx"
click node_providers "https://github.com/juvialski/invoiceapp/blob/main/src/app/AppProviders.tsx"
click node_shell "https://github.com/juvialski/invoiceapp/blob/main/src/app/AppShell.tsx"
click node_router "https://github.com/juvialski/invoiceapp/blob/main/src/app/routes/AppRouter.tsx"
click node_navigation "https://github.com/juvialski/invoiceapp/blob/main/src/navigation/navigationModel.ts"
click node_demo "https://github.com/juvialski/invoiceapp/blob/main/src/demo/DemoRoot.tsx"
click node_company_access "https://github.com/juvialski/invoiceapp/blob/main/src/context/CompanyAccessContext.tsx"
click node_projects "https://github.com/juvialski/invoiceapp/blob/main/src/components/projects/ProjectWorkspace.tsx"
click node_engineering_docs "https://github.com/juvialski/invoiceapp/blob/main/src/features/engineering/useEngineeringDocumentsController.ts"
click node_site_logs "https://github.com/juvialski/invoiceapp/blob/main/src/features/engineering/useDailySiteLogsController.ts"
click node_financial_domains "https://github.com/juvialski/invoiceapp/blob/main/src/lib/financialSettlement.ts"
click node_cash_banking "https://github.com/juvialski/invoiceapp/blob/main/src/lib/cashBanking.ts"
click node_payroll "https://github.com/juvialski/invoiceapp/blob/main/src/lib/payrollWorkflow.ts"
click node_sync "https://github.com/juvialski/invoiceapp/blob/main/src/lib/workspaceSync.ts"
click node_load_cache "https://github.com/juvialski/invoiceapp/blob/main/src/lib/workspaceLoadCache.ts"
click node_supabase_client "https://github.com/juvialski/invoiceapp/blob/main/src/lib/supabase.ts"
click node_database "https://github.com/juvialski/invoiceapp/blob/main/supabase/migrations/20260824093000_company_tenancy_rls_and_admin_rpcs.sql"
click node_realtime_schema "https://github.com/juvialski/invoiceapp/blob/main/supabase/migrations/20260823180000_workspace_sync_realtime.sql"
click node_assistant_ui "https://github.com/juvialski/invoiceapp/blob/main/src/assistant/AssistantProvider.tsx"
click node_server "https://github.com/juvialski/invoiceapp/blob/main/server.ts"
click node_assistant_handler "https://github.com/juvialski/invoiceapp/blob/main/src/server/assistant/assistantHandler.ts"
click node_ai_runtime "https://github.com/juvialski/invoiceapp/blob/main/src/server/ai/companyAiRuntime.ts"

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_entry,node_providers,node_shell,node_router,node_navigation,node_demo,node_company_access toneBlue
class node_projects,node_engineering_docs,node_site_logs,node_financial_domains,node_cash_banking,node_payroll toneAmber
class node_sync,node_load_cache,node_supabase_client,node_database,node_realtime_schema toneMint
class node_assistant_ui,node_server,node_assistant_handler,node_ai_runtime,node_gemini toneRose
```

## Components

### Browser application

- [Client entry](https://github.com/juvialski/invoiceapp/blob/main/src/main.tsx): React bootstrap · `src/main.tsx`
- [App providers](https://github.com/juvialski/invoiceapp/blob/main/src/app/AppProviders.tsx): context composition · `src/app/AppProviders.tsx`
- [Application shell](https://github.com/juvialski/invoiceapp/blob/main/src/app/AppShell.tsx): workspace chrome · `src/app/AppShell.tsx`
- [Product router](https://github.com/juvialski/invoiceapp/blob/main/src/app/routes/AppRouter.tsx): route resolver · Selects dashboard, projects, financial, reporting, settings, and administration workspaces. · `src/app/routes/AppRouter.tsx`
- [Navigation model](https://github.com/juvialski/invoiceapp/blob/main/src/navigation/navigationModel.ts): navigation policy · Keeps menus and guided-tour behavior independent from routes. · `src/navigation/navigationModel.ts`
- [Demo runtime](https://github.com/juvialski/invoiceapp/blob/main/src/demo/DemoRoot.tsx): seeded parallel mode · Runs a seeded experience outside production persistence. · `src/demo/DemoRoot.tsx`
- [Company access context](https://github.com/juvialski/invoiceapp/blob/main/src/context/CompanyAccessContext.tsx): tenant UI state · `src/context/CompanyAccessContext.tsx`

### Operational domains

- [Project workspace](https://github.com/juvialski/invoiceapp/blob/main/src/components/projects/ProjectWorkspace.tsx): project aggregate UI · Aggregates budgets, allocations, labor, expenses, reports, documents, coordination, and site logs. · `src/components/projects/ProjectWorkspace.tsx`
- [Engineering documents](https://github.com/juvialski/invoiceapp/blob/main/src/features/engineering/useEngineeringDocumentsController.ts): document controller · Manages revision lineage, annotations, and controlled storage access. · `src/features/engineering/useEngineeringDocumentsController.ts`
- [Daily site logs](https://github.com/juvialski/invoiceapp/blob/main/src/features/engineering/useDailySiteLogsController.ts): lifecycle controller · `src/features/engineering/useDailySiteLogsController.ts`
- [Financial workflows](https://github.com/juvialski/invoiceapp/blob/main/src/lib/financialSettlement.ts): domain libraries · Invoices, costs, expenses, payroll, and banking remain separate source domains. · `src/lib/financialSettlement.ts`
- [Cash and banking](https://github.com/juvialski/invoiceapp/blob/main/src/lib/cashBanking.ts): banking workflow · Imports and reconciles transactions for settlement evidence. · `src/lib/cashBanking.ts`
- [Payroll workflow](https://github.com/juvialski/invoiceapp/blob/main/src/lib/payrollWorkflow.ts): payroll domain · `src/lib/payrollWorkflow.ts`

### Supabase backend

- [Workspace sync](https://github.com/juvialski/invoiceapp/blob/main/src/lib/workspaceSync.ts): cache and realtime coordinator · Handles loading, refresh sequencing, conflicts, and realtime updates. · `src/lib/workspaceSync.ts`
- [Workspace load cache](https://github.com/juvialski/invoiceapp/blob/main/src/lib/workspaceLoadCache.ts): workspace cache · `src/lib/workspaceLoadCache.ts`
- [Supabase client](https://github.com/juvialski/invoiceapp/blob/main/src/lib/supabase.ts): persistence gateway · `src/lib/supabase.ts`
- [PostgreSQL, RLS and RPCs](https://github.com/juvialski/invoiceapp/blob/main/supabase/migrations/20260824093000_company_tenancy_rls_and_admin_rpcs.sql): shared system of record · Database policy is the authority for tenant isolation and privileged actions. · `supabase/migrations/20260824093000_company_tenancy_rls_and_admin_rpcs.sql`
- [Workspace realtime schema](https://github.com/juvialski/invoiceapp/blob/main/supabase/migrations/20260823180000_workspace_sync_realtime.sql): realtime migration · `supabase/migrations/20260823180000_workspace_sync_realtime.sql`

### Guarded AI assistant

- [Assistant provider](https://github.com/juvialski/invoiceapp/blob/main/src/assistant/AssistantProvider.tsx): client assistant policy · Requires human confirmation before write actions. · `src/assistant/AssistantProvider.tsx`
- [Node server](https://github.com/juvialski/invoiceapp/blob/main/server.ts): server runtime · `server.ts`
- [Assistant handler](https://github.com/juvialski/invoiceapp/blob/main/src/server/assistant/assistantHandler.ts): request handler · `src/server/assistant/assistantHandler.ts`
- [Company AI runtime](https://github.com/juvialski/invoiceapp/blob/main/src/server/ai/companyAiRuntime.ts): credential and tool runtime · Resolves company credentials and validates authorized tool loops. · `src/server/ai/companyAiRuntime.ts`
- **Gemini-compatible models**: external AI service

## Connections

- Client entry → App providers
- App providers → Application shell
- Application shell → Product router
- Application shell → Navigation model: menu and tour behavior
- App providers → Company access context: tenant-aware UI
- Product router → Demo runtime: parallel demo mode
- Product router → Project workspace: project routes
- Product router → Financial workflows: financial routes
- Project workspace → Engineering documents: project records
- Project workspace → Daily site logs: project records
- Project workspace → Workspace sync: load and synchronize
- Financial workflows → Cash and banking: settlement evidence
- Financial workflows → Payroll workflow: payroll settlement
- Financial workflows → Workspace sync: persist workflows
- Workspace sync → Workspace load cache: cached loading
- Workspace sync → Supabase client: tables and RPCs
- Supabase client → PostgreSQL, RLS and RPCs: tenant-scoped persistence
- Workspace realtime schema → PostgreSQL, RLS and RPCs: realtime changes
- PostgreSQL, RLS and RPCs → Workspace sync: shared state updates
- Company access context → PostgreSQL, RLS and RPCs: RLS-enforced access
- Assistant provider → Node server: context and attachments
- Node server → Assistant handler: assistant requests
- Assistant handler → Company AI runtime: authorized tool loop
- Company AI runtime → Gemini-compatible models: model calls
- Company AI runtime → Supabase client: company-scoped actions
- Assistant handler → Assistant provider: prepared actions
