# sirtimi/hefa architecture

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

- Interactive diagram: https://gitdiagram.com/sirtimi/hefa
- Repository: https://github.com/sirtimi/hefa

## Overview

1. This is a monorepo with three main runtime surfaces: a NestJS backend in `apps/api`, a Next.js app in `apps/web`, and a small shared package in `packages/shared`. The backend is the system of record; the web app is the primary client UI; the shared package holds cross-cutting code used by both.

2. The backend is organized as feature modules, each with controllers, services, DTOs, and guards. Core domains include auth, users, catalog, orders, deliveries/dispatch, payments, payouts, wallet/ledger, KYC, promotions, notifications, disputes, reporting, merchant/driver/fleet-owner flows, and admin operations. The module layout suggests a multi-tenant marketplace and logistics platform rather than a single-purpose app.

3. The persistence layer is Prisma-based over PostgreSQL. `apps/api/prisma/schema.prisma` plus a long migration history show a heavily evolved schema covering merchants, drivers, fleet owners, orders, delivery orchestration, payments, refunds, notifications, audits, and content pages. `generated/prisma` is committed for client/runtime distribution, but the source of truth is the Prisma schema and migrations.

4. Authentication and authorization are a major architectural boundary. The API uses JWT access/refresh strategies, cookie handling, role guards, and specialized guards for merchant, driver, fleet-owner, and admin areas. The web app mirrors this with auth bootstrap, session helpers, and route-level authorization logic, indicating role-based UX and API access control across multiple user personas.

5. The frontend is a Next.js App Router application with distinct user-facing zones: shopping/browse flows, checkout, orders, account, merchant onboarding, driver onboarding, fleet dashboards, and admin consoles. Many pages have `.client.tsx` companions, showing a mix of server-rendered shells and client-heavy interactive screens. The same web app is also wrapped for mobile via Capacitor with native Android and iOS projects under `apps/web/android` and `apps/web/ios`.

6. Marketplace/catalog flows are split between public browsing and merchant management. On the API side, `catalog`, `services`, `wishlist`, `home-feed`, `content-pages`, and `promotions` support product discovery and merchant-managed inventory/content. On the web side, category pages, product detail pages, store pages, search, home feed sections, and wishlist/account pages consume that data to drive buyer-facing discovery.

7. Orders, payments, and wallet/ledger form a critical transactional pipeline. The order modules expose public, checkout, and admin endpoints; payments are split into a generic payments layer plus an MPesa integration; payouts and reconciliation are separate services; wallet and ledger modules provide read and operational access to balances and journal entries. The presence of refunds, disputes, commission handling, and tax snapshots indicates accounting-sensitive flows centered on order completion and settlement.

8. Delivery and dispatch are handled as a distinct subsystem with both orchestration and provider abstraction. `dispatch`, `deliveries`, `delivery-orchestration`, and `delivery-providers` separate business logic from external carrier integrations. There is an internal provider plus a Safeboda provider, routing/mapping code, timeout monitoring, fallback dispatch logic, and webhooks, which implies asynchronous assignment, status updates, and provider-specific normalization.

9. Asynchronous processing is a first-class pattern. The repo includes queue infrastructure, dedicated worker modules, webhook workers, notification workers, delivery orchestration workers, and offer expiry services. This means external events and delayed business actions are not handled inline; they are pushed through queues/workers to keep request paths responsive and to isolate retries/timeouts.

10. External integrations materially shape the architecture: Cloudinary for media storage, Google Maps for location and route/map experiences in the web and mobile UI, MPesa for payments/payouts, Safeboda for delivery logistics, and Termii/email/SMS providers for notifications. The API also exposes multiple webhook entry points, so inbound third-party events are a core part of the system boundary.

11. Cross-cutting infrastructure includes security, observability, and file handling. The API has request timing, security-event middleware, Sentry interception, Prisma exception filtering, health checks, and Prometheus-style metrics. File uploads are handled through dedicated upload/files/storage modules and GCS support, keeping binary assets separate from transactional data.

12. There is an ops layer outside the application code: Cloud Build files, Dockerfiles, monitoring alerts/prometheus config, backup scripts, and database seed/migration tooling. This suggests deployment is containerized and operationally managed around the API/web split, with local setup anchored on PostgreSQL and Prisma migrations.

13. `archive/messaging` contains older messaging controllers/services/tests and appears to be a deprecated or sidelined subsystem. It is useful for historical context but should not be treated as part of the current main runtime path unless you are specifically mapping legacy messaging behavior.

Main entry points: [`apps/api/src/app.module.ts`](https://github.com/sirtimi/hefa/blob/main/apps/api/src/app.module.ts) (API app), [`apps/web/src/app/page.tsx`](https://github.com/sirtimi/hefa/blob/main/apps/web/src/app/page.tsx) (Web app), [`packages/shared/src/index.ts`](https://github.com/sirtimi/hefa/blob/main/packages/shared/src/index.ts) (Shared pkg), [`apps/api/src/queue/queue.module.ts`](https://github.com/sirtimi/hefa/blob/main/apps/api/src/queue/queue.module.ts) (Async jobs)

## Diagram (Mermaid)

```mermaid
flowchart TD

subgraph group_api["API"]
  node_api_app["API app<br/>NestJS entry<br/>[app.module.ts]"]
  node_api_auth["Auth<br/>[auth.module.ts]"]
  node_api_users["Users<br/>identity module<br/>[users.module.ts]"]
  node_api_marketplace["Catalog<br/>marketplace module<br/>[catalog.module.ts]"]
  node_api_orders["Orders<br/>transaction module<br/>[orders.module.ts]"]
  node_api_payments["Payments<br/>[payments.module.ts]"]
  node_api_settlement["Settlement<br/>finance module<br/>[wallet.module.ts]"]
  node_api_delivery["Delivery<br/>dispatch module"]
  node_api_kyc["KYC<br/>compliance module<br/>[kyc.module.ts]"]
  node_api_personas["Personas<br/>role modules<br/>[merchant.module.ts]"]
  node_api_async["Async jobs<br/>queues/workers<br/>[queue.module.ts]"]
  node_api_integrations["Integrations<br/>provider adapters<br/>[gcs.service.ts]"]
  node_api_platform["Platform<br/>cross-cutting infra<br/>[prisma.module.ts]"]
end

subgraph group_web["Web"]
  node_web_app["Web app<br/>Next.js entry<br/>[page.tsx]"]
  node_web_auth["Web auth<br/>session layer<br/>[auth-bootstrap.ts]"]
  node_web_storefront["Storefront<br/>buyer zone<br/>[page.tsx]"]
  node_web_checkout["Checkout<br/>purchase zone<br/>[page.tsx]"]
  node_web_account["Account<br/>buyer zone<br/>[page.tsx]"]
  node_web_ops["Dashboards<br/>role zones<br/>[page.tsx]"]
  node_web_mobile["Mobile shell<br/>capacitor wrapper"]
end

subgraph group_shared["Shared"]
  node_shared_pkg["Shared pkg<br/>cross-package code<br/>[index.ts]"]
end

subgraph group_ops["Ops"]
  node_ops_layer["Ops layer<br/>deployment/monitoring<br/>[cloudbuild.yaml]"]
end

subgraph group_legacy["Legacy"]
  node_legacy_msg["Legacy messaging<br/>archived module<br/>[messages.module.ts]"]
end

node_api_app -->|"uses"| node_api_auth
node_api_app -->|"includes"| node_api_users
node_api_app -->|"includes"| node_api_marketplace
node_api_app -->|"includes"| node_api_orders
node_api_app -->|"includes"| node_api_payments
node_api_app -->|"includes"| node_api_delivery
node_api_app -->|"includes"| node_api_settlement
node_api_app -->|"includes"| node_api_personas
node_api_app -->|"uses"| node_api_async
node_api_app -->|"uses"| node_api_platform
node_api_marketplace -->|"uses"| node_api_integrations
node_api_orders -->|"calls"| node_api_payments
node_api_orders -->|"calls"| node_api_delivery
node_api_payments -->|"feeds"| node_api_settlement
node_api_delivery -->|"offloads"| node_api_async
node_api_integrations -->|"hooks"| node_api_async
node_web_app -->|"uses"| node_web_auth
node_web_app -->|"serves"| node_web_storefront
node_web_app -->|"serves"| node_web_checkout
node_web_app -->|"serves"| node_web_account
node_web_app -->|"serves"| node_web_ops
node_web_app -->|"packs"| node_web_mobile
node_web_storefront -->|"shares"| node_shared_pkg
node_web_checkout -->|"shares"| node_shared_pkg
node_web_account -->|"shares"| node_shared_pkg
node_web_ops -->|"shares"| node_shared_pkg
node_web_app -->|"consumes"| node_api_app
node_api_app -->|"consumes"| node_shared_pkg
node_ops_layer -->|"supports"| node_api_app
node_ops_layer -->|"supports"| node_web_app
node_legacy_msg -.->|"historical overlap"| node_api_async

click node_api_app "https://github.com/sirtimi/hefa/blob/main/apps/api/src/app.module.ts"
click node_api_auth "https://github.com/sirtimi/hefa/blob/main/apps/api/src/auth/auth.module.ts"
click node_api_users "https://github.com/sirtimi/hefa/blob/main/apps/api/src/users/users.module.ts"
click node_api_marketplace "https://github.com/sirtimi/hefa/blob/main/apps/api/src/catalog/catalog.module.ts"
click node_api_orders "https://github.com/sirtimi/hefa/blob/main/apps/api/src/orders/orders.module.ts"
click node_api_payments "https://github.com/sirtimi/hefa/blob/main/apps/api/src/payments/payments.module.ts"
click node_api_settlement "https://github.com/sirtimi/hefa/blob/main/apps/api/src/wallet/wallet.module.ts"
click node_api_delivery "https://github.com/sirtimi/hefa/blob/main/apps/api/src/delivery-orchestration/delivery-orchestration.module.ts"
click node_api_kyc "https://github.com/sirtimi/hefa/blob/main/apps/api/src/kyc/kyc.module.ts"
click node_api_personas "https://github.com/sirtimi/hefa/blob/main/apps/api/src/merchant/merchant.module.ts"
click node_api_async "https://github.com/sirtimi/hefa/blob/main/apps/api/src/queue/queue.module.ts"
click node_api_integrations "https://github.com/sirtimi/hefa/blob/main/apps/api/src/storage/gcs.service.ts"
click node_api_platform "https://github.com/sirtimi/hefa/blob/main/apps/api/src/prisma/prisma.module.ts"
click node_web_app "https://github.com/sirtimi/hefa/blob/main/apps/web/src/app/page.tsx"
click node_web_auth "https://github.com/sirtimi/hefa/blob/main/apps/web/src/lib/auth-bootstrap.ts"
click node_web_storefront "https://github.com/sirtimi/hefa/blob/main/apps/web/src/app/c/[slug]/page.tsx"
click node_web_checkout "https://github.com/sirtimi/hefa/blob/main/apps/web/src/app/checkout/page.tsx"
click node_web_account "https://github.com/sirtimi/hefa/blob/main/apps/web/src/app/account/page.tsx"
click node_web_ops "https://github.com/sirtimi/hefa/blob/main/apps/web/src/app/admin/page.tsx"
click node_web_mobile "https://github.com/sirtimi/hefa/blob/main/apps/web/capacitor.config.ts"
click node_shared_pkg "https://github.com/sirtimi/hefa/blob/main/packages/shared/src/index.ts"
click node_ops_layer "https://github.com/sirtimi/hefa/blob/main/cloudbuild.yaml"
click node_legacy_msg "https://github.com/sirtimi/hefa/blob/main/archive/messaging/messages/messages.module.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_api_app,node_api_auth,node_api_users,node_api_marketplace,node_api_orders,node_api_payments,node_api_settlement,node_api_delivery,node_api_kyc,node_api_personas,node_api_async,node_api_integrations,node_api_platform toneBlue
class node_web_app,node_web_auth,node_web_storefront,node_web_checkout,node_web_account,node_web_ops,node_web_mobile toneAmber
class node_shared_pkg toneMint
class node_ops_layer toneRose
class node_legacy_msg toneIndigo
```

## Components

### API

NestJS backend and data platform

- [API app](https://github.com/sirtimi/hefa/blob/main/apps/api/src/app.module.ts): NestJS entry · Application bootstrap and module composition · `apps/api/src/app.module.ts`
- [Auth](https://github.com/sirtimi/hefa/blob/main/apps/api/src/auth/auth.module.ts): auth boundary · JWT sessions, cookies, roles, and guards · `apps/api/src/auth/auth.module.ts`
- [Users](https://github.com/sirtimi/hefa/blob/main/apps/api/src/users/users.module.ts): identity module · User profile and account domain · `apps/api/src/users/users.module.ts`
- [Catalog](https://github.com/sirtimi/hefa/blob/main/apps/api/src/catalog/catalog.module.ts): marketplace module · Browse, merchant catalog, services, home feed, content pages, wishlist, and promotions · `apps/api/src/catalog/catalog.module.ts`
- [Orders](https://github.com/sirtimi/hefa/blob/main/apps/api/src/orders/orders.module.ts): transaction module · Cart, checkout, order lifecycle, admin order access · `apps/api/src/orders/orders.module.ts`
- [Payments](https://github.com/sirtimi/hefa/blob/main/apps/api/src/payments/payments.module.ts): payments module · Payment initiation, MPesa integration, webhooks, reconciliation · `apps/api/src/payments/payments.module.ts`
- [Settlement](https://github.com/sirtimi/hefa/blob/main/apps/api/src/wallet/wallet.module.ts): finance module · Payouts, wallet, ledger, commissions, refunds, disputes · `apps/api/src/wallet/wallet.module.ts`
- [Delivery](https://github.com/sirtimi/hefa/blob/main/apps/api/src/delivery-orchestration/delivery-orchestration.module.ts): dispatch module · Dispatch, delivery orchestration, providers, and routing · `apps/api/src/delivery-orchestration/delivery-orchestration.module.ts`
- [KYC](https://github.com/sirtimi/hefa/blob/main/apps/api/src/kyc/kyc.module.ts): compliance module · Merchant and driver verification flows · `apps/api/src/kyc/kyc.module.ts`
- [Personas](https://github.com/sirtimi/hefa/blob/main/apps/api/src/merchant/merchant.module.ts): role modules · Merchant, driver, fleet-owner, and admin operations · `apps/api/src/merchant/merchant.module.ts`
- [Async jobs](https://github.com/sirtimi/hefa/blob/main/apps/api/src/queue/queue.module.ts): queues/workers · Queue infrastructure, webhook workers, notification workers, delivery workers · `apps/api/src/queue/queue.module.ts`
- [Integrations](https://github.com/sirtimi/hefa/blob/main/apps/api/src/storage/gcs.service.ts): provider adapters · Cloudinary, GCS, maps, termii/email/sms, delivery providers, webhooks · `apps/api/src/storage/gcs.service.ts`
- [Platform](https://github.com/sirtimi/hefa/blob/main/apps/api/src/prisma/prisma.module.ts): cross-cutting infra · Prisma, metrics, health, security middleware, Sentry, uploads · `apps/api/src/prisma/prisma.module.ts`

### Web

Next.js client and mobile shell

- [Web app](https://github.com/sirtimi/hefa/blob/main/apps/web/src/app/page.tsx): Next.js entry · App Router client with server and client components · `apps/web/src/app/page.tsx`
- [Web auth](https://github.com/sirtimi/hefa/blob/main/apps/web/src/lib/auth-bootstrap.ts): session layer · Auth bootstrap, session helpers, redirects, route protection · `apps/web/src/lib/auth-bootstrap.ts`
- [Storefront](https://github.com/sirtimi/hefa/blob/main/apps/web/src/app/c/[slug]/page.tsx): buyer zone · Home, browse, search, categories, product and store pages · `apps/web/src/app/c/[slug]/page.tsx`
- [Checkout](https://github.com/sirtimi/hefa/blob/main/apps/web/src/app/checkout/page.tsx): purchase zone · Cart, checkout, order confirmation, tracking · `apps/web/src/app/checkout/page.tsx`
- [Account](https://github.com/sirtimi/hefa/blob/main/apps/web/src/app/account/page.tsx): buyer zone · Profile, orders, wishlist, notifications, settings · `apps/web/src/app/account/page.tsx`
- [Dashboards](https://github.com/sirtimi/hefa/blob/main/apps/web/src/app/admin/page.tsx): role zones · Merchant, driver, fleet, and admin consoles · `apps/web/src/app/admin/page.tsx`
- [Mobile shell](https://github.com/sirtimi/hefa/blob/main/apps/web/capacitor.config.ts): capacitor wrapper · Android and iOS packaging around the web app · `apps/web/capacitor.config.ts`

### Shared

Cross-cutting package

- [Shared pkg](https://github.com/sirtimi/hefa/blob/main/packages/shared/src/index.ts): cross-package code · Shared types and utilities used by app surfaces · `packages/shared/src/index.ts`

### Ops

Deployment and platform support

- [Ops layer](https://github.com/sirtimi/hefa/blob/main/cloudbuild.yaml): deployment/monitoring · Cloud build, Docker, alerts, backups, and DB tooling · `cloudbuild.yaml`

### Legacy

Deprecated messaging area

- [Legacy messaging](https://github.com/sirtimi/hefa/blob/main/archive/messaging/messages/messages.module.ts): archived module · Deprecated messaging surface kept for historical context · `archive/messaging/messages/messages.module.ts`

## Connections

- API app → Auth: uses · Bootstraps auth and guards
- API app → Users: includes · Loads user domain
- API app → Catalog: includes · Loads catalog and discovery
- API app → Orders: includes · Loads order pipeline
- API app → Payments: includes · Loads payment services
- API app → Delivery: includes · Loads dispatch and orchestration
- API app → Settlement: includes · Loads wallet and payouts
- API app → Personas: includes · Loads merchant/driver/fleet/admin flows
- API app → Async jobs: uses · Queues and workers
- API app → Platform: uses · Shared infra and persistence
- Catalog → Integrations: uses · Media, maps, and content providers
- Orders → Payments: calls · Checkout and payment linkage
- Orders → Delivery: calls · Order fulfillment handoff
- Payments → Settlement: feeds · Reconciliation and financial state
- Delivery → Async jobs: offloads · Orchestration workers and retries
- Integrations → Async jobs: hooks · Inbound webhook processing
- Web app → Web auth: uses · Session and route protection
- Web app → Storefront: serves · Buyer discovery UI
- Web app → Checkout: serves · Purchase and tracking flows
- Web app → Account: serves · Account and history surfaces
- Web app → Dashboards: serves · Role-based admin and partner portals
- Web app → Mobile shell: packs · Capacitor Android/iOS shells
- Storefront → Shared pkg: shares · Common types and helpers
- Checkout → Shared pkg: shares · Money, cart, and query helpers
- Account → Shared pkg: shares · Shared client utilities
- Dashboards → Shared pkg: shares · Shared UI/data contracts
- Web app → API app: consumes · Primary backend API
- API app → Shared pkg: consumes · Cross-package shared code
- Ops layer → API app: supports · Build, deploy, and runtime ops
- Ops layer → Web app: supports · Build and container delivery
- Legacy messaging → Async jobs: historical overlap · Older messaging path near current workers
