How sirtimi/hefa works
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: app.module.ts API app · page.tsx Web app · index.ts Shared pkg · queue.module.ts Async jobs
Components 23
API
NestJS backend and data platform
- API app · Application bootstrap and module composition
apps/api/src/app.module.ts - Auth · JWT sessions, cookies, roles, and guards
apps/api/src/auth/auth.module.ts - Users · User profile and account domain
apps/api/src/users/users.module.ts - Catalog · Browse, merchant catalog, services, home feed, content pages, wishlist, and promotions
apps/api/src/catalog/catalog.module.ts - Orders · Cart, checkout, order lifecycle, admin order access
apps/api/src/orders/orders.module.ts - Payments · Payment initiation, MPesa integration, webhooks, reconciliation
apps/api/src/payments/payments.module.ts - Settlement · Payouts, wallet, ledger, commissions, refunds, disputes
apps/api/src/wallet/wallet.module.ts - Delivery · Dispatch, delivery orchestration, providers, and routing
apps/api/src/delivery-orchestration/delivery-orchestration.module.ts - KYC · Merchant and driver verification flows
apps/api/src/kyc/kyc.module.ts - Personas · Merchant, driver, fleet-owner, and admin operations
apps/api/src/merchant/merchant.module.ts - Async jobs · Queue infrastructure, webhook workers, notification workers, delivery workers
apps/api/src/queue/queue.module.ts - Integrations · Cloudinary, GCS, maps, termii/email/sms, delivery providers, webhooks
apps/api/src/storage/gcs.service.ts - Platform · Prisma, metrics, health, security middleware, Sentry, uploads
apps/api/src/prisma/prisma.module.ts
Web
Next.js client and mobile shell
- Web app · App Router client with server and client components
apps/web/src/app/page.tsx - Web auth · Auth bootstrap, session helpers, redirects, route protection
apps/web/src/lib/auth-bootstrap.ts - Storefront · Home, browse, search, categories, product and store pages
apps/web/src/app/c/[slug]/page.tsx - Checkout · Cart, checkout, order confirmation, tracking
apps/web/src/app/checkout/page.tsx - Account · Profile, orders, wishlist, notifications, settings
apps/web/src/app/account/page.tsx - Dashboards · Merchant, driver, fleet, and admin consoles
apps/web/src/app/admin/page.tsx - Mobile shell · Android and iOS packaging around the web app
apps/web/capacitor.config.ts
Shared
Cross-cutting package
- Shared pkg · Shared types and utilities used by app surfaces
packages/shared/src/index.ts
Ops
Deployment and platform support
- Ops layer · Cloud build, Docker, alerts, backups, and DB tooling
cloudbuild.yaml
Legacy
Deprecated messaging area
- Legacy messaging · Deprecated messaging surface kept for historical context
archive/messaging/messages/messages.module.ts
Connections 31
- 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