# bolboceanuigor/espace architecture

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

- Interactive diagram: https://gitdiagram.com/bolboceanuigor/espace
- Repository: https://github.com/bolboceanuigor/espace

## Overview

1. **Repository shape**
   - This is a monorepo for a condominium/association management platform called **Espace**.
   - The repo is split into a **NestJS backend**, a **Next.js frontend**, a small **iOS companion app**, and **deployment/ops** assets.
   - The product is explicitly organized around three operating roles: **Superadmin**, **Administrator**, and **Resident**.

2. **Backend is a modular monolith**
   - `backend/src` is a large NestJS application, organized by domain modules rather than microservices.
   - The backend exposes the main API under `/api`, plus a WebSocket gateway for live events.
   - Each feature area has its own controller/service/module pair, with Prisma repositories used where the module needs direct data access.

3. **Persistence and schema management**
   - The backend uses **Prisma** with a **PostgreSQL** database.
   - `backend/prisma/schema.prisma` and the migration history are the source of truth for the data model.
   - There are seed scripts for demo, production base data, and production admin/superadmin accounts, which indicates the app depends heavily on seeded role and org setup.

4. **Security, auth, and access boundaries**
   - Auth is JWT-based, with local and Google strategies, password reset and email verification flows, and rate-limited auth endpoints.
   - Authorization is layered through **roles**, **permissions**, and multiple guards for:
     - superadmin access
     - organization scoping
     - resident-portal restrictions
     - demo-mode restrictions
   - A tenant/association context layer ensures requests are evaluated in the correct organization scope.

5. **Multi-tenant data model**
   - The app is effectively **organization-scoped**: most operational data belongs to one A.P.C./association.
   - Superadmin modules can see across organizations, while admin and resident modules are constrained by tenant context.
   - The backend contains explicit support for association context, support-session context, and organization limits, which are important boundaries for the diagram.

6. **Frontend is a role-based Next.js application**
   - The frontend uses **Next.js App Router** with a very large route tree.
   - It is split into:
     - **public/marketing pages**
     - **auth flows**
     - **admin workspace**
     - **resident workspace**
     - **superadmin workspace**
     - locale-prefixed equivalents under `/[locale]/...`
   - UI composition is done through reusable shells, sidebars, nav bars, and feature-specific page components.

7. **Internationalization is a first-class routing concern**
   - Locale is encoded in the URL (`ro`, `ru`, `en`) and persisted via middleware/cookies.
   - The frontend has both localized and non-localized route families, with localized routes taking priority for the product experience.
   - This matters architecturally because route handling, redirects, and navigation are locale-aware rather than purely presentation-layer concerns.

8. **Main domain subsystems**
   - The backend and frontend are organized around the platform’s core workflows:
     - **apartment/building/resident management**
     - **meters and meter readings**
     - **billing, invoices, and payments**
     - **requests, issues, maintenance, announcements**
     - **documents, exports, imports**
     - **team/invitations/permissions**
     - **notifications and communication**
   - There are also substantial **superadmin CRM / customer-success / SaaS management** modules for the platform operator.

9. **Billing and payment flow**
   - Billing is split into read/write concerns, draft generation, invoice publishing, payment intents, proofs, and reconciliation.
   - The backend includes several payment providers and a provider factory, so payment handling is abstracted behind provider interfaces.
   - The frontend mirrors this with dedicated resident and admin payment pages, invoice PDFs/prints, and superadmin billing views.
   - The repository also contains SaaS billing concepts for platform subscriptions, usage, upgrade requests, and SaaS invoices.

10. **Data operations and quality controls**
   - Imports, exports, bulk operations, saved views, and data-quality modules are core platform features.
   - The system includes duplicate detection, merge/fix flows, data retention, archive/export capabilities, and admin-facing bulk operations.
   - This indicates the backend is not just transactional CRUD; it also acts as an operational data-management platform with auditability and correction workflows.

11. **Notifications, email, and real-time updates**
   - The backend has a dedicated email subsystem, notification delivery management, transactional notifications, and websocket events.
   - Email is environment-driven: console in dev, Resend in production.
   - Realtime events appear to support UI refreshes and status updates rather than being the primary application backbone.

12. **Operational and platform services**
   - The backend contains modules for backup/recovery, launch control, system monitoring, legal/help center content, feature flags, and audit logs.
   - These are not product “business” features so much as platform-operating subsystems for running the SaaS and managing customer support/onboarding.
   - The superadmin area in particular is a control plane for organization lifecycle, health, onboarding, billing, and support work.

13. **Legacy/hidden modules are present but not the primary architecture**
   - The repo still contains older modules for rentals, reservations, cleaning, and other legacy flows.
   - The README says these are **legacy-only**, hidden from production navigation, and redirected away where possible.
   - For an architecture diagram, these should be shown as non-primary or deprecated paths, not central business flows.

14. **Mobile companion app**
   - The `ios` folder contains a native Swift app that talks to the backend through its own API client and token/session storage.
   - It is focused on resident-facing experiences, so it should be treated as a secondary client of the same backend rather than a separate system.

15. **Deployment and runtime topology**
   - The repo supports Docker-based local and production setups, with Caddy used as a reverse proxy in the full-stack deployment path.
   - There are backup/restore scripts, deploy scripts, and production environment templates.
   - The documented baseline deployment is a **Docker-hosted backend + database + frontend** setup, with an alternate option of hosting the frontend separately.

Main entry points: [`backend/src/main.ts`](https://github.com/bolboceanuigor/espace/blob/main/backend/src/main.ts) (API entry), [`frontend/app`](https://github.com/bolboceanuigor/espace/tree/main/frontend/app) (Web app), [`backend/prisma/schema.prisma`](https://github.com/bolboceanuigor/espace/blob/main/backend/prisma/schema.prisma) (Prisma DB), [`docker-compose.prod.yml`](https://github.com/bolboceanuigor/espace/blob/main/docker-compose.prod.yml) (Runtime deploy)

## Diagram (Mermaid)

```mermaid
flowchart TD

subgraph group_repo["Espace monorepo"]
end

subgraph group_backend["NestJS backend"]
  node_api_root["API entry<br/>NestJS bootstrap<br/>[main.ts]"]
  node_api_auth["Auth & RBAC<br/>security modules"]
  node_api_tenant["Tenant context<br/>scoping layer"]
  node_api_org["Organization core<br/>multi-tenant domain"]
  node_api_resident["Resident domain<br/>association domain"]
  node_api_billing["Billing stack<br/>finance domain"]
  node_api_payments["Payment providers<br/>provider layer"]
  node_api_ops["Ops controls<br/>platform modules"]
  node_api_quality["Data ops<br/>quality workflows"]
  node_api_notify["Notifications<br/>delivery subsystem"]
  node_api_saas["SaaS control plane<br/>operator domain"]
  node_api_legacy["Legacy flows<br/>deprecated domains"]
end

subgraph group_frontend["Next.js frontend"]
  node_web["Web app<br/>Next.js App Router"]
  node_web_shells["App shells<br/>UI shells"]
  node_web_i18n["Locale routing<br/>i18n middleware<br/>[middleware.ts]"]
  node_web_admin["Admin workspace<br/>admin UI"]
  node_web_resident["Resident workspace<br/>resident UI"]
  node_web_superadmin["Superadmin workspace<br/>operator UI"]
end

subgraph group_mobile["iOS app"]
  node_ios_app["iOS app<br/>Swift companion"]
end

subgraph group_ops["Deployment & data"]
  node_db[("Prisma DB<br/>PostgreSQL schema<br/>[schema.prisma]")]
  node_seeds["Seed data<br/>bootstrap scripts<br/>[seed.ts]"]
  node_deploy["Runtime deploy<br/>Docker/Caddy"]
end

node_web -->|"composes"| node_web_shells
node_web -->|"localized by"| node_web_i18n
node_web -->|"includes"| node_web_admin
node_web -->|"includes"| node_web_resident
node_web -->|"includes"| node_web_superadmin
node_web_admin -->|"calls"| node_api_root
node_web_resident -->|"calls"| node_api_root
node_web_superadmin -->|"calls"| node_api_root
node_api_root -->|"uses"| node_api_auth
node_api_root -->|"scopes by"| node_api_tenant
node_api_root -->|"routes to"| node_api_org
node_api_root -->|"routes to"| node_api_resident
node_api_root -->|"routes to"| node_api_billing
node_api_billing -->|"delegates to"| node_api_payments
node_api_root -->|"routes to"| node_api_ops
node_api_root -->|"routes to"| node_api_quality
node_api_root -->|"routes to"| node_api_notify
node_api_root -->|"routes to"| node_api_saas
node_api_root -.->|"contains"| node_api_legacy
node_api_org -->|"persists in"| node_db
node_api_resident -->|"persists in"| node_db
node_api_billing -->|"persists in"| node_db
node_api_ops -->|"persists in"| node_db
node_api_quality -->|"persists in"| node_db
node_seeds -->|"initializes"| node_db
node_deploy -->|"runs"| node_api_root
node_deploy -->|"serves"| node_web
node_deploy -->|"depends on"| node_db
node_ios_app -->|"calls"| node_api_root

click node_web "https://github.com/bolboceanuigor/espace/tree/main/frontend/app"
click node_web_shells "https://github.com/bolboceanuigor/espace/tree/main/frontend/components/layout"
click node_web_i18n "https://github.com/bolboceanuigor/espace/blob/main/frontend/middleware.ts"
click node_web_admin "https://github.com/bolboceanuigor/espace/tree/main/frontend/app/(app)/admin"
click node_web_resident "https://github.com/bolboceanuigor/espace/tree/main/frontend/app/(app)/resident"
click node_web_superadmin "https://github.com/bolboceanuigor/espace/tree/main/frontend/app/(app)/superadmin"
click node_api_root "https://github.com/bolboceanuigor/espace/blob/main/backend/src/main.ts"
click node_api_auth "https://github.com/bolboceanuigor/espace/tree/main/backend/src/auth"
click node_api_tenant "https://github.com/bolboceanuigor/espace/tree/main/backend/src/tenant"
click node_api_org "https://github.com/bolboceanuigor/espace/tree/main/backend/src/organizations"
click node_api_resident "https://github.com/bolboceanuigor/espace/tree/main/backend/src/residents"
click node_api_billing "https://github.com/bolboceanuigor/espace/tree/main/backend/src/billing-read"
click node_api_payments "https://github.com/bolboceanuigor/espace/tree/main/backend/src/payments/providers"
click node_api_ops "https://github.com/bolboceanuigor/espace/tree/main/backend/src/system-monitoring"
click node_api_quality "https://github.com/bolboceanuigor/espace/tree/main/backend/src/data-quality"
click node_api_notify "https://github.com/bolboceanuigor/espace/tree/main/backend/src/notifications"
click node_api_saas "https://github.com/bolboceanuigor/espace/tree/main/backend/src/saas-billing"
click node_api_legacy "https://github.com/bolboceanuigor/espace/tree/main/backend/src/reservations"
click node_db "https://github.com/bolboceanuigor/espace/blob/main/backend/prisma/schema.prisma"
click node_seeds "https://github.com/bolboceanuigor/espace/blob/main/backend/prisma/seed.ts"
click node_deploy "https://github.com/bolboceanuigor/espace/blob/main/docker-compose.prod.yml"
click node_ios_app "https://github.com/bolboceanuigor/espace/tree/main/ios/Espace"

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_root,node_api_auth,node_api_tenant,node_api_org,node_api_resident,node_api_billing,node_api_payments,node_api_ops,node_api_quality,node_api_notify,node_api_saas,node_api_legacy toneAmber
class node_web,node_web_shells,node_web_i18n,node_web_admin,node_web_resident,node_web_superadmin toneMint
class node_ios_app toneRose
class node_db,node_seeds,node_deploy toneIndigo
```

## Components

### NestJS backend

Modular monolith API and domain logic

- [API entry](https://github.com/bolboceanuigor/espace/blob/main/backend/src/main.ts): NestJS bootstrap · Main HTTP application wiring and cross-cutting setup · `backend/src/main.ts`
- [Auth & RBAC](https://github.com/bolboceanuigor/espace/tree/main/backend/src/auth): security modules · JWT, local/google auth, roles, permissions, and guards · `backend/src/auth`
- [Tenant context](https://github.com/bolboceanuigor/espace/tree/main/backend/src/tenant): scoping layer · Organization scope, support-session context, and tenant guards · `backend/src/tenant`
- [Organization core](https://github.com/bolboceanuigor/espace/tree/main/backend/src/organizations): multi-tenant domain · Associations, onboarding, limits, contracts, and admin scoping · `backend/src/organizations`
- [Resident domain](https://github.com/bolboceanuigor/espace/tree/main/backend/src/residents): association domain · Apartments, residents, access, invitations, requests, chat, and votes · `backend/src/residents`
- [Billing stack](https://github.com/bolboceanuigor/espace/tree/main/backend/src/billing-read): finance domain · Billing drafts, invoices, payments, reconciliation, and providers · `backend/src/billing-read`
- [Payment providers](https://github.com/bolboceanuigor/espace/tree/main/backend/src/payments/providers): provider layer · Payment intent orchestration and provider factory abstractions · `backend/src/payments/providers`
- [Ops controls](https://github.com/bolboceanuigor/espace/tree/main/backend/src/system-monitoring): platform modules · Monitoring, backups, launch control, feature flags, audit, help, legal · `backend/src/system-monitoring`
- [Data ops](https://github.com/bolboceanuigor/espace/tree/main/backend/src/data-quality): quality workflows · Imports, exports, bulk operations, saved views, retention, and duplicates · `backend/src/data-quality`
- [Notifications](https://github.com/bolboceanuigor/espace/tree/main/backend/src/notifications): delivery subsystem · Email, transactional notifications, notification management, and websocket events · `backend/src/notifications`
- [SaaS control plane](https://github.com/bolboceanuigor/espace/tree/main/backend/src/saas-billing): operator domain · Subscriptions, upgrade requests, SaaS invoices, usage, and client success · `backend/src/saas-billing`
- [Legacy flows](https://github.com/bolboceanuigor/espace/tree/main/backend/src/reservations): deprecated domains · Hidden or legacy-only paths retained for compatibility · `backend/src/reservations`

### Next.js frontend

Role-based web application with locale-aware routing

- [Web app](https://github.com/bolboceanuigor/espace/tree/main/frontend/app): Next.js App Router · Public site plus localized role workspaces · `frontend/app`
- [App shells](https://github.com/bolboceanuigor/espace/tree/main/frontend/components/layout): UI shells · Shared layouts, sidebars, nav, and role-aware wrappers · `frontend/components/layout`
- [Locale routing](https://github.com/bolboceanuigor/espace/blob/main/frontend/middleware.ts): i18n middleware · Locale-prefixed routes, cookies, redirects, and translations · `frontend/middleware.ts`
- [Admin workspace](https://github.com/bolboceanuigor/espace/tree/main/frontend/app/(app)/admin): admin UI · Association operations, billing, data quality, and settings · `frontend/app/(app)/admin`
- [Resident workspace](https://github.com/bolboceanuigor/espace/tree/main/frontend/app/(app)/resident): resident UI · Resident dashboard, invoices, requests, payments, and profile · `frontend/app/(app)/resident`
- [Superadmin workspace](https://github.com/bolboceanuigor/espace/tree/main/frontend/app/(app)/superadmin): operator UI · CRM, SaaS management, monitoring, launch, and support control plane · `frontend/app/(app)/superadmin`

### iOS app

Resident companion client

- [iOS app](https://github.com/bolboceanuigor/espace/tree/main/ios/Espace): Swift companion · Resident-facing mobile client using its own API client and session store · `ios/Espace`

### Deployment & data

Prisma, migrations, seeds, and runtime ops

- [Prisma DB](https://github.com/bolboceanuigor/espace/blob/main/backend/prisma/schema.prisma): PostgreSQL schema · Source-of-truth schema, migrations, and direct repository access · `backend/prisma/schema.prisma`
- [Seed data](https://github.com/bolboceanuigor/espace/blob/main/backend/prisma/seed.ts): bootstrap scripts · Demo, production base, and privileged account seed routines · `backend/prisma/seed.ts`
- [Runtime deploy](https://github.com/bolboceanuigor/espace/blob/main/docker-compose.prod.yml): Docker/Caddy · Containerized deployment, reverse proxy, backups, and restore scripts · `docker-compose.prod.yml`

## Connections

- Web app → App shells: composes · Routes are rendered through shared shells and layouts
- Web app → Locale routing: localized by · App routes are locale-aware and middleware-driven
- Web app → Admin workspace: includes · Admin routes are a major product workspace
- Web app → Resident workspace: includes · Resident routes are a major product workspace
- Web app → Superadmin workspace: includes · Superadmin routes form the operator workspace
- Admin workspace → API entry: calls · Admin pages consume backend APIs
- Resident workspace → API entry: calls · Resident pages consume backend APIs
- Superadmin workspace → API entry: calls · Operator pages consume backend APIs
- API entry → Auth & RBAC: uses · HTTP endpoints are protected by auth and RBAC
- API entry → Tenant context: scopes by · Requests are resolved into an organization context
- API entry → Organization core: routes to · Organization lifecycle and tenancy operations
- API entry → Resident domain: routes to · Core association workflows and resident-facing data
- API entry → Billing stack: routes to · Billing, invoices, and payment operations
- Billing stack → Payment providers: delegates to · Online payment handling is abstracted behind providers
- API entry → Ops controls: routes to · Platform control, audit, and support modules
- API entry → Data ops: routes to · Bulk ops, imports/exports, and data correction workflows
- API entry → Notifications: routes to · Email, notifications, and realtime events
- API entry → SaaS control plane: routes to · Subscription and SaaS operator workflows
- API entry → Legacy flows: contains · Legacy modules remain present but are not primary flows
- Organization core → Prisma DB: persists in · Organization-scoped state is stored via Prisma
- Resident domain → Prisma DB: persists in · Association data is stored via Prisma repositories
- Billing stack → Prisma DB: persists in · Billing state and invoice records live in PostgreSQL
- Ops controls → Prisma DB: persists in · Operational modules share the same database
- Data ops → Prisma DB: persists in · Data ops workflows rely on the shared schema
- Seed data → Prisma DB: initializes · Seed scripts populate demo and production baselines
- Runtime deploy → API entry: runs · Backend is deployed in containerized runtime
- Runtime deploy → Web app: serves · Frontend is deployed in the same stack or adjacent runtime
- Runtime deploy → Prisma DB: depends on · Production setup includes a database service
- iOS app → API entry: calls · Mobile client talks to the same backend API
