# thesergioandres/essence-landing-page architecture

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

- Interactive diagram: https://gitdiagram.com/thesergioandres/essence-landing-page
- Repository: https://github.com/thesergioandres/essence-landing-page

## Overview

1. **Repository shape**
   - This is a full-stack monorepo with a React frontend in `client/` and a Node.js/Express backend in `server/`.
   - The repo also contains deployment assets, migration dumps, operational scripts, and extensive test fixtures, so the architecture includes both runtime services and data/ops tooling.

2. **User-facing application boundary**
   - The frontend is the interactive ERP shell for multiple business roles: admin, distributor, and support/“god” access.
   - It is organized as feature modules under `client/src/features/...`, which keeps screens, API clients, hooks, and types grouped by business capability rather than by technical layer.

3. **Frontend composition**
   - Shared UI primitives live in `client/src/shared/components/ui`, while cross-cutting app state is handled by hooks and context (`useSession`, `BusinessContext`, push notification hooks, error reporting, caching).
   - Routing is protected through `ProtectedRoute` and `RoleRoute`, which enforce authentication and role-based access before users reach feature pages.

4. **Frontend-to-backend data flow**
   - Most feature folders expose a service layer that calls the backend through a shared HTTP client (`client/src/shared/api/httpClient.ts` and `client/src/api/axios.ts`).
   - The frontend is therefore a thin orchestration layer: pages render business workflows, services fetch/submit data, and shared components visualize inventories, sales, analytics, alerts, and forms.

5. **Backend architecture**
   - The backend is a hybrid architecture: legacy controllers/models coexist with a cleaner hexagonal layout under `server/src/application`, `server/src/domain`, and `server/src/infrastructure`.
   - HTTP routes map to controllers, controllers delegate to use cases or services, and repositories encapsulate persistence against MongoDB via Mongoose.

6. **Core backend boundaries**
   - `application/use-cases` contains workflow-level operations such as login, registering users, creating products, updating stock, and registering sales.
   - `domain/services` holds business rules like inventory, finance, authentication, and analytics.
   - `infrastructure/database/repositories` isolates access to each aggregate or concern: products, sales, customers, branches, credits, promotions, notifications, and so on.

7. **Data layer and persistence**
   - MongoDB is the primary system of record, with Mongoose models defined both in the legacy root models and in the newer infrastructure layer.
   - The repository includes many BSON dumps in `migracion_erp/essence/`, indicating historical data migration and backup/restore are part of the system’s operational architecture.

8. **Business context and authorization**
   - A key architectural boundary is the active business context, enforced through middleware and headers such as `x-business-id`.
   - Authentication uses JWT, while permissions are role- and module-based; middleware handles auth, business scoping, rate limiting, validation, and security checks before controller execution.

9. **Main business subsystems**
   - The application is not a generic CRUD app; it centers on ERP workflows for inventory, sales, credits, distributors, branches, customers, warranties/defectives, promotions, notifications, and analytics.
   - There are dedicated features and backend routes for each of these domains, which are mirrored in the frontend feature structure and backend repository/controller layout.

10. **Inventory and stock hierarchy**
   - Inventory is modeled across three operational levels: central warehouse, branches, and distributors.
   - Stock changes propagate through dedicated controllers/repositories and are reinforced by scripts for backfills, migrations, index optimization, and integrity checks, reflecting the importance of inventory consistency.

11. **Sales, credits, and warranty flows**
   - Sales are split into standard, special, and promotion flows, with separate frontend pages and backend controllers/use cases.
   - Credits, payments, and warranty/defective-product handling are first-class flows with their own pages, services, and audit trails; profit history and analysis logs show that financial traceability is a core concern.

12. **Analytics and operational intelligence**
   - Analytics is a major subsystem, including dashboards, KPI views, rankings, audit logs, profit history, and advanced charts.
   - On the backend, analytics has dedicated repositories and controllers, suggesting read-optimized aggregation paths separate from transactional CRUD flows.

13. **Notifications and background work**
   - The system includes push notifications, subscription management, and cron/worker jobs for async tasks such as backups, business assistant processing, debt notifications, and subscription maintenance.
   - Redis/BullMQ is optional but present as an infrastructure choice for queued or scheduled work, even if not always active in every environment.

14. **Deployment and runtime topology**
   - Production is designed around Railway with separate frontend, backend, and MongoDB services, typically co-located in the same region for lower latency.
   - The frontend is built and served separately from the backend, while the backend connects to MongoDB over the platform’s private network; Nginx and Docker assets support containerized deployment.

15. **Testing and maintenance surface**
   - The repo has substantial test coverage at both unit/integration and end-to-end levels: Jest-style backend tests, Vitest frontend tests, and Playwright E2E suites.
   - There are many scripts for validation, indexes, migration, backfills, and data repair, so an architecture diagram should include an explicit “ops/maintenance” lane alongside runtime components.

Main entry points: [`client/src/shared/api/httpClient.ts`](https://github.com/thesergioandres/essence-landing-page/blob/main/client/src/shared/api/httpClient.ts) (HTTP client), [`client/src/App.tsx`](https://github.com/thesergioandres/essence-landing-page/blob/main/client/src/App.tsx) (ERP shell), [`server/src/infrastructure/http/routes/auth.routes.v2.js`](https://github.com/thesergioandres/essence-landing-page/blob/main/server/src/infrastructure/http/routes/auth.routes.v2.js) (HTTP layer), [`server/src/infrastructure/database/repositories/ProductRepository.js`](https://github.com/thesergioandres/essence-landing-page/blob/main/server/src/infrastructure/database/repositories/ProductRepository.js) (Repositories)

## Diagram (Mermaid)

```mermaid
flowchart TD

subgraph group_frontend["Client app"]
  node_client_app["ERP shell<br/>React app<br/>[App.tsx]"]
  node_client_routing["Access routes<br/>route guards<br/>[ProtectedRoute.tsx]"]
  node_client_session["Session state<br/>hooks/context"]
  node_client_http["HTTP client<br/>api layer<br/>[httpClient.ts]"]
  node_client_shared_ui["UI kit<br/>shared components<br/>[index.ts]"]
  node_client_auth["Auth flows<br/>feature module<br/>[LoginPage.tsx]"]
  node_client_inventory["Inventory<br/>feature module<br/>[InventoryPage.tsx]"]
  node_client_sales["Sales<br/>feature module<br/>[SalesPage.tsx]"]
  node_client_distributors["Distributors<br/>feature module"]
  node_client_business["Business admin<br/>feature module"]
  node_client_customers["Customers<br/>feature module<br/>[CustomersPage.tsx]"]
  node_client_credits["Credits<br/>feature module<br/>[CreditsPage.tsx]"]
  node_client_analytics["Analytics<br/>feature module<br/>[AnalyticsPage.tsx]"]
  node_client_notifications["Notifications<br/>feature module"]
  node_client_settings["Settings<br/>feature module"]
end

subgraph group_backend["Server app"]
  node_server_entry["API entry<br/>Express server<br/>[server.js]"]
  node_server_http["HTTP layer<br/>controllers/routes<br/>[auth.routes.v2.js]"]
  node_server_middleware["Middleware<br/>cross-cutting<br/>[auth.middleware.js]"]
  node_server_app["Use cases<br/>application layer<br/>[LoginUseCase.js]"]
  node_server_domain["Domain services<br/>business rules"]
  node_server_repos["Repositories<br/>persistence adapters"]
  node_server_models["Mongoose models<br/>schema layer<br/>[Product.js]"]
  node_server_jobs["Background jobs<br/>async work<br/>[backup.worker.js]"]
end

subgraph group_ops["Ops & data"]
  node_server_scripts["Ops scripts<br/>maintenance"]
  node_mongo_dumps[("Data dumps<br/>migrations/backups<br/>[prelude.json]")]
  node_deploy_assets["Deploy config<br/>deployment<br/>[docker-compose.yml]"]
end

subgraph group_testing["Testing"]
  node_client_tests["Frontend tests<br/>Vitest/Playwright"]
  node_server_tests["Backend tests<br/>Jest tests"]
end

node_client_app -->|"uses"| node_client_routing
node_client_app -->|"depends on"| node_client_session
node_client_app -->|"renders with"| node_client_shared_ui
node_client_auth -->|"calls"| node_client_http
node_client_inventory -->|"calls"| node_client_http
node_client_sales -->|"calls"| node_client_http
node_client_distributors -->|"calls"| node_client_http
node_client_business -->|"calls"| node_client_http
node_client_customers -->|"calls"| node_client_http
node_client_credits -->|"calls"| node_client_http
node_client_analytics -->|"calls"| node_client_http
node_client_notifications -->|"calls"| node_client_http
node_client_settings -->|"calls"| node_client_http
node_client_http -->|"requests"| node_server_http
node_server_entry -->|"installs"| node_server_middleware
node_server_entry -->|"mounts"| node_server_http
node_server_http -->|"delegates to"| node_server_app
node_server_http -->|"delegates to"| node_server_domain
node_server_app -->|"uses"| node_server_repos
node_server_domain -->|"reads/writes"| node_server_repos
node_server_repos -->|"maps to"| node_server_models
node_server_jobs -->|"processes"| node_server_repos
node_server_scripts -->|"maintains"| node_server_models
node_server_scripts -->|"restores"| node_mongo_dumps
node_deploy_assets -->|"deploys"| node_server_entry
node_deploy_assets -->|"deploys"| node_client_app
node_client_tests -.->|"verifies"| node_client_app
node_server_tests -.->|"verifies"| node_server_http
node_server_tests -.->|"verifies"| node_server_middleware

click node_client_app "https://github.com/thesergioandres/essence-landing-page/blob/main/client/src/App.tsx"
click node_client_routing "https://github.com/thesergioandres/essence-landing-page/blob/main/client/src/routes/ProtectedRoute.tsx"
click node_client_session "https://github.com/thesergioandres/essence-landing-page/blob/main/client/src/context/BusinessContext.tsx"
click node_client_http "https://github.com/thesergioandres/essence-landing-page/blob/main/client/src/shared/api/httpClient.ts"
click node_client_shared_ui "https://github.com/thesergioandres/essence-landing-page/blob/main/client/src/shared/components/ui/index.ts"
click node_client_auth "https://github.com/thesergioandres/essence-landing-page/blob/main/client/src/features/auth/pages/LoginPage.tsx"
click node_client_inventory "https://github.com/thesergioandres/essence-landing-page/blob/main/client/src/features/inventory/pages/InventoryPage.tsx"
click node_client_sales "https://github.com/thesergioandres/essence-landing-page/blob/main/client/src/features/sales/pages/SalesPage.tsx"
click node_client_distributors "https://github.com/thesergioandres/essence-landing-page/blob/main/client/src/features/distributors/pages/DistributorsPage.tsx"
click node_client_business "https://github.com/thesergioandres/essence-landing-page/blob/main/client/src/features/business/pages/BusinessSettingsPage.tsx"
click node_client_customers "https://github.com/thesergioandres/essence-landing-page/blob/main/client/src/features/customers/pages/CustomersPage.tsx"
click node_client_credits "https://github.com/thesergioandres/essence-landing-page/blob/main/client/src/features/credits/pages/CreditsPage.tsx"
click node_client_analytics "https://github.com/thesergioandres/essence-landing-page/blob/main/client/src/features/analytics/pages/AnalyticsPage.tsx"
click node_client_notifications "https://github.com/thesergioandres/essence-landing-page/blob/main/client/src/features/notifications/pages/NotificationsPage.tsx"
click node_client_settings "https://github.com/thesergioandres/essence-landing-page/blob/main/client/src/features/settings/pages/UserSettingsPage.tsx"
click node_server_entry "https://github.com/thesergioandres/essence-landing-page/blob/main/server/server.js"
click node_server_http "https://github.com/thesergioandres/essence-landing-page/blob/main/server/src/infrastructure/http/routes/auth.routes.v2.js"
click node_server_middleware "https://github.com/thesergioandres/essence-landing-page/blob/main/server/middleware/auth.middleware.js"
click node_server_app "https://github.com/thesergioandres/essence-landing-page/blob/main/server/src/application/use-cases/LoginUseCase.js"
click node_server_domain "https://github.com/thesergioandres/essence-landing-page/blob/main/server/src/domain/services/InventoryService.js"
click node_server_repos "https://github.com/thesergioandres/essence-landing-page/blob/main/server/src/infrastructure/database/repositories/ProductRepository.js"
click node_server_models "https://github.com/thesergioandres/essence-landing-page/blob/main/server/models/Product.js"
click node_server_jobs "https://github.com/thesergioandres/essence-landing-page/blob/main/server/jobs/backup.worker.js"
click node_server_scripts "https://github.com/thesergioandres/essence-landing-page/blob/main/server/scripts/optimize_indexes.js"
click node_mongo_dumps "https://github.com/thesergioandres/essence-landing-page/blob/main/migracion_erp/essence/prelude.json"
click node_deploy_assets "https://github.com/thesergioandres/essence-landing-page/blob/main/docker-compose.yml"
click node_client_tests "https://github.com/thesergioandres/essence-landing-page/blob/main/client/e2e/01-authentication.spec.ts"
click node_server_tests "https://github.com/thesergioandres/essence-landing-page/blob/main/server/__tests__/controllers/auth.controller.test.js"

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_client_app,node_client_routing,node_client_session,node_client_http,node_client_shared_ui,node_client_auth,node_client_inventory,node_client_sales,node_client_distributors,node_client_business,node_client_customers,node_client_credits,node_client_analytics,node_client_notifications,node_client_settings toneBlue
class node_server_entry,node_server_http,node_server_middleware,node_server_app,node_server_domain,node_server_repos,node_server_models,node_server_jobs toneAmber
class node_server_scripts,node_mongo_dumps,node_deploy_assets toneMint
class node_client_tests,node_server_tests toneRose
```

## Components

### Client app

React ERP shell and feature modules

- [ERP shell](https://github.com/thesergioandres/essence-landing-page/blob/main/client/src/App.tsx): React app · Main authenticated frontend entrypoint and route composition · `client/src/App.tsx`
- [Access routes](https://github.com/thesergioandres/essence-landing-page/blob/main/client/src/routes/ProtectedRoute.tsx): route guards · Protected and role-based navigation boundary · `client/src/routes/ProtectedRoute.tsx`
- [Session state](https://github.com/thesergioandres/essence-landing-page/blob/main/client/src/context/BusinessContext.tsx): hooks/context · Business context, session, cache, notifications, and error reporting · `client/src/context/BusinessContext.tsx`
- [HTTP client](https://github.com/thesergioandres/essence-landing-page/blob/main/client/src/shared/api/httpClient.ts): api layer · Shared axios/http wrapper used by feature services · `client/src/shared/api/httpClient.ts`
- [UI kit](https://github.com/thesergioandres/essence-landing-page/blob/main/client/src/shared/components/ui/index.ts): shared components · Reusable primitives and cross-feature visual components · `client/src/shared/components/ui/index.ts`
- [Auth flows](https://github.com/thesergioandres/essence-landing-page/blob/main/client/src/features/auth/pages/LoginPage.tsx): feature module · Login, register, redirects, and account-hold flows · `client/src/features/auth/pages/LoginPage.tsx`
- [Inventory](https://github.com/thesergioandres/essence-landing-page/blob/main/client/src/features/inventory/pages/InventoryPage.tsx): feature module · Products, categories, price lists, stock, and entries · `client/src/features/inventory/pages/InventoryPage.tsx`
- [Sales](https://github.com/thesergioandres/essence-landing-page/blob/main/client/src/features/sales/pages/SalesPage.tsx): feature module · Standard, special, promotion, and register-sale workflows · `client/src/features/sales/pages/SalesPage.tsx`
- [Distributors](https://github.com/thesergioandres/essence-landing-page/blob/main/client/src/features/distributors/pages/DistributorsPage.tsx): feature module · Distributor dashboards, catalogs, stats, pricing, and sharing · `client/src/features/distributors/pages/DistributorsPage.tsx`
- [Business admin](https://github.com/thesergioandres/essence-landing-page/blob/main/client/src/features/business/pages/BusinessSettingsPage.tsx): feature module · Business settings, team management, onboarding, assistant, and gamification · `client/src/features/business/pages/BusinessSettingsPage.tsx`
- [Customers](https://github.com/thesergioandres/essence-landing-page/blob/main/client/src/features/customers/pages/CustomersPage.tsx): feature module · Customer records, segmentation, and point-related views · `client/src/features/customers/pages/CustomersPage.tsx`
- [Credits](https://github.com/thesergioandres/essence-landing-page/blob/main/client/src/features/credits/pages/CreditsPage.tsx): feature module · Credit management and detail views · `client/src/features/credits/pages/CreditsPage.tsx`
- [Analytics](https://github.com/thesergioandres/essence-landing-page/blob/main/client/src/features/analytics/pages/AnalyticsPage.tsx): feature module · Dashboards, audits, expenses, profit history, and rankings · `client/src/features/analytics/pages/AnalyticsPage.tsx`
- [Notifications](https://github.com/thesergioandres/essence-landing-page/blob/main/client/src/features/notifications/pages/NotificationsPage.tsx): feature module · Push notification settings and notification center · `client/src/features/notifications/pages/NotificationsPage.tsx`
- [Settings](https://github.com/thesergioandres/essence-landing-page/blob/main/client/src/features/settings/pages/UserSettingsPage.tsx): feature module · Payment methods, delivery methods, promotions, providers, and user settings · `client/src/features/settings/pages/UserSettingsPage.tsx`

### Server app

Express API with hexagonal core and legacy surface

- [API entry](https://github.com/thesergioandres/essence-landing-page/blob/main/server/server.js): Express server · Server bootstrap and runtime composition · `server/server.js`
- [HTTP layer](https://github.com/thesergioandres/essence-landing-page/blob/main/server/src/infrastructure/http/routes/auth.routes.v2.js): controllers/routes · Controllers and versioned routes for public and protected APIs · `server/src/infrastructure/http/routes/auth.routes.v2.js`
- [Middleware](https://github.com/thesergioandres/essence-landing-page/blob/main/server/middleware/auth.middleware.js): cross-cutting · Auth, business scoping, validation, security, logging, and rate limiting · `server/middleware/auth.middleware.js`
- [Use cases](https://github.com/thesergioandres/essence-landing-page/blob/main/server/src/application/use-cases/LoginUseCase.js): application layer · Workflow orchestration for login, register, sales, products, stock, and permissions · `server/src/application/use-cases/LoginUseCase.js`
- [Domain services](https://github.com/thesergioandres/essence-landing-page/blob/main/server/src/domain/services/InventoryService.js): business rules · Core rules for inventory, finance, auth, and analytics · `server/src/domain/services/InventoryService.js`
- [Repositories](https://github.com/thesergioandres/essence-landing-page/blob/main/server/src/infrastructure/database/repositories/ProductRepository.js): persistence adapters · MongoDB repositories for products, sales, customers, branches, credits, promotions, and more · `server/src/infrastructure/database/repositories/ProductRepository.js`
- [Mongoose models](https://github.com/thesergioandres/essence-landing-page/blob/main/server/models/Product.js): schema layer · Legacy and infrastructure data models backed by MongoDB · `server/models/Product.js`
- [Background jobs](https://github.com/thesergioandres/essence-landing-page/blob/main/server/jobs/backup.worker.js): async work · Backups, notifications, business assistant, and sync tasks · `server/jobs/backup.worker.js`

### Ops & data

Deployment, migrations, scripts, and backups

- [Ops scripts](https://github.com/thesergioandres/essence-landing-page/blob/main/server/scripts/optimize_indexes.js): maintenance · Backfills, indexes, repair, restore, validation, and debug utilities · `server/scripts/optimize_indexes.js`
- [Data dumps](https://github.com/thesergioandres/essence-landing-page/blob/main/migracion_erp/essence/prelude.json): migrations/backups · Historical BSON exports used for restore and migration · `migracion_erp/essence/prelude.json`
- [Deploy config](https://github.com/thesergioandres/essence-landing-page/blob/main/docker-compose.yml): deployment · Docker, Nginx, Railway, and environment configuration · `docker-compose.yml`

### Testing

Unit, integration, and end-to-end verification

- [Frontend tests](https://github.com/thesergioandres/essence-landing-page/blob/main/client/e2e/01-authentication.spec.ts): Vitest/Playwright · Unit tests and end-to-end browser suites · `client/e2e/01-authentication.spec.ts`
- [Backend tests](https://github.com/thesergioandres/essence-landing-page/blob/main/server/__tests__/controllers/auth.controller.test.js): Jest tests · Controller, middleware, model, service, and smoke tests · `server/__tests__/controllers/auth.controller.test.js`

## Connections

- ERP shell → Access routes: uses · App composes guarded routes
- ERP shell → Session state: depends on · App relies on session and business context
- ERP shell → UI kit: renders with · App and feature pages use shared UI primitives
- Auth flows → HTTP client: calls · Auth feature talks to backend through shared HTTP
- Inventory → HTTP client: calls · Inventory services fetch and submit API data
- Sales → HTTP client: calls · Sales workflows submit orders and promotions
- Distributors → HTTP client: calls · Distributor screens consume API services
- Business admin → HTTP client: calls · Business administration features use shared API
- Customers → HTTP client: calls · Customer features load records and segments
- Credits → HTTP client: calls · Credit views read and update credit flows
- Analytics → HTTP client: calls · Analytics screens request dashboards and logs
- Notifications → HTTP client: calls · Notification settings and inbox actions use API
- Settings → HTTP client: calls · Settings screens manage configurable business data
- HTTP client → HTTP layer: requests · Frontend services reach versioned backend endpoints
- API entry → Middleware: installs · Server boots with security, logging, and business middleware
- API entry → HTTP layer: mounts · Entry point mounts API routes and controllers
- HTTP layer → Use cases: delegates to · Controllers invoke use cases for transactional workflows
- HTTP layer → Domain services: delegates to · Controllers rely on domain services for rules
- Use cases → Repositories: uses · Use cases persist and query through repositories
- Domain services → Repositories: reads/writes · Domain logic uses persistence adapters when needed
- Repositories → Mongoose models: maps to · Repositories wrap MongoDB models and schemas
- Background jobs → Repositories: processes · Async work reads and updates stored operational data
- Ops scripts → Mongoose models: maintains · Scripts backfill, repair, and optimize persisted data
- Ops scripts → Data dumps: restores · Maintenance scripts and backups interact with BSON dumps
- Deploy config → API entry: deploys · Deployment assets package and run the backend
- Deploy config → ERP shell: deploys · Deployment assets build and serve the frontend
- Frontend tests → ERP shell: verifies · Frontend tests validate auth, inventory, sales, and dashboard flows
- Backend tests → HTTP layer: verifies · Backend tests validate controllers, middleware, and services
- Backend tests → Middleware: verifies · Tests cover auth, permissions, and business scoping
