How thesergioandres/essence-landing-page works
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: httpClient.ts HTTP client · App.tsx ERP shell · auth.routes.v2.js HTTP layer · ProductRepository.js Repositories
Components 28
Client app
React ERP shell and feature modules
- ERP shell · Main authenticated frontend entrypoint and route composition
client/src/App.tsx - Access routes · Protected and role-based navigation boundary
client/src/routes/ProtectedRoute.tsx - Session state · Business context, session, cache, notifications, and error reporting
client/src/context/BusinessContext.tsx - HTTP client · Shared axios/http wrapper used by feature services
client/src/shared/api/httpClient.ts - UI kit · Reusable primitives and cross-feature visual components
client/src/shared/components/ui/index.ts - Auth flows · Login, register, redirects, and account-hold flows
client/src/features/auth/pages/LoginPage.tsx - Inventory · Products, categories, price lists, stock, and entries
client/src/features/inventory/pages/InventoryPage.tsx - Sales · Standard, special, promotion, and register-sale workflows
client/src/features/sales/pages/SalesPage.tsx - Distributors · Distributor dashboards, catalogs, stats, pricing, and sharing
client/src/features/distributors/pages/DistributorsPage.tsx - Business admin · Business settings, team management, onboarding, assistant, and gamification
client/src/features/business/pages/BusinessSettingsPage.tsx - Customers · Customer records, segmentation, and point-related views
client/src/features/customers/pages/CustomersPage.tsx - Credits · Credit management and detail views
client/src/features/credits/pages/CreditsPage.tsx - Analytics · Dashboards, audits, expenses, profit history, and rankings
client/src/features/analytics/pages/AnalyticsPage.tsx - Notifications · Push notification settings and notification center
client/src/features/notifications/pages/NotificationsPage.tsx - Settings · 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 · Server bootstrap and runtime composition
server/server.js - HTTP layer · Controllers and versioned routes for public and protected APIs
server/src/infrastructure/http/routes/auth.routes.v2.js - Middleware · Auth, business scoping, validation, security, logging, and rate limiting
server/middleware/auth.middleware.js - Use cases · Workflow orchestration for login, register, sales, products, stock, and permissions
server/src/application/use-cases/LoginUseCase.js - Domain services · Core rules for inventory, finance, auth, and analytics
server/src/domain/services/InventoryService.js - Repositories · MongoDB repositories for products, sales, customers, branches, credits, promotions, and more
server/src/infrastructure/database/repositories/ProductRepository.js - Mongoose models · Legacy and infrastructure data models backed by MongoDB
server/models/Product.js - Background jobs · Backups, notifications, business assistant, and sync tasks
server/jobs/backup.worker.js
Ops & data
Deployment, migrations, scripts, and backups
- Ops scripts · Backfills, indexes, repair, restore, validation, and debug utilities
server/scripts/optimize_indexes.js - Data dumps · Historical BSON exports used for restore and migration
migracion_erp/essence/prelude.json - Deploy config · Docker, Nginx, Railway, and environment configuration
docker-compose.yml
Testing
Unit, integration, and end-to-end verification
- Frontend tests · Unit tests and end-to-end browser suites
client/e2e/01-authentication.spec.ts - Backend tests · Controller, middleware, model, service, and smoke tests
server/__tests__/controllers/auth.controller.test.js
Connections 29
- 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