# aak-developers/centraleats-backend architecture

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

- Interactive diagram: https://gitdiagram.com/aak-developers/centraleats-backend
- Repository: https://github.com/aak-developers/centraleats-backend

## Overview

1. **Overall shape**
   This is a transactional **Node.js/TypeScript HTTP backend** built as a **modular monolith**. Business capabilities are isolated by domain under `src/modules/*`, while shared cross-cutting concerns live in `src/shared/*`.

2. **Request entry and runtime**
   The runtime starts in `src/main.ts`, which boots the Express application defined in `src/app.ts`. The app is the composition root: it wires middleware, module routes, error handling, logging, auth checks, and the WebSocket server.

3. **Layering inside each module**
   Each module follows a clean Architecture-style flow:
   - **presentation/http**: Express controllers, routes, and Zod schemas
   - **application/use-cases**: orchestration and business operations
   - **domain**: entities, repository contracts, and business rules
   - **infrastructure/persistence**: Prisma-backed repository implementations  
   Dependencies flow inward only; controllers call use cases, and use cases depend on repository interfaces rather than Prisma directly.

4. **Primary business modules**
   The implemented domains are:
   - **users**: local user records, Clerk webhook sync, role reset
   - **vendors**: vendor registration and listing
   - **catalog**: categories and products, including product creation/update and public catalog reads
   - **orders**: order creation, student/vendor order queries, order status updates, lifecycle rules
   - **auth**: authenticated identity/me responses and protected route examples  
   `notifications` and `payments` exist as scaffolding but are not yet fully implemented.

5. **Data access boundary**
   Persistence is abstracted through repository interfaces in each domain and implemented with **Prisma** in `src/modules/*/infrastructure/persistence`. The shared Prisma client sits in `src/infrastructure/database/prismaClient.ts`. The backing database is **Supabase PostgreSQL**, with migrations and seed data under `prisma/`.

6. **Domain model and business rules**
   The repo emphasizes richer domain logic rather than fat controllers. Notable domain rules include:
   - `Money` value object in `src/shared/domain/value-objects/Money.ts`
   - order lifecycle logic in `orders/domain/rules/OrderLifecycle.ts`
   - stock-related failure handling in `orders/domain/rules/StockError.ts`  
   This suggests important invariants are enforced in application/domain rather than only at the API layer.

7. **Authentication and authorization flow**
   Authentication is handled with **Clerk**. Incoming protected requests go through `requireAuth` and `requireRole` middlewares, and token validation is backed by Clerk token verification utilities in `src/infrastructure/external-services/clerk/`. Roles are enforced at the HTTP boundary, with the API exposing RBAC-sensitive endpoints for student/vendor/admin-style access.

8. **Webhook-driven user synchronization**
   Clerk user lifecycle events are received at the webhook route in the **users** module. The webhook controller verifies the request signature via **Svix**, then drives local user synchronization use cases. This is an important boundary: Clerk is the identity source, while the app keeps a local user projection for roles and business permissions.

9. **HTTP API surface**
   Routes are grouped per module, not globally by concern. The catalog, vendors, orders, users, and auth modules each expose their own route files under `presentation/http/routes`. Validation is done with **Zod** schemas near the controllers, keeping request parsing close to the transport layer.

10. **Shared infrastructure**
    `src/shared` contains cross-module utilities that affect architecture:
    - centralized error handling
    - request logging
    - auth/role middleware
    - Express type augmentation
    - storage abstraction and a Supabase storage adapter  
    These are shared boundaries, not domain logic, and are reused by multiple modules.

11. **External systems**
    The runtime depends materially on:
    - **Supabase/PostgreSQL** for transactional persistence
    - **Clerk** for authentication and identity events
    - **Svix** for webhook verification
    - a **WebSocket server** scaffold in `src/infrastructure/websocket/socketServer.ts` for real-time features such as order updates

12. **Operational and delivery concerns**
    The repo is containerized with a `Dockerfile` and `docker-compose.yml`, and has GitHub Actions workflows for build, CI, QA, dev, and deploy. Database migrations are first-class via Prisma, indicating schema changes are part of the delivery pipeline.

13. **Testing shape**
    The test suite is focused on **unit testing use cases and domain rules**, with mocks for repositories under `tests/mocks`. This reinforces the architecture: application and domain are intended to be testable without the database or HTTP layer.

Main entry points: [`src/app.ts`](https://github.com/aak-developers/centraleats-backend/blob/main/src/app.ts) (app), [`src/infrastructure/database/prismaClient.ts`](https://github.com/aak-developers/centraleats-backend/blob/main/src/infrastructure/database/prismaClient.ts) (Prisma client), [`src/modules/users/presentation/http`](https://github.com/aak-developers/centraleats-backend/tree/main/src/modules/users/presentation/http) (Users API), [`src/modules/orders/application/use-cases`](https://github.com/aak-developers/centraleats-backend/tree/main/src/modules/orders/application/use-cases) (Order use cases)

## Diagram (Mermaid)

```mermaid
flowchart TD

subgraph group_runtime["Runtime"]
  node_main["main<br/>bootstrap<br/>[main.ts]"]
  node_app["app<br/>express composition<br/>[app.ts]"]
end

subgraph group_shared["Shared"]
  node_shared_mw["Middleware<br/>http cross-cutting"]
  node_money{{"Money<br/>domain value object<br/>[Money.ts]"}}
  node_storage[("Storage")]
end

subgraph group_users["Users"]
  node_users_api["Users API<br/>http routes/controllers"]
  node_users_app["User use cases<br/>application orchestration"]
  node_users_repo[("User repo<br/>prisma persistence")]
end

subgraph group_catalog["Catalog"]
  node_catalog_api["Catalog API<br/>http routes/controllers"]
  node_catalog_app["Catalog use cases<br/>application/domain"]
  node_catalog_repo[("Catalog repo<br/>prisma persistence")]
end

subgraph group_orders["Orders"]
  node_orders_api["Orders API<br/>http routes/controllers"]
  node_orders_app["Order use cases<br/>application/domain"]
  node_orders_domain{{"Order rules<br/>domain model"}}
  node_orders_repo[("Order repo<br/>prisma persistence")]
end

subgraph group_vendors["Vendors"]
  node_vendors_api["Vendors API<br/>http routes/controllers"]
  node_vendors_app["Vendor use cases<br/>application orchestration"]
  node_vendors_repo[("Vendor repo<br/>prisma persistence")]
end

subgraph group_auth["Auth"]
  node_auth_api["Auth API<br/>http routes"]
end

subgraph group_external["External"]
  node_clerk["Clerk verify<br/>auth verification"]
  node_socket(("WebSocket<br/>realtime server<br/>[socketServer.ts]"))
  node_supabase[("Supabase<br/>postgres/storage")]
  node_svix["Svix<br/>webhook verification"]
  node_database[("Postgres<br/>database")]
end

subgraph group_delivery["Delivery"]
  node_prisma[("Prisma client<br/>database client<br/>[prismaClient.ts]")]
end

node_main -->|"boots"| node_app
node_app -->|"uses"| node_shared_mw
node_app -->|"mounts"| node_users_api
node_app -->|"mounts"| node_catalog_api
node_app -->|"mounts"| node_orders_api
node_app -->|"mounts"| node_vendors_api
node_app -->|"mounts"| node_auth_api
node_app -->|"starts"| node_socket
node_users_api -->|"calls"| node_users_app
node_users_api -->|"verifies"| node_clerk
node_users_app -->|"persists"| node_users_repo
node_users_api -.->|"reads/writes"| node_users_repo
node_catalog_api -->|"calls"| node_catalog_app
node_catalog_app -->|"persists"| node_catalog_repo
node_catalog_app -->|"uses"| node_money
node_orders_api -->|"calls"| node_orders_app
node_orders_app -->|"enforces"| node_orders_domain
node_orders_app -->|"persists"| node_orders_repo
node_orders_app -->|"uses"| node_money
node_vendors_api -->|"calls"| node_vendors_app
node_vendors_app -->|"persists"| node_vendors_repo
node_auth_api -->|"trusts"| node_clerk
node_users_repo -->|"uses"| node_prisma
node_catalog_repo -->|"uses"| node_prisma
node_orders_repo -->|"uses"| node_prisma
node_vendors_repo -->|"uses"| node_prisma
node_prisma -->|"connects"| node_database
node_storage -->|"adapts"| node_supabase
node_clerk -->|"verifies"| node_svix

click node_main "https://github.com/aak-developers/centraleats-backend/blob/main/src/main.ts"
click node_app "https://github.com/aak-developers/centraleats-backend/blob/main/src/app.ts"
click node_shared_mw "https://github.com/aak-developers/centraleats-backend/tree/main/src/shared/middlewares"
click node_money "https://github.com/aak-developers/centraleats-backend/blob/main/src/shared/domain/value-objects/Money.ts"
click node_storage "https://github.com/aak-developers/centraleats-backend/blob/main/src/shared/domain/ports/storage.repository.ts"
click node_prisma "https://github.com/aak-developers/centraleats-backend/blob/main/src/infrastructure/database/prismaClient.ts"
click node_clerk "https://github.com/aak-developers/centraleats-backend/tree/main/src/infrastructure/external-services/clerk"
click node_socket "https://github.com/aak-developers/centraleats-backend/blob/main/src/infrastructure/websocket/socketServer.ts"
click node_users_api "https://github.com/aak-developers/centraleats-backend/tree/main/src/modules/users/presentation/http"
click node_users_app "https://github.com/aak-developers/centraleats-backend/tree/main/src/modules/users/application/use-cases"
click node_users_repo "https://github.com/aak-developers/centraleats-backend/blob/main/src/modules/users/infrastructure/persistence/PrismaUserRepository.ts"
click node_catalog_api "https://github.com/aak-developers/centraleats-backend/tree/main/src/modules/catalog/presentation/http"
click node_catalog_app "https://github.com/aak-developers/centraleats-backend/tree/main/src/modules/catalog/application/use-cases"
click node_catalog_repo "https://github.com/aak-developers/centraleats-backend/tree/main/src/modules/catalog/infrastructure/persistence"
click node_orders_api "https://github.com/aak-developers/centraleats-backend/tree/main/src/modules/orders/presentation/http"
click node_orders_app "https://github.com/aak-developers/centraleats-backend/tree/main/src/modules/orders/application/use-cases"
click node_orders_domain "https://github.com/aak-developers/centraleats-backend/tree/main/src/modules/orders/domain"
click node_orders_repo "https://github.com/aak-developers/centraleats-backend/blob/main/src/modules/orders/infrastructure/persistence/PrismaOrderRepository.ts"
click node_vendors_api "https://github.com/aak-developers/centraleats-backend/tree/main/src/modules/vendors/presentation/http"
click node_vendors_app "https://github.com/aak-developers/centraleats-backend/tree/main/src/modules/vendors/application/use-cases"
click node_vendors_repo "https://github.com/aak-developers/centraleats-backend/blob/main/src/modules/vendors/infrastructure/persistence/PrismaVendorRepository.ts"
click node_auth_api "https://github.com/aak-developers/centraleats-backend/tree/main/src/modules/auth/presentation/http/routes"

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,node_auth_api toneBlue
class node_shared_mw,node_money,node_storage,node_clerk,node_socket,node_supabase,node_svix,node_database toneAmber
class node_users_api,node_users_app,node_users_repo,node_prisma toneMint
class node_catalog_api,node_catalog_app,node_catalog_repo toneRose
class node_orders_api,node_orders_app,node_orders_domain,node_orders_repo toneIndigo
class node_vendors_api,node_vendors_app,node_vendors_repo toneTeal
```

## Components

### Runtime

Process bootstrap and HTTP composition

- [main](https://github.com/aak-developers/centraleats-backend/blob/main/src/main.ts): bootstrap · Starts the Node process and loads the app · `src/main.ts`
- [app](https://github.com/aak-developers/centraleats-backend/blob/main/src/app.ts): express composition · Wires middleware, routes, auth, errors, and websocket server · `src/app.ts`

### Shared

Cross-cutting infrastructure and domain primitives

- [Middleware](https://github.com/aak-developers/centraleats-backend/tree/main/src/shared/middlewares): http cross-cutting · Request logging, auth, roles, and error handling · `src/shared/middlewares`
- [Money](https://github.com/aak-developers/centraleats-backend/blob/main/src/shared/domain/value-objects/Money.ts): domain value object · Shared monetary value abstraction · `src/shared/domain/value-objects/Money.ts`
- [Storage](https://github.com/aak-developers/centraleats-backend/blob/main/src/shared/domain/ports/storage.repository.ts): storage port/adapter · Shared storage contract and Supabase adapter · `src/shared/domain/ports/storage.repository.ts`

### Users

User sync and role management

- [Users API](https://github.com/aak-developers/centraleats-backend/tree/main/src/modules/users/presentation/http): http routes/controllers · User registration, sync webhook, and role reset endpoints · `src/modules/users/presentation/http`
- [User use cases](https://github.com/aak-developers/centraleats-backend/tree/main/src/modules/users/application/use-cases): application orchestration · Registers, syncs, and resets local users · `src/modules/users/application/use-cases`
- [User repo](https://github.com/aak-developers/centraleats-backend/blob/main/src/modules/users/infrastructure/persistence/PrismaUserRepository.ts): prisma persistence · Prisma-backed user persistence implementation · `src/modules/users/infrastructure/persistence/PrismaUserRepository.ts`

### Catalog

Categories and products

- [Catalog API](https://github.com/aak-developers/centraleats-backend/tree/main/src/modules/catalog/presentation/http): http routes/controllers · Catalog read and product write endpoints · `src/modules/catalog/presentation/http`
- [Catalog use cases](https://github.com/aak-developers/centraleats-backend/tree/main/src/modules/catalog/application/use-cases): application/domain · Creates and updates products through domain rules · `src/modules/catalog/application/use-cases`
- [Catalog repo](https://github.com/aak-developers/centraleats-backend/tree/main/src/modules/catalog/infrastructure/persistence): prisma persistence · Prisma-backed category and product repositories · `src/modules/catalog/infrastructure/persistence`

### Orders

Order lifecycle and queries

- [Orders API](https://github.com/aak-developers/centraleats-backend/tree/main/src/modules/orders/presentation/http): http routes/controllers · Order creation, history, and status endpoints · `src/modules/orders/presentation/http`
- [Order use cases](https://github.com/aak-developers/centraleats-backend/tree/main/src/modules/orders/application/use-cases): application/domain · Orchestrates order lifecycle and stock checks · `src/modules/orders/application/use-cases`
- [Order rules](https://github.com/aak-developers/centraleats-backend/tree/main/src/modules/orders/domain): domain model · Order entity plus lifecycle and stock errors · `src/modules/orders/domain`
- [Order repo](https://github.com/aak-developers/centraleats-backend/blob/main/src/modules/orders/infrastructure/persistence/PrismaOrderRepository.ts): prisma persistence · Prisma-backed order persistence implementation · `src/modules/orders/infrastructure/persistence/PrismaOrderRepository.ts`

### Vendors

Vendor registration and listing

- [Vendors API](https://github.com/aak-developers/centraleats-backend/tree/main/src/modules/vendors/presentation/http): http routes/controllers · Vendor registration and listing endpoints · `src/modules/vendors/presentation/http`
- [Vendor use cases](https://github.com/aak-developers/centraleats-backend/tree/main/src/modules/vendors/application/use-cases): application orchestration · Registers vendors through domain rules · `src/modules/vendors/application/use-cases`
- [Vendor repo](https://github.com/aak-developers/centraleats-backend/blob/main/src/modules/vendors/infrastructure/persistence/PrismaVendorRepository.ts): prisma persistence · Prisma-backed vendor persistence implementation · `src/modules/vendors/infrastructure/persistence/PrismaVendorRepository.ts`

### Auth

Identity and protected route surface

- [Auth API](https://github.com/aak-developers/centraleats-backend/tree/main/src/modules/auth/presentation/http/routes): http routes · Me / protected test route surface · `src/modules/auth/presentation/http/routes`

### External

Outside systems and services

- [Clerk verify](https://github.com/aak-developers/centraleats-backend/tree/main/src/infrastructure/external-services/clerk): auth verification · Validates Clerk tokens and webhook signatures · `src/infrastructure/external-services/clerk`
- [WebSocket](https://github.com/aak-developers/centraleats-backend/blob/main/src/infrastructure/websocket/socketServer.ts): realtime server · Socket server scaffold for live updates · `src/infrastructure/websocket/socketServer.ts`
- **Supabase**: postgres/storage · Transactional database and object storage backend
- **Svix**: webhook verification · Webhook signature verification provider
- **Postgres**: database · Supabase PostgreSQL backing store

### Delivery

Database, migrations, and test support

- [Prisma client](https://github.com/aak-developers/centraleats-backend/blob/main/src/infrastructure/database/prismaClient.ts): database client · Shared Prisma connection used by module repositories · `src/infrastructure/database/prismaClient.ts`

## Connections

- main → app: boots · Process entry initializes the Express composition root
- app → Middleware: uses · Applies shared HTTP middleware and error handling
- app → Users API: mounts · Registers module routes
- app → Catalog API: mounts · Registers module routes
- app → Orders API: mounts · Registers module routes
- app → Vendors API: mounts · Registers module routes
- app → Auth API: mounts · Registers module routes
- app → WebSocket: starts · Initializes realtime server scaffold
- Users API → User use cases: calls · HTTP controllers dispatch to use cases
- Users API → Clerk verify: verifies · Webhook controller checks Clerk/Svix signatures
- User use cases → User repo: persists · User orchestration reads and writes local users
- Users API → User repo: reads/writes · Presentation layer stays thin but uses repository-backed flows
- Catalog API → Catalog use cases: calls · Controllers trigger product and category flows
- Catalog use cases → Catalog repo: persists · Use cases depend on repository interfaces and Prisma adapters
- Catalog use cases → Money: uses · Product pricing relies on shared monetary primitive
- Orders API → Order use cases: calls · Controllers orchestrate order workflows
- Order use cases → Order rules: enforces · Application logic depends on domain rules and entities
- Order use cases → Order repo: persists · Order operations read and write through repository contracts
- Order use cases → Money: uses · Order totals use shared monetary primitive
- Vendors API → Vendor use cases: calls · Vendor endpoints dispatch to application layer
- Vendor use cases → Vendor repo: persists · Vendor registration is stored via repository implementation
- Auth API → Clerk verify: trusts · Protected route behavior depends on Clerk-backed auth state
- User repo → Prisma client: uses · Module repositories share the central Prisma client
- Catalog repo → Prisma client: uses · Module repositories share the central Prisma client
- Order repo → Prisma client: uses · Module repositories share the central Prisma client
- Vendor repo → Prisma client: uses · Module repositories share the central Prisma client
- Prisma client → Postgres: connects · Prisma targets the Supabase PostgreSQL database
- Storage → Supabase: adapts · Shared storage abstraction bridges to Supabase storage
- Clerk verify → Svix: verifies · Webhook boundary relies on Svix verification
