# ema1322/lafileto-monorepo architecture

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

- Interactive diagram: https://gitdiagram.com/ema1322/lafileto-monorepo
- Repository: https://github.com/ema1322/lafileto-monorepo

## Overview

1. **Repository shape**
   - This is a pnpm monorepo with three deployable apps: `apps/client` (public storefront SPA), `apps/admin` (back-office SPA), and `apps/backend` (Node/Express API).
   - There are also small shared packages under `packages/`, plus repo-level docs, CI, and utility scripts.

2. **Public client SPA**
   - `apps/client` is the customer-facing app for catalog browsing, cart, confirmation, and WhatsApp-driven ordering.
   - It has a React-based shell (`src/react`) for the newer flow, plus some legacy/static UI assets and CSS under `src/components` and `src/styles`.
   - The client consumes a public API through `src/api/public.js` and `src/react/services/publicApi.js`.

3. **Admin SPA**
   - `apps/admin` is the management console for products, categories, users, roles/permissions, settings, and dashboard views.
   - It is organized by feature folders in `src/components`, with a lot of UI-state/render split files for tables, modals, bindings, and state.
   - It uses JWT-based authentication and RBAC gates to decide which module routes are visible or accessible.

4. **Backend API boundary**
   - `apps/backend` is the system of record: an Express API under `/api/v1` that exposes auth, users, roles, modules, settings, dashboard, products, categories, offers, and public catalog endpoints.
   - The code is layered: routes → controllers → services → repositories, with validation and middleware at the edges.
   - This boundary is where auth, authorization, persistence, and domain rules are enforced.

5. **Persistence and data model**
   - The backend uses Prisma with MySQL as the database layer.
   - Migrations show the main persisted entities: users, roles, modules, role-permissions, settings, categories, products, and offers.
   - Seeds and migration history indicate the schema has evolved significantly, especially around admin RBAC, product modeling, and settings.

6. **Authentication and authorization flow**
   - Login is handled by `authController`/`authService` and JWT helpers; the client/admin stores the token and rehydrates session state via `GET /auth/me`.
   - Authorization is module-based RBAC: permissions are fetched from the backend and enforced both server-side (`rbacGuard`, `requireAdminRole`) and client-side via route guards.
   - The admin flow depends on “hydrated” session state before navigation is allowed, which is a major architectural boundary.

7. **Public vs admin data paths**
   - The client can operate against either JSON fixtures or the API, controlled by `VITE_DATA_SOURCE=json|api`; this supports gradual migration from static data to backend-backed data.
   - The admin app also ships with JSON data files in `public/data`, which suggests local/mock-driven screens and test fixtures alongside live API usage.
   - This dual-source approach is an important diagram element because it means not all UI reads go directly to MySQL-backed endpoints.

8. **Domain services and repositories**
   - The backend business logic is concentrated in services such as `productService`, `userService`, `categoryService`, `rbacService`, `settingsService`, and `dashboardService`.
   - Repositories isolate Prisma access for each aggregate, keeping controllers thin and making the database the only persistence boundary.
   - Domain-specific helpers exist for offers, formatting, pagination, and site-config validation/sanitization.

9. **Cross-cutting middleware and API safeguards**
   - The backend uses dedicated middleware for request IDs, timeouts, validation, login rate limiting, CORS, error handling, and RBAC enforcement.
   - This means request processing is not just route handling; the API has a clear guardrail layer before business logic runs.
   - For diagrams, these are the main horizontal concerns spanning all routes.

10. **Shared UI and utility layer**
   - The admin and client each have their own UI stacks, but there are reusable packages for design and utilities under `packages/`.
   - `packages/shared-utils` contains common formatting/text helpers; `packages/shared-design` supports UI consistency.
   - These are smaller than the app boundaries, but they matter as shared dependencies that reduce duplication.

11. **External integrations**
   - The public client’s ordering flow includes WhatsApp as an external handoff channel.
   - The admin/backend architecture also relies on environment-configured CORS allowlists and JWT secrets, so runtime configuration is part of the deployment boundary.
   - There is no evidence of a heavy third-party SaaS core; the main external dependency is MySQL, plus the browser/WhatsApp interaction on the public side.

12. **Testing and quality boundaries**
   - Each app has its own tests: backend integration/smoke tests, client component/integration tests, and admin feature tests.
   - CI runs lint/test workflows from the repo root, and Vite-based builds produce the SPA bundles.
   - From an architecture perspective, tests mirror the same boundaries as runtime: public client, admin console, and backend API.

Main entry points: [`apps/backend/src/routes/index.js`](https://github.com/ema1322/lafileto-monorepo/blob/main/apps/backend/src/routes/index.js) (Routes), [`apps/client/src/react/pages`](https://github.com/ema1322/lafileto-monorepo/tree/main/apps/client/src/react/pages) (Client pages), [`apps/admin/src/components/dashboard/dashboard.js`](https://github.com/ema1322/lafileto-monorepo/blob/main/apps/admin/src/components/dashboard/dashboard.js) (Dashboard), [`apps/admin/src/main.js`](https://github.com/ema1322/lafileto-monorepo/blob/main/apps/admin/src/main.js) (Admin shell)

## Diagram (Mermaid)

```mermaid
flowchart TD

subgraph group_group_apps["Apps"]
end

subgraph group_group_shared["Shared"]
  node_node_shared_utils["Shared utils<br/>[index.js]"]
  node_node_shared_design["Shared design<br/>[package.json]"]
end

subgraph group_group_backend["Backend"]
  node_node_backend_app["API app<br/>express api<br/>[app.js]"]
  node_node_backend_routes["Routes<br/>routing layer<br/>[index.js]"]
  node_node_backend_edge["Edge guards<br/>middleware"]
  node_node_backend_auth["Auth flow<br/>service boundary<br/>[authController.js]"]
  node_node_backend_domain["Domain services<br/>business logic"]
  node_node_backend_repos["Repositories<br/>persistence layer"]
  node_node_backend_prisma[("Prisma/MySQL<br/>database<br/>[schema.prisma]")]
end

subgraph group_group_client["Client"]
  node_node_client_appshell["Client shell<br/>spa shell<br/>[main.js]"]
  node_node_client_router["Client router<br/>navigation<br/>[AppRouter.jsx]"]
  node_node_client_pages["Client pages"]
  node_node_client_api["Public API<br/>data access<br/>[public.js]"]
  node_node_client_state["Client state<br/>ui state<br/>[shellState.js]"]
  node_node_client_whatsapp(("WhatsApp handoff<br/>external channel"))
end

subgraph group_group_admin["Admin"]
  node_node_admin_shell["Admin shell<br/>spa shell<br/>[main.js]"]
  node_node_admin_auth["Admin auth<br/>session/rbac<br/>[auth.js]"]
  node_node_admin_users["Users module<br/>feature module<br/>[users.js]"]
  node_node_admin_products["Products module<br/>feature module<br/>[products.js]"]
  node_node_admin_categories["Categories module<br/>feature module<br/>[categories.js]"]
  node_node_admin_settings["Settings module<br/>feature module<br/>[settings.js]"]
  node_node_admin_dashboard["Dashboard<br/>feature module<br/>[dashboard.js]"]
  node_node_admin_mockdata["Mock data<br/>fixtures<br/>[products.json]"]
end

node_node_client_appshell -->|"bootstraps"| node_node_client_router
node_node_client_router -->|"renders"| node_node_client_pages
node_node_client_pages -->|"loads data"| node_node_client_api
node_node_client_pages -->|"uses"| node_node_client_state
node_node_client_pages -->|"handoff"| node_node_client_whatsapp
node_node_client_api -->|"calls public API"| node_node_backend_routes
node_node_admin_shell -->|"guards routes"| node_node_admin_auth
node_node_admin_shell -->|"starts"| node_node_admin_dashboard
node_node_admin_dashboard -->|"links to"| node_node_admin_users
node_node_admin_dashboard -->|"links to"| node_node_admin_products
node_node_admin_dashboard -->|"links to"| node_node_admin_categories
node_node_admin_dashboard -->|"links to"| node_node_admin_settings
node_node_admin_users -.->|"can use"| node_node_admin_mockdata
node_node_admin_products -.->|"can use"| node_node_admin_mockdata
node_node_admin_users -->|"calls API"| node_node_backend_routes
node_node_admin_products -->|"calls API"| node_node_backend_routes
node_node_admin_categories -->|"calls API"| node_node_backend_routes
node_node_admin_settings -->|"calls API"| node_node_backend_routes
node_node_backend_app -->|"applies"| node_node_backend_edge
node_node_backend_app -->|"mounts"| node_node_backend_routes
node_node_backend_routes -->|"exposes"| node_node_backend_auth
node_node_backend_routes -->|"delegates"| node_node_backend_domain
node_node_backend_domain -->|"persists via"| node_node_backend_repos
node_node_backend_repos -->|"uses"| node_node_backend_prisma
node_node_backend_auth -->|"protected by"| node_node_backend_edge
node_node_backend_domain -.->|"formats with"| node_node_shared_utils
node_node_client_pages -.->|"styled by"| node_node_shared_design
node_node_admin_shell -.->|"styled by"| node_node_shared_design

click node_node_client_appshell "https://github.com/ema1322/lafileto-monorepo/blob/main/apps/client/src/main.js"
click node_node_client_router "https://github.com/ema1322/lafileto-monorepo/blob/main/apps/client/src/react/router/AppRouter.jsx"
click node_node_client_pages "https://github.com/ema1322/lafileto-monorepo/tree/main/apps/client/src/react/pages"
click node_node_client_api "https://github.com/ema1322/lafileto-monorepo/blob/main/apps/client/src/api/public.js"
click node_node_client_state "https://github.com/ema1322/lafileto-monorepo/blob/main/apps/client/src/react/state/shellState.js"
click node_node_client_whatsapp "https://github.com/ema1322/lafileto-monorepo/blob/main/apps/client/src/utils/commercialContext.js"
click node_node_admin_shell "https://github.com/ema1322/lafileto-monorepo/blob/main/apps/admin/src/main.js"
click node_node_admin_auth "https://github.com/ema1322/lafileto-monorepo/blob/main/apps/admin/src/utils/auth.js"
click node_node_admin_users "https://github.com/ema1322/lafileto-monorepo/blob/main/apps/admin/src/components/users/users.js"
click node_node_admin_products "https://github.com/ema1322/lafileto-monorepo/blob/main/apps/admin/src/components/products/products.js"
click node_node_admin_categories "https://github.com/ema1322/lafileto-monorepo/blob/main/apps/admin/src/components/categories/categories.js"
click node_node_admin_settings "https://github.com/ema1322/lafileto-monorepo/blob/main/apps/admin/src/components/settings/settings.js"
click node_node_admin_dashboard "https://github.com/ema1322/lafileto-monorepo/blob/main/apps/admin/src/components/dashboard/dashboard.js"
click node_node_admin_mockdata "https://github.com/ema1322/lafileto-monorepo/blob/main/apps/admin/public/data/products.json"
click node_node_backend_app "https://github.com/ema1322/lafileto-monorepo/blob/main/apps/backend/src/app.js"
click node_node_backend_routes "https://github.com/ema1322/lafileto-monorepo/blob/main/apps/backend/src/routes/index.js"
click node_node_backend_edge "https://github.com/ema1322/lafileto-monorepo/tree/main/apps/backend/src/middlewares"
click node_node_backend_auth "https://github.com/ema1322/lafileto-monorepo/blob/main/apps/backend/src/controllers/authController.js"
click node_node_backend_domain "https://github.com/ema1322/lafileto-monorepo/tree/main/apps/backend/src/services"
click node_node_backend_repos "https://github.com/ema1322/lafileto-monorepo/tree/main/apps/backend/src/repositories"
click node_node_backend_prisma "https://github.com/ema1322/lafileto-monorepo/blob/main/apps/backend/prisma/schema.prisma"
click node_node_shared_utils "https://github.com/ema1322/lafileto-monorepo/blob/main/packages/shared-utils/src/index.js"
click node_node_shared_design "https://github.com/ema1322/lafileto-monorepo/blob/main/packages/shared-design/package.json"

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_node_shared_utils,node_node_shared_design toneAmber
class node_node_backend_app,node_node_backend_routes,node_node_backend_edge,node_node_backend_auth,node_node_backend_domain,node_node_backend_repos,node_node_backend_prisma toneMint
class node_node_client_appshell,node_node_client_router,node_node_client_pages,node_node_client_api,node_node_client_state,node_node_client_whatsapp toneRose
class node_node_admin_shell,node_node_admin_auth,node_node_admin_users,node_node_admin_products,node_node_admin_categories,node_node_admin_settings,node_node_admin_dashboard,node_node_admin_mockdata toneIndigo
```

## Components

### Shared

Reusable packages consumed by the apps.

- [Shared utils](https://github.com/ema1322/lafileto-monorepo/blob/main/packages/shared-utils/src/index.js): package · Common formatting and text helpers used across apps. · `packages/shared-utils/src/index.js`
- [Shared design](https://github.com/ema1322/lafileto-monorepo/blob/main/packages/shared-design/package.json): package · Reusable UI design package for visual consistency. · `packages/shared-design/package.json`

### Backend

Express API boundary and its internal layers.

- [API app](https://github.com/ema1322/lafileto-monorepo/blob/main/apps/backend/src/app.js): express api · Express server composition, route mounting, and top-level middleware. · `apps/backend/src/app.js`
- [Routes](https://github.com/ema1322/lafileto-monorepo/blob/main/apps/backend/src/routes/index.js): routing layer · Versioned API surface for auth, users, roles, modules, settings, dashboard, products, categories, offers, and public endpoints. · `apps/backend/src/routes/index.js`
- [Edge guards](https://github.com/ema1322/lafileto-monorepo/tree/main/apps/backend/src/middlewares): middleware · Request IDs, timeouts, validation, login rate limiting, CORS, auth, RBAC, and error handling. · `apps/backend/src/middlewares`
- [Auth flow](https://github.com/ema1322/lafileto-monorepo/blob/main/apps/backend/src/controllers/authController.js): service boundary · JWT login, session hydration, role checks, and admin access enforcement. · `apps/backend/src/controllers/authController.js`
- [Domain services](https://github.com/ema1322/lafileto-monorepo/tree/main/apps/backend/src/services): business logic · Product, category, user, RBAC, settings, dashboard, public catalog, and offers logic. · `apps/backend/src/services`
- [Repositories](https://github.com/ema1322/lafileto-monorepo/tree/main/apps/backend/src/repositories): persistence layer · Prisma-backed data access for users, roles, modules, settings, products, categories, offers, and permissions. · `apps/backend/src/repositories`
- [Prisma/MySQL](https://github.com/ema1322/lafileto-monorepo/blob/main/apps/backend/prisma/schema.prisma): database · Schema, migrations, seeds, and MySQL persistence boundary. · `apps/backend/prisma/schema.prisma`

### Client

Public storefront SPA and its React migration path.

- [Client shell](https://github.com/ema1322/lafileto-monorepo/blob/main/apps/client/src/main.js): spa shell · Public storefront entry, legacy UI, and React shell composition. · `apps/client/src/main.js`
- [Client router](https://github.com/ema1322/lafileto-monorepo/blob/main/apps/client/src/react/router/AppRouter.jsx): navigation · Routes between storefront home, products, cart, and confirmation flows. · `apps/client/src/react/router/AppRouter.jsx`
- [Client pages](https://github.com/ema1322/lafileto-monorepo/tree/main/apps/client/src/react/pages): pages · Home, products, cart, confirm, and fallback views for the storefront. · `apps/client/src/react/pages`
- [Public API](https://github.com/ema1322/lafileto-monorepo/blob/main/apps/client/src/api/public.js): data access · Client-side adapter for public catalog and ordering data. · `apps/client/src/api/public.js`
- [Client state](https://github.com/ema1322/lafileto-monorepo/blob/main/apps/client/src/react/state/shellState.js): ui state · Shell state, async resource handling, and cart/commercial helpers. · `apps/client/src/react/state/shellState.js`
- [WhatsApp handoff](https://github.com/ema1322/lafileto-monorepo/blob/main/apps/client/src/utils/commercialContext.js): external channel · Ordering handoff path used by the public storefront. · `apps/client/src/utils/commercialContext.js`

### Admin

Back-office SPA and module-based management screens.

- [Admin shell](https://github.com/ema1322/lafileto-monorepo/blob/main/apps/admin/src/main.js): spa shell · Admin entry point, route bootstrap, and shared screen chrome. · `apps/admin/src/main.js`
- [Admin auth](https://github.com/ema1322/lafileto-monorepo/blob/main/apps/admin/src/utils/auth.js): session/rbac · JWT session hydration, route gating, and access-control helpers. · `apps/admin/src/utils/auth.js`
- [Users module](https://github.com/ema1322/lafileto-monorepo/blob/main/apps/admin/src/components/users/users.js): feature module · User management screens, modals, tabs, roles, and status rendering. · `apps/admin/src/components/users/users.js`
- [Products module](https://github.com/ema1322/lafileto-monorepo/blob/main/apps/admin/src/components/products/products.js): feature module · Product management tables, modals, bindings, and state. · `apps/admin/src/components/products/products.js`
- [Categories module](https://github.com/ema1322/lafileto-monorepo/blob/main/apps/admin/src/components/categories/categories.js): feature module · Category management screens and table/modal flow. · `apps/admin/src/components/categories/categories.js`
- [Settings module](https://github.com/ema1322/lafileto-monorepo/blob/main/apps/admin/src/components/settings/settings.js): feature module · Administrative settings screen and configuration editing. · `apps/admin/src/components/settings/settings.js`
- [Dashboard](https://github.com/ema1322/lafileto-monorepo/blob/main/apps/admin/src/components/dashboard/dashboard.js): feature module · Admin overview and summary views. · `apps/admin/src/components/dashboard/dashboard.js`
- [Mock data](https://github.com/ema1322/lafileto-monorepo/blob/main/apps/admin/public/data/products.json): fixtures · Local JSON-backed admin data for offline or mock-driven screens. · `apps/admin/public/data/products.json`

## Connections

- Client shell → Client router: bootstraps · Client shell initializes the React routing flow.
- Client router → Client pages: renders · Router selects the storefront page views.
- Client pages → Public API: loads data · Pages request public catalog and order data.
- Client pages → Client state: uses · Pages share shell, async, and cart state.
- Client pages → WhatsApp handoff: handoff · Confirmation flow can transfer ordering to WhatsApp.
- Public API → Routes: calls public API · Storefront data access targets the backend public endpoints.
- Admin shell → Admin auth: guards routes · Admin navigation depends on hydrated JWT/RBAC state.
- Admin shell → Dashboard: starts · Admin shell enters at the dashboard and module chrome.
- Dashboard → Users module: links to · Dashboard and navigation expose management modules.
- Dashboard → Products module: links to · Dashboard and navigation expose management modules.
- Dashboard → Categories module: links to · Dashboard and navigation expose management modules.
- Dashboard → Settings module: links to · Dashboard and navigation expose management modules.
- Users module → Mock data: can use · Admin screens can be backed by local JSON fixtures.
- Products module → Mock data: can use · Admin screens can be backed by local JSON fixtures.
- Users module → Routes: calls API · Management screens consume backend endpoints when live.
- Products module → Routes: calls API · Management screens consume backend endpoints when live.
- Categories module → Routes: calls API · Management screens consume backend endpoints when live.
- Settings module → Routes: calls API · Management screens consume backend endpoints when live.
- API app → Edge guards: applies · All requests pass through shared API safeguards.
- API app → Routes: mounts · Express app mounts the versioned route tree.
- Routes → Auth flow: exposes · Auth routes feed login and session hydration.
- Routes → Domain services: delegates · Route handlers defer business logic to services.
- Domain services → Repositories: persists via · Services isolate Prisma access behind repositories.
- Repositories → Prisma/MySQL: uses · Repositories read and write through Prisma/MySQL.
- Auth flow → Edge guards: protected by · Auth flows are constrained by rate limiting and validation.
- Domain services → Shared utils: formats with · Domain services share formatting and text helpers.
- Client pages → Shared design: styled by · Client UI can reuse shared design assets.
- Admin shell → Shared design: styled by · Admin UI can reuse shared design assets.
