# bhanusunrise/hms-backend architecture

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

- Interactive diagram: https://gitdiagram.com/bhanusunrise/hms-backend
- Repository: https://github.com/bhanusunrise/hms-backend

## Overview

1. This is a NestJS backend application in TypeScript. The architecture is module-based: `AppModule` composes a set of feature modules, and each feature typically exposes a controller, service, DTOs, and tests.

2. The request path is conventional NestJS: HTTP request enters a controller, DTOs define and validate input shape, the service contains business logic, and shared infrastructure is injected through modules/providers. There is no frontend in this repo; this is the server-side domain layer.

3. The core domain is a retail/inventory management system. The major business modules are `items`, `inventory`, `stock-batches`, `billing`, `returns`, `supplier_orders`, `supplier-returns`, `suppliers`, `transactions`, `accounts`, `locations`, `units`, and `users`. These look like the system’s operational backbone for catalog, stock, sales, purchasing, and returns.

4. Master/reference data is separated into its own modules: `items`, `units`, `locations`, `suppliers`, `accounts`, and `users`. These are likely used by the transactional modules as shared entities, so the diagram should treat them as foundational services rather than one-off endpoints.

5. Transactional flows are centered on sales and procurement. `billing` manages sales creation, `returns` handles customer return processing, `supplier_orders` covers inbound purchasing, and `supplier-returns` likely handles reverse logistics to suppliers. `transactions` appears to be a ledger or event record layer that other workflows may write to for financial tracking.

6. Stock management is split into `inventory` and `stock-batches`. That suggests inventory is the aggregate stock view, while stock batches capture lot-level or batch-level movement/history. `barcode` is a separate module, likely used to generate or resolve product identifiers for scanning workflows.

7. Analytics and operational visibility are handled by `dashboard` and `reports`. The reports module has distinct query DTOs for financial, sales, and inventory reports, so reporting is not just a generic endpoint; it is a dedicated read path over domain data with query-driven filtering.

8. Authentication and authorization are a clear cross-cutting subsystem. `auth` includes JWT utilities, `jwt.guard`, `roles.guard`, and decorators such as `public` and `roles`. This implies route-level access control with protected endpoints and role-based authorization layered on top of authentication.

9. Notifications and generated content are isolated in `mail` and `common/utils`. The mail module includes templates for OTP, password, and welcome messages, while shared utilities generate OTPs and passwords. That means account lifecycle flows probably trigger email-based onboarding and verification.

10. External/infrastructure integration is abstracted behind `supabase`, `storage`, and `mail`. `supabase` likely provides the backend data connection/provider, `storage` handles file/object storage concerns, and `mail` encapsulates outbound email delivery. These are the main non-domain boundaries in the system.

11. DTOs are used extensively, including nested DTO folders for complex payloads like sales, returns, and supplier returns. That indicates the API is designed around structured command objects rather than leaking persistence models directly into controllers.

12. Tests are present at both unit/service level and end-to-end level. The repo includes `*.spec.ts` files beside most modules plus a `test/app.e2e-spec.ts`, so architecture should show both isolated service behavior and full application wiring under Nest’s test harness.

13. In diagram terms, the most important boundaries are: HTTP/API layer, auth/guard layer, domain service layer, infrastructure adapters, and external systems. The runtime is Node.js with NestJS, and the application is organized as a cohesive monolith rather than microservices.

Main entry points: [`src/app.module.ts`](https://github.com/bhanusunrise/hms-backend/blob/main/src/app.module.ts) (AppModule), [`src/auth`](https://github.com/bhanusunrise/hms-backend/tree/main/src/auth) (Auth), [`src/billing`](https://github.com/bhanusunrise/hms-backend/tree/main/src/billing) (Billing), [`src/inventory`](https://github.com/bhanusunrise/hms-backend/tree/main/src/inventory) (Inventory)

## Diagram (Mermaid)

```mermaid
flowchart TD

subgraph group_runtime["Runtime & bootstrap"]
  node_main(("main.ts<br/>bootstrap<br/>[main.ts]"))
  node_app_module["AppModule<br/>composition<br/>[app.module.ts]"]
end

subgraph group_api["HTTP API"]
end

subgraph group_domain["Domain modules"]
  node_users["Users<br/>reference module"]
  node_items["Items<br/>catalog module"]
  node_units["Units<br/>reference module"]
  node_locations["Locations<br/>reference module"]
  node_suppliers["Suppliers<br/>reference module"]
  node_inventory["Inventory<br/>stock aggregate"]
  node_stock_batches["Stock batches<br/>batch ledger"]
  node_billing["Billing<br/>sales flow"]
  node_returns["Returns"]
  node_supplier_orders["Supplier orders<br/>purchasing flow"]
  node_supplier_returns["Supplier returns<br/>reverse logistics"]
  node_transactions["Transactions<br/>ledger"]
  node_barcode["Barcode<br/>identifier service"]
end

subgraph group_infra["Infrastructure"]
  node_auth["Auth<br/>security"]
  node_supabase[("Supabase<br/>data provider")]
  node_storage[("Storage")]
  node_mail["Mail"]
  node_common_utils["Common utils<br/>shared helpers"]
end

subgraph group_read["Read models"]
  node_dashboard["Dashboard<br/>operational view"]
  node_reports["Reports<br/>query API"]
end

node_main -->|"boots"| node_app_module
node_app_module -->|"imports"| node_auth
node_app_module -->|"imports"| node_supabase
node_app_module -->|"imports"| node_storage
node_app_module -->|"imports"| node_mail
node_app_module -->|"imports"| node_users
node_app_module -->|"imports"| node_items
node_app_module -->|"imports"| node_units
node_app_module -->|"imports"| node_locations
node_app_module -->|"imports"| node_suppliers
node_app_module -->|"imports"| node_inventory
node_app_module -->|"imports"| node_stock_batches
node_app_module -->|"imports"| node_billing
node_app_module -->|"imports"| node_returns
node_app_module -->|"imports"| node_supplier_orders
node_app_module -->|"imports"| node_supplier_returns
node_app_module -->|"imports"| node_transactions
node_app_module -->|"imports"| node_barcode
node_app_module -->|"imports"| node_dashboard
node_app_module -->|"imports"| node_reports
node_auth -->|"protects"| node_users
node_auth -->|"guards"| node_billing
node_auth -->|"guards"| node_returns
node_auth -->|"guards"| node_supplier_orders
node_auth -->|"guards"| node_supplier_returns
node_auth -->|"guards"| node_reports
node_mail -->|"uses"| node_common_utils
node_users -->|"triggers"| node_mail
node_billing -->|"reads"| node_items
node_billing -->|"updates"| node_inventory
node_billing -->|"writes"| node_transactions
node_returns -->|"references"| node_billing
node_returns -->|"updates"| node_inventory
node_returns -->|"writes"| node_transactions
node_supplier_orders -->|"reads"| node_suppliers
node_supplier_orders -->|"updates"| node_inventory
node_supplier_orders -->|"writes"| node_transactions
node_supplier_returns -->|"reads"| node_suppliers
node_supplier_returns -->|"updates"| node_inventory
node_supplier_returns -->|"writes"| node_transactions
node_inventory -->|"tracks"| node_stock_batches
node_stock_batches -->|"references"| node_items
node_stock_batches -->|"references"| node_locations
node_barcode -->|"encodes"| node_items
node_dashboard -->|"summarizes"| node_reports
node_reports -->|"queries"| node_billing
node_reports -->|"queries"| node_inventory
node_reports -->|"queries"| node_transactions

click node_main "https://github.com/bhanusunrise/hms-backend/blob/main/src/main.ts"
click node_app_module "https://github.com/bhanusunrise/hms-backend/blob/main/src/app.module.ts"
click node_auth "https://github.com/bhanusunrise/hms-backend/tree/main/src/auth"
click node_supabase "https://github.com/bhanusunrise/hms-backend/tree/main/src/supabase"
click node_storage "https://github.com/bhanusunrise/hms-backend/tree/main/src/storage"
click node_mail "https://github.com/bhanusunrise/hms-backend/tree/main/src/mail"
click node_common_utils "https://github.com/bhanusunrise/hms-backend/tree/main/src/common/utils"
click node_users "https://github.com/bhanusunrise/hms-backend/tree/main/src/users"
click node_items "https://github.com/bhanusunrise/hms-backend/tree/main/src/items"
click node_units "https://github.com/bhanusunrise/hms-backend/tree/main/src/units"
click node_locations "https://github.com/bhanusunrise/hms-backend/tree/main/src/locations"
click node_suppliers "https://github.com/bhanusunrise/hms-backend/tree/main/src/suppliers"
click node_inventory "https://github.com/bhanusunrise/hms-backend/tree/main/src/inventory"
click node_stock_batches "https://github.com/bhanusunrise/hms-backend/tree/main/src/stock-batches"
click node_billing "https://github.com/bhanusunrise/hms-backend/tree/main/src/billing"
click node_returns "https://github.com/bhanusunrise/hms-backend/tree/main/src/returns"
click node_supplier_orders "https://github.com/bhanusunrise/hms-backend/tree/main/src/supplier_orders"
click node_supplier_returns "https://github.com/bhanusunrise/hms-backend/tree/main/src/supplier-returns"
click node_transactions "https://github.com/bhanusunrise/hms-backend/tree/main/src/transactions"
click node_barcode "https://github.com/bhanusunrise/hms-backend/tree/main/src/barcode"
click node_dashboard "https://github.com/bhanusunrise/hms-backend/tree/main/src/dashboard"
click node_reports "https://github.com/bhanusunrise/hms-backend/tree/main/src/reports"

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_main,node_app_module toneBlue
class node_users,node_items,node_units,node_locations,node_suppliers,node_inventory,node_stock_batches,node_billing,node_returns,node_supplier_orders,node_supplier_returns,node_transactions,node_barcode toneMint
class node_auth,node_supabase,node_storage,node_mail,node_common_utils toneRose
class node_dashboard,node_reports toneIndigo
```

## Components

### Runtime & bootstrap

NestJS application bootstrap and top-level composition.

- [main.ts](https://github.com/bhanusunrise/hms-backend/blob/main/src/main.ts): bootstrap · Starts the NestJS application. · `src/main.ts`
- [AppModule](https://github.com/bhanusunrise/hms-backend/blob/main/src/app.module.ts): composition · Root module that wires the application together. · `src/app.module.ts`

### Domain modules

Core retail, inventory, purchasing, and reference-data modules.

- [Users](https://github.com/bhanusunrise/hms-backend/tree/main/src/users): reference module · User account management and identity data. · `src/users`
- [Items](https://github.com/bhanusunrise/hms-backend/tree/main/src/items): catalog module · Product/item master data. · `src/items`
- [Units](https://github.com/bhanusunrise/hms-backend/tree/main/src/units): reference module · Measurement units for catalog and inventory. · `src/units`
- [Locations](https://github.com/bhanusunrise/hms-backend/tree/main/src/locations): reference module · Physical or logical stock locations. · `src/locations`
- [Suppliers](https://github.com/bhanusunrise/hms-backend/tree/main/src/suppliers): reference module · Supplier master data. · `src/suppliers`
- [Inventory](https://github.com/bhanusunrise/hms-backend/tree/main/src/inventory): stock aggregate · Current stock and availability view. · `src/inventory`
- [Stock batches](https://github.com/bhanusunrise/hms-backend/tree/main/src/stock-batches): batch ledger · Lot/batch-level stock tracking and movement history. · `src/stock-batches`
- [Billing](https://github.com/bhanusunrise/hms-backend/tree/main/src/billing): sales flow · Sales creation and billing workflow. · `src/billing`
- [Returns](https://github.com/bhanusunrise/hms-backend/tree/main/src/returns): customer returns · Customer return processing. · `src/returns`
- [Supplier orders](https://github.com/bhanusunrise/hms-backend/tree/main/src/supplier_orders): purchasing flow · Inbound purchasing and supplier order processing. · `src/supplier_orders`
- [Supplier returns](https://github.com/bhanusunrise/hms-backend/tree/main/src/supplier-returns): reverse logistics · Returns sent back to suppliers. · `src/supplier-returns`
- [Transactions](https://github.com/bhanusunrise/hms-backend/tree/main/src/transactions): ledger · Financial or event record layer shared by workflows. · `src/transactions`
- [Barcode](https://github.com/bhanusunrise/hms-backend/tree/main/src/barcode): identifier service · Barcode generation or lookup for scanning workflows. · `src/barcode`

### Infrastructure

Cross-cutting auth, persistence, storage, email, and shared utilities.

- [Auth](https://github.com/bhanusunrise/hms-backend/tree/main/src/auth): security · JWT auth, guards, and role decorators. · `src/auth`
- [Supabase](https://github.com/bhanusunrise/hms-backend/tree/main/src/supabase): data provider · Backend data connection/provider layer. · `src/supabase`
- [Storage](https://github.com/bhanusunrise/hms-backend/tree/main/src/storage): object storage · File/object storage integration. · `src/storage`
- [Mail](https://github.com/bhanusunrise/hms-backend/tree/main/src/mail): email adapter · Outbound email delivery and templates. · `src/mail`
- [Common utils](https://github.com/bhanusunrise/hms-backend/tree/main/src/common/utils): shared helpers · OTP and password generation utilities. · `src/common/utils`

### Read models

Analytics and reporting endpoints built on domain data.

- [Dashboard](https://github.com/bhanusunrise/hms-backend/tree/main/src/dashboard): operational view · High-level operational visibility endpoints. · `src/dashboard`
- [Reports](https://github.com/bhanusunrise/hms-backend/tree/main/src/reports): query API · Financial, sales, and inventory reporting endpoints. · `src/reports`

## Connections

- main.ts → AppModule: boots · Application bootstrap loads the root module.
- AppModule → Auth: imports · Root module composes authentication.
- AppModule → Supabase: imports · Root module wires the data provider.
- AppModule → Storage: imports · Root module wires storage services.
- AppModule → Mail: imports · Root module wires email services.
- AppModule → Users: imports · Root module composes identity management.
- AppModule → Items: imports · Root module composes catalog management.
- AppModule → Units: imports · Root module composes unit reference data.
- AppModule → Locations: imports · Root module composes location reference data.
- AppModule → Suppliers: imports · Root module composes supplier reference data.
- AppModule → Inventory: imports · Root module composes inventory management.
- AppModule → Stock batches: imports · Root module composes batch tracking.
- AppModule → Billing: imports · Root module composes sales flow.
- AppModule → Returns: imports · Root module composes customer returns.
- AppModule → Supplier orders: imports · Root module composes purchasing flow.
- AppModule → Supplier returns: imports · Root module composes supplier returns.
- AppModule → Transactions: imports · Root module composes the transaction ledger.
- AppModule → Barcode: imports · Root module composes barcode services.
- AppModule → Dashboard: imports · Root module composes operational dashboard.
- AppModule → Reports: imports · Root module composes reporting.
- Auth → Users: protects · Authentication and roles gate user-facing routes.
- Auth → Billing: guards · Sales actions are protected by auth.
- Auth → Returns: guards · Return actions are protected by auth.
- Auth → Supplier orders: guards · Purchasing actions are protected by auth.
- Auth → Supplier returns: guards · Supplier return actions are protected by auth.
- Auth → Reports: guards · Reporting access is protected by auth.
- Mail → Common utils: uses · Mail flows use generated OTP/password content.
- Users → Mail: triggers · User lifecycle flows can send onboarding and verification email.
- Billing → Items: reads · Sales need catalog item data.
- Billing → Inventory: updates · Sales reduce available stock.
- Billing → Transactions: writes · Sales are recorded in the ledger.
- Returns → Billing: references · Returns are tied back to sales.
- Returns → Inventory: updates · Customer returns restore stock.
- Returns → Transactions: writes · Return events are written to the ledger.
- Supplier orders → Suppliers: reads · Purchasing uses supplier master data.
- Supplier orders → Inventory: updates · Inbound purchasing increases stock.
- Supplier orders → Transactions: writes · Purchase events are recorded.
- Supplier returns → Suppliers: reads · Reverse logistics uses supplier data.
- Supplier returns → Inventory: updates · Supplier returns adjust stock.
- Supplier returns → Transactions: writes · Supplier return events are recorded.
- Inventory → Stock batches: tracks · Inventory is backed by batch-level movement.
- Stock batches → Items: references · Batches are associated with items.
- Stock batches → Locations: references · Batch movements are tied to locations.
- Barcode → Items: encodes · Barcodes resolve product identifiers.
- Dashboard → Reports: summarizes · Dashboard surfaces report-like metrics.
- Reports → Billing: queries · Sales reports read billing data.
- Reports → Inventory: queries · Inventory reports read stock data.
- Reports → Transactions: queries · Financial reports read ledger data.
