# mohammedbetkaoui/khayati-manager architecture

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

- Interactive diagram: https://gitdiagram.com/mohammedbetkaoui/khayati-manager
- Repository: https://github.com/mohammedbetkaoui/khayati-manager

## Overview

#### Purpose and Runtime Topology
This is a workshop/business-management desktop application covering sales, customers, inventory/production, suppliers, workers/payroll, expenses, invoices, analytics, reports, and settings. It combines a React frontend, NestJS backend, and Electron desktop host.

Entry points:
- Electron process and backend lifecycle: `electron/main.js`, `electron/backend-runtime.js`, `electron/preload.js`
- Backend HTTP application: `backend/src/main.ts`, `backend/src/app.module.ts`
- Frontend SPA bootstrap: `frontend/src/main.tsx`, `frontend/src/app/App.tsx`

#### Desktop Host and Process Boundary
Electron owns the native desktop window, splash/launcher behavior, update handling, and starting/stopping the bundled backend runtime. The preload layer is the security boundary for renderer-to-native APIs; the frontend should not directly depend on Node/Electron internals.

Key paths:
- `electron/main.js`
- `electron/preload.js`
- `electron/updater.js`

Control flow is Electron main process → local backend runtime → React renderer, with environment-specific frontend configuration in `frontend/.env.development` and `frontend/.env.production`.

#### Backend API and Domain Composition
The backend is a modular NestJS application. Controllers expose domain APIs, services implement workflows, DTOs define request/query boundaries, and entity directories model persisted business records. `AppModule` is the composition root.

Key paths:
- `backend/src/app.module.ts`
- `backend/src/analytics/analytics.controller.ts`
- `backend/src/common/enums/index.ts`

Shared enums and money handling establish cross-domain status/category/value conventions. API clients should submit DTO-shaped data and treat service-owned state transitions—such as cancellations, payments, and stock adjustments—as backend authority.

#### Sales, Orders, and Invoicing Flow
Customer and sales records support measurements, notes, sales invoices, line items, and payments. A separate orders domain supplies order/order-item records. The invoicing domain adds document sequencing, calculation, draft updates, cancellation, payment recording, invoice creation from orders, and PDF output.

Key paths:
- `backend/src/sales/sales.service.ts`
- `backend/src/orders/entities/order.entity.ts`
- `backend/src/invoices/invoices.service.ts`
- `backend/src/invoices/invoice-pdf.service.ts`

Typical flow: customer/order inputs → invoice creation and line calculation → persistent document number/payment state → optional PDF generation. Invoice-specific database preparation utilities indicate that invoice and invoice-payment persistence need normalization before calculations or output.

#### Inventory, Supply, and Production Flow
Inventory is the operational core for materials and finished goods. It models inventory items, suppliers, purchases, supplier advances/payments, stock movements, product variants, production batches, production materials, and material consumption. Finished products are handled through a dedicated service alongside general inventory workflows.

Key paths:
- `backend/src/inventory/inventory.service.ts`
- `backend/src/inventory/finished-products.service.ts`
- `backend/src/inventory/entities/production-batch.entity.ts`

Primary control flow: supplier purchase or stock adjustment changes inventory → production batch consumes materials → finished-product/product stock movements record output. Stock changes and production consumption should be treated as auditable domain operations, not independent frontend mutations.

#### Workforce, Payroll, Expenses, and Configuration
Workers have roles, attendance, and production records; payroll combines salary payments with advances, loans, and repayment/deduction records. Expenses are an independent financial ledger domain. Settings centralize workshop settings, piece prices, and production stages, making them upstream reference data for operations and compensation.

Key paths:
- `backend/src/workers/workers.service.ts`
- `backend/src/payroll/payroll.service.ts`
- `backend/src/expenses/expenses.service.ts`
- `backend/src/settings/settings.service.ts`

Payroll preparation routines suggest calculated payroll data is assembled from related worker-finance records before persistence or presentation.

#### Frontend Application and Reporting
The React renderer is organized around page-level operational screens and reusable feature components. Its API adapter is the frontend/backend boundary; page state should derive from backend records rather than duplicated local domain logic. Analytics and reports consume operational data for summaries and saved reporting.

Key paths:
- `frontend/src/app/lib/api.ts`
- `frontend/src/app/pages/stock-page.tsx`
- `frontend/src/app/pages/new-sale-page.tsx`
- `backend/src/analytics/analytics.service.ts`
- `backend/src/reports/reports.service.ts`

Material user workflows include new sales, stock/production operations, workers/payroll, expenses, and archival/profile views. The UI includes language and theme contexts, but business state remains centered in the NestJS domain services.

Main entry points: [`backend/src/app.module.ts`](https://github.com/mohammedbetkaoui/khayati-manager/blob/main/backend/src/app.module.ts) (Application composition), [`backend/src/analytics/analytics.service.ts`](https://github.com/mohammedbetkaoui/khayati-manager/blob/main/backend/src/analytics/analytics.service.ts) (Analytics and reports), [`frontend/src/app/lib/api.ts`](https://github.com/mohammedbetkaoui/khayati-manager/blob/main/frontend/src/app/lib/api.ts) (API adapter), [`backend/src/invoices/invoices.service.ts`](https://github.com/mohammedbetkaoui/khayati-manager/blob/main/backend/src/invoices/invoices.service.ts) (Invoicing)

## Diagram (Mermaid)

```mermaid
flowchart TD

subgraph group_desktop["Desktop host"]
  node_electron_main["Electron main process<br/>desktop lifecycle<br/>[main.js]"]
  node_backend_runtime["Bundled backend runtime<br/>local process<br/>[backend-runtime.js]"]
  node_preload["Preload bridge<br/>security boundary<br/>[preload.js]"]
  node_updater["Update handler<br/>desktop service<br/>[updater.js]"]
end

subgraph group_frontend["React renderer"]
  node_frontend_app["React application<br/>SPA shell<br/>[App.tsx]"]
  node_api_client["API adapter<br/>frontend boundary<br/>[api.ts]"]
  node_sales_ui["Sales workflow UI<br/>operational page<br/>[new-sale-page.tsx]"]
  node_stock_ui["Stock workflow UI<br/>operational page<br/>[stock-page.tsx]"]
end

subgraph group_backend["NestJS backend"]
  node_app_module["Application composition<br/>NestJS root module<br/>[app.module.ts]"]
  node_analytics_reports["Analytics and reports<br/>read-model services"]
end

subgraph group_operations["Operational domains"]
  node_sales["Sales and customers<br/>domain service<br/>[sales.service.ts]"]
  node_orders[("Orders<br/>domain model<br/>[order.entity.ts]")]
  node_invoices["Invoicing<br/>domain service"]
  node_invoice_pdf["Invoice PDF output<br/>document generator"]
  node_inventory["Inventory and supply<br/>domain service"]
  node_production[("Production batches<br/>persisted workflow")]
  node_finished_products["Finished products<br/>inventory service"]
  node_workforce_payroll["Workers and payroll<br/>financial operations<br/>[payroll.service.ts]"]
  node_expenses["Expenses ledger<br/>financial domain"]
  node_settings["Workshop settings<br/>reference-data service"]
end

node_electron_main -->|"starts and stops"| node_backend_runtime
node_electron_main -->|"loads"| node_preload
node_electron_main -->|"uses"| node_updater
node_preload -->|"approved native APIs"| node_frontend_app
node_frontend_app -->|"uses"| node_api_client
node_sales_ui -->|"submits sales"| node_api_client
node_stock_ui -->|"submits stock operations"| node_api_client
node_api_client -->|"local HTTP"| node_backend_runtime
node_backend_runtime -->|"runs API"| node_app_module
node_app_module -->|"composes"| node_sales
node_app_module -->|"composes"| node_orders
node_app_module -->|"composes"| node_invoices
node_app_module -->|"composes"| node_inventory
node_app_module -->|"composes"| node_workforce_payroll
node_app_module -->|"composes"| node_expenses
node_app_module -->|"composes"| node_settings
node_app_module -->|"composes"| node_analytics_reports
node_sales -->|"sales invoice inputs"| node_invoices
node_orders -->|"create invoice from order"| node_invoices
node_invoices -->|"generates"| node_invoice_pdf
node_inventory -->|"material availability and movements"| node_production
node_production -->|"records output stock"| node_finished_products
node_settings -.->|"production stages"| node_production
node_settings -.->|"piece-price inputs"| node_workforce_payroll
node_sales -->|"operational data"| node_analytics_reports
node_inventory -->|"operational data"| node_analytics_reports
node_workforce_payroll -->|"operational data"| node_analytics_reports
node_expenses -->|"operational data"| node_analytics_reports

click node_electron_main "https://github.com/mohammedbetkaoui/khayati-manager/blob/main/electron/main.js"
click node_backend_runtime "https://github.com/mohammedbetkaoui/khayati-manager/blob/main/electron/backend-runtime.js"
click node_preload "https://github.com/mohammedbetkaoui/khayati-manager/blob/main/electron/preload.js"
click node_updater "https://github.com/mohammedbetkaoui/khayati-manager/blob/main/electron/updater.js"
click node_frontend_app "https://github.com/mohammedbetkaoui/khayati-manager/blob/main/frontend/src/app/App.tsx"
click node_api_client "https://github.com/mohammedbetkaoui/khayati-manager/blob/main/frontend/src/app/lib/api.ts"
click node_sales_ui "https://github.com/mohammedbetkaoui/khayati-manager/blob/main/frontend/src/app/pages/new-sale-page.tsx"
click node_stock_ui "https://github.com/mohammedbetkaoui/khayati-manager/blob/main/frontend/src/app/pages/stock-page.tsx"
click node_app_module "https://github.com/mohammedbetkaoui/khayati-manager/blob/main/backend/src/app.module.ts"
click node_analytics_reports "https://github.com/mohammedbetkaoui/khayati-manager/blob/main/backend/src/analytics/analytics.service.ts"
click node_sales "https://github.com/mohammedbetkaoui/khayati-manager/blob/main/backend/src/sales/sales.service.ts"
click node_orders "https://github.com/mohammedbetkaoui/khayati-manager/blob/main/backend/src/orders/entities/order.entity.ts"
click node_invoices "https://github.com/mohammedbetkaoui/khayati-manager/blob/main/backend/src/invoices/invoices.service.ts"
click node_invoice_pdf "https://github.com/mohammedbetkaoui/khayati-manager/blob/main/backend/src/invoices/invoice-pdf.service.ts"
click node_inventory "https://github.com/mohammedbetkaoui/khayati-manager/blob/main/backend/src/inventory/inventory.service.ts"
click node_production "https://github.com/mohammedbetkaoui/khayati-manager/blob/main/backend/src/inventory/entities/production-batch.entity.ts"
click node_finished_products "https://github.com/mohammedbetkaoui/khayati-manager/blob/main/backend/src/inventory/finished-products.service.ts"
click node_workforce_payroll "https://github.com/mohammedbetkaoui/khayati-manager/blob/main/backend/src/payroll/payroll.service.ts"
click node_expenses "https://github.com/mohammedbetkaoui/khayati-manager/blob/main/backend/src/expenses/expenses.service.ts"
click node_settings "https://github.com/mohammedbetkaoui/khayati-manager/blob/main/backend/src/settings/settings.service.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_electron_main,node_backend_runtime,node_preload,node_updater toneBlue
class node_frontend_app,node_api_client,node_sales_ui,node_stock_ui toneAmber
class node_app_module,node_analytics_reports toneMint
class node_sales,node_orders,node_invoices,node_invoice_pdf,node_inventory,node_production,node_finished_products,node_workforce_payroll,node_expenses,node_settings toneRose
```

## Components

### Desktop host

- [Electron main process](https://github.com/mohammedbetkaoui/khayati-manager/blob/main/electron/main.js): desktop lifecycle · Owns windows, launcher behavior, and backend lifecycle. · `electron/main.js`
- [Bundled backend runtime](https://github.com/mohammedbetkaoui/khayati-manager/blob/main/electron/backend-runtime.js): local process · Local API process started and stopped by Electron. · `electron/backend-runtime.js`
- [Preload bridge](https://github.com/mohammedbetkaoui/khayati-manager/blob/main/electron/preload.js): security boundary · Exposes approved native APIs to the renderer. · `electron/preload.js`
- [Update handler](https://github.com/mohammedbetkaoui/khayati-manager/blob/main/electron/updater.js): desktop service · `electron/updater.js`

### React renderer

- [React application](https://github.com/mohammedbetkaoui/khayati-manager/blob/main/frontend/src/app/App.tsx): SPA shell · Renderer bootstrap and page composition. · `frontend/src/app/App.tsx`
- [API adapter](https://github.com/mohammedbetkaoui/khayati-manager/blob/main/frontend/src/app/lib/api.ts): frontend boundary · Maps page actions to backend API requests. · `frontend/src/app/lib/api.ts`
- [Sales workflow UI](https://github.com/mohammedbetkaoui/khayati-manager/blob/main/frontend/src/app/pages/new-sale-page.tsx): operational page · `frontend/src/app/pages/new-sale-page.tsx`
- [Stock workflow UI](https://github.com/mohammedbetkaoui/khayati-manager/blob/main/frontend/src/app/pages/stock-page.tsx): operational page · `frontend/src/app/pages/stock-page.tsx`

### NestJS backend

- [Application composition](https://github.com/mohammedbetkaoui/khayati-manager/blob/main/backend/src/app.module.ts): NestJS root module · Composes controllers and domain modules. · `backend/src/app.module.ts`
- [Analytics and reports](https://github.com/mohammedbetkaoui/khayati-manager/blob/main/backend/src/analytics/analytics.service.ts): read-model services · Builds operational summaries and reports. · `backend/src/analytics/analytics.service.ts`

### Operational domains

- [Sales and customers](https://github.com/mohammedbetkaoui/khayati-manager/blob/main/backend/src/sales/sales.service.ts): domain service · Owns sales, customer records, and payments. · `backend/src/sales/sales.service.ts`
- [Orders](https://github.com/mohammedbetkaoui/khayati-manager/blob/main/backend/src/orders/entities/order.entity.ts): domain model · Order and order-item source records for invoicing. · `backend/src/orders/entities/order.entity.ts`
- [Invoicing](https://github.com/mohammedbetkaoui/khayati-manager/blob/main/backend/src/invoices/invoices.service.ts): domain service · Calculates, sequences, cancels, and records invoice payments. · `backend/src/invoices/invoices.service.ts`
- [Invoice PDF output](https://github.com/mohammedbetkaoui/khayati-manager/blob/main/backend/src/invoices/invoice-pdf.service.ts): document generator · `backend/src/invoices/invoice-pdf.service.ts`
- [Inventory and supply](https://github.com/mohammedbetkaoui/khayati-manager/blob/main/backend/src/inventory/inventory.service.ts): domain service · Audits purchases, supplier finance, and stock movements. · `backend/src/inventory/inventory.service.ts`
- [Production batches](https://github.com/mohammedbetkaoui/khayati-manager/blob/main/backend/src/inventory/entities/production-batch.entity.ts): persisted workflow · Consumes materials and records batch output. · `backend/src/inventory/entities/production-batch.entity.ts`
- [Finished products](https://github.com/mohammedbetkaoui/khayati-manager/blob/main/backend/src/inventory/finished-products.service.ts): inventory service · Records finished-product stock operations. · `backend/src/inventory/finished-products.service.ts`
- [Workers and payroll](https://github.com/mohammedbetkaoui/khayati-manager/blob/main/backend/src/payroll/payroll.service.ts): financial operations · Combines worker records with pay, advances, loans, and deductions. · `backend/src/payroll/payroll.service.ts`
- [Expenses ledger](https://github.com/mohammedbetkaoui/khayati-manager/blob/main/backend/src/expenses/expenses.service.ts): financial domain · `backend/src/expenses/expenses.service.ts`
- [Workshop settings](https://github.com/mohammedbetkaoui/khayati-manager/blob/main/backend/src/settings/settings.service.ts): reference-data service · Maintains piece prices and production stages. · `backend/src/settings/settings.service.ts`

## Connections

- Electron main process → Bundled backend runtime: starts and stops
- Electron main process → Preload bridge: loads
- Electron main process → Update handler: uses
- Preload bridge → React application: approved native APIs
- React application → API adapter: uses
- Sales workflow UI → API adapter: submits sales
- Stock workflow UI → API adapter: submits stock operations
- API adapter → Bundled backend runtime: local HTTP
- Bundled backend runtime → Application composition: runs API
- Application composition → Sales and customers: composes
- Application composition → Orders: composes
- Application composition → Invoicing: composes
- Application composition → Inventory and supply: composes
- Application composition → Workers and payroll: composes
- Application composition → Expenses ledger: composes
- Application composition → Workshop settings: composes
- Application composition → Analytics and reports: composes
- Sales and customers → Invoicing: sales invoice inputs
- Orders → Invoicing: create invoice from order
- Invoicing → Invoice PDF output: generates
- Inventory and supply → Production batches: material availability and movements
- Production batches → Finished products: records output stock
- Workshop settings → Production batches: production stages
- Workshop settings → Workers and payroll: piece-price inputs
- Sales and customers → Analytics and reports: operational data
- Inventory and supply → Analytics and reports: operational data
- Workers and payroll → Analytics and reports: operational data
- Expenses ledger → Analytics and reports: operational data
