# meslopes/portal_entregador architecture

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

- Interactive diagram: https://gitdiagram.com/meslopes/portal_entregador
- Repository: https://github.com/meslopes/portal_entregador

## Overview

#### Purpose and deployment shape
muv.log is a multi-role delivery-management SaaS: logistics admins operate marketplaces (“praças”), establishments create and track deliveries, and drivers accept and complete them. It is a React SPA deployed to Vercel, calling a Flask REST API on Render, backed by Render PostgreSQL; SQLite is present for local development at `instance/app.db`.

#### Application entry points
- Frontend bootstraps through `portal-frontend/src/main.jsx`, with routing, role-specific screens, and application shell in `portal-frontend/src/App.jsx`.
- Production backend is served as Gunicorn target `portal-backend/src/main_production.py:app`; the alternate/general app entry is `portal-backend/src/main.py`.
- Render deployment metadata is in `portal-backend/render.yaml`; frontend hosting rules are in `portal-frontend/vercel.json`.

#### Frontend identity, authorization, and tenant context
The SPA separates driver, establishment/client, admin, own-driver, and platform experiences. Authentication state and JWT-bearing API access originate in `portal-frontend/src/contexts/AuthContext.jsx` and `portal-frontend/src/lib/api.js`. Route access is enforced by `portal-frontend/src/components/ProtectedRoute.jsx` and `portal-frontend/src/components/OwnDriverProtectedRoute.jsx`.
“Square”/praça selection is client-side context in `portal-frontend/src/contexts/SquareContext.jsx`, forming a key scope boundary for operational views.

#### Backend API composition and persistence
Flask route modules express the API’s business boundaries: identity/user lifecycle in `portal-backend/src/routes/auth.py` and `portal-backend/src/routes/user.py`; delivery lifecycle in `portal-backend/src/routes/order.py` and `portal-backend/src/routes/driver.py`; admin/platform operation in `portal-backend/src/routes/admin.py` and `portal-backend/src/routes/platform.py`.
SQLAlchemy entities are centralized in `portal-backend/src/models/portal_models.py`, with user-specific models in `portal-backend/src/models/user.py`. Configuration, including production database and JWT secrets, is loaded through `portal-backend/src/config.py`.

#### Order dispatch and delivery execution
Establishments use client pages such as `portal-frontend/src/pages/client/NewOrderPage.jsx` and `portal-frontend/src/pages/client/ClientOrdersPage.jsx` to submit and monitor orders. The order API persists, offers, accepts/rejects, and advances delivery status through `portal-backend/src/routes/order.py`.
Drivers consume available/active deliveries and report activity via `portal-frontend/src/pages/OrdersPage.jsx`, `portal-frontend/src/pages/ActiveDeliveryPage.jsx`, and `portal-backend/src/routes/driver.py`. Driver location and route/map experiences span `portal-frontend/src/pages/DriverRouteMap.jsx` and `portal-frontend/src/pages/TrackPage.jsx`.

#### Operations, finance, and incentives
Admin operational control is concentrated in `portal-frontend/src/pages/admin/AdminDashboardPage.jsx`, `portal-frontend/src/pages/admin/AdminOrdersPage.jsx`, and `portal-frontend/src/pages/admin/AdminSquaresPage.jsx`, backed by `portal-backend/src/routes/admin.py`. This includes live tracking, establishments, drivers, pricing, and praça administration.
Financial reporting, payments, and withdrawals are separated server-side in `portal-backend/src/routes/finance.py`; bonus/ranking workflows are in `portal-backend/src/routes/bonus.py`. The pricing model derives freight from distance and praça pricing, then allocates driver earnings, bonus pool, and platform revenue.

#### Integrations and asynchronous boundaries
External adapter boundaries are explicit: payment/billing through `portal-backend/src/services/asaas_service.py`; iFood and Open Delivery ingestion through `portal-backend/src/services/ifood_service.py` and `portal-backend/src/services/open_delivery_service.py`; geocoding via OpenStreetMap/Nominatim in `portal-backend/src/services/geocoding.py`; and WhatsApp Business communication in `portal-backend/src/services/whatsapp_service.py`.
Inbound provider events belong in `portal-backend/src/routes/webhooks.py`. Background processing is isolated in `portal-backend/src/utils/background_tasks.py`, preventing notification/integration work from being solely coupled to synchronous request handling.

Main entry points: [`portal-frontend/src/App.jsx`](https://github.com/meslopes/portal_entregador/blob/main/portal-frontend/src/App.jsx) (Role application shell), [`portal-backend/src/models/portal_models.py`](https://github.com/meslopes/portal_entregador/blob/main/portal-backend/src/models/portal_models.py) (Delivery domain models), [`portal-backend/src/routes/order.py`](https://github.com/meslopes/portal_entregador/blob/main/portal-backend/src/routes/order.py) (Order dispatch), [`portal-backend/src/utils/background_tasks.py`](https://github.com/meslopes/portal_entregador/blob/main/portal-backend/src/utils/background_tasks.py) (Background processing)

## Diagram (Mermaid)

```mermaid
flowchart TD

subgraph group_frontend["React SPA"]
  node_spa_bootstrap["SPA bootstrap &amp; routes<br/>React entry<br/>[main.jsx]"]
  node_app_shell["Role application shell<br/>React shell<br/>[App.jsx]"]
  node_auth_client["Auth &amp; API client<br/>JWT client<br/>[AuthContext.jsx]"]
  node_route_guards["Role route guards<br/>authorization<br/>[ProtectedRoute.jsx]"]
  node_square_context["Praça scope<br/>tenant context<br/>[SquareContext.jsx]"]
  node_client_orders["Establishment orders<br/>client screens<br/>[NewOrderPage.jsx]"]
  node_driver_execution["Driver delivery flow<br/>driver screens"]
  node_tracking_maps["Tracking &amp; route maps<br/>map screens<br/>[DriverRouteMap.jsx]"]
  node_admin_operations["Admin operations<br/>admin screens"]
end

subgraph group_backend["Flask API"]
  node_api_entry["Production API<br/>Gunicorn Flask app"]
  node_api_config["API configuration<br/>[config.py]"]
  node_identity_api["Identity &amp; users<br/>Flask routes<br/>[auth.py]"]
  node_order_api["Order dispatch<br/>Flask routes<br/>[order.py]"]
  node_driver_api["Driver activity<br/>Flask routes<br/>[driver.py]"]
  node_operations_api["Admin, finance &amp; incentives<br/>Flask routes<br/>[admin.py]"]
  node_domain_models["Delivery domain models<br/>SQLAlchemy models<br/>[portal_models.py]"]
end

subgraph group_integrations["External services"]
  node_webhooks["Provider webhooks<br/>Flask routes<br/>[webhooks.py]"]
  node_provider_adapters["Payment &amp; order adapters<br/>service adapters<br/>[asaas_service.py]"]
  node_geocoding["Geocoding adapter<br/>OpenStreetMap adapter<br/>[geocoding.py]"]
  node_background_tasks["Background processing<br/>async worker"]
  node_whatsapp["WhatsApp Business<br/>messaging provider"]
end

node_postgres[("Render PostgreSQL<br/>database")]

node_spa_bootstrap -->|"mounts"| node_app_shell
node_app_shell -->|"uses"| node_auth_client
node_app_shell -->|"enforces access"| node_route_guards
node_app_shell -->|"scopes views"| node_square_context
node_app_shell -->|"serves"| node_client_orders
node_app_shell -->|"serves"| node_driver_execution
node_app_shell -->|"serves"| node_tracking_maps
node_app_shell -->|"serves"| node_admin_operations
node_auth_client -->|"JWT REST calls"| node_api_entry
node_api_entry -->|"routes"| node_identity_api
node_api_entry -->|"routes"| node_order_api
node_api_entry -->|"routes"| node_driver_api
node_api_entry -->|"routes"| node_operations_api
node_api_entry -->|"routes inbound events"| node_webhooks
node_api_config -->|"configures"| node_api_entry
node_identity_api -->|"manages users"| node_domain_models
node_order_api -->|"manages orders"| node_domain_models
node_driver_api -->|"updates execution"| node_domain_models
node_operations_api -->|"operates &amp; reports"| node_domain_models
node_domain_models -->|"persists"| node_postgres
node_order_api -.->|"dispatches async work"| node_background_tasks
node_webhooks -.->|"queues processing"| node_background_tasks
node_background_tasks -->|"integrates"| node_provider_adapters
node_background_tasks -->|"sends notifications"| node_whatsapp
node_order_api -->|"resolves locations"| node_geocoding
node_operations_api -->|"billing &amp; ingestion"| node_provider_adapters

click node_spa_bootstrap "https://github.com/meslopes/portal_entregador/blob/main/portal-frontend/src/main.jsx"
click node_app_shell "https://github.com/meslopes/portal_entregador/blob/main/portal-frontend/src/App.jsx"
click node_auth_client "https://github.com/meslopes/portal_entregador/blob/main/portal-frontend/src/contexts/AuthContext.jsx"
click node_route_guards "https://github.com/meslopes/portal_entregador/blob/main/portal-frontend/src/components/ProtectedRoute.jsx"
click node_square_context "https://github.com/meslopes/portal_entregador/blob/main/portal-frontend/src/contexts/SquareContext.jsx"
click node_client_orders "https://github.com/meslopes/portal_entregador/blob/main/portal-frontend/src/pages/client/NewOrderPage.jsx"
click node_driver_execution "https://github.com/meslopes/portal_entregador/blob/main/portal-frontend/src/pages/ActiveDeliveryPage.jsx"
click node_tracking_maps "https://github.com/meslopes/portal_entregador/blob/main/portal-frontend/src/pages/DriverRouteMap.jsx"
click node_admin_operations "https://github.com/meslopes/portal_entregador/blob/main/portal-frontend/src/pages/admin/AdminDashboardPage.jsx"
click node_api_config "https://github.com/meslopes/portal_entregador/blob/main/portal-backend/src/config.py"
click node_identity_api "https://github.com/meslopes/portal_entregador/blob/main/portal-backend/src/routes/auth.py"
click node_order_api "https://github.com/meslopes/portal_entregador/blob/main/portal-backend/src/routes/order.py"
click node_driver_api "https://github.com/meslopes/portal_entregador/blob/main/portal-backend/src/routes/driver.py"
click node_operations_api "https://github.com/meslopes/portal_entregador/blob/main/portal-backend/src/routes/admin.py"
click node_domain_models "https://github.com/meslopes/portal_entregador/blob/main/portal-backend/src/models/portal_models.py"
click node_webhooks "https://github.com/meslopes/portal_entregador/blob/main/portal-backend/src/routes/webhooks.py"
click node_provider_adapters "https://github.com/meslopes/portal_entregador/blob/main/portal-backend/src/services/asaas_service.py"
click node_geocoding "https://github.com/meslopes/portal_entregador/blob/main/portal-backend/src/services/geocoding.py"
click node_background_tasks "https://github.com/meslopes/portal_entregador/blob/main/portal-backend/src/utils/background_tasks.py"
click node_whatsapp "https://github.com/meslopes/portal_entregador/blob/main/portal-backend/src/services/whatsapp_service.py"

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_spa_bootstrap,node_app_shell,node_auth_client,node_route_guards,node_square_context,node_client_orders,node_driver_execution,node_tracking_maps,node_admin_operations toneBlue
class node_api_entry,node_api_config,node_identity_api,node_order_api,node_driver_api,node_operations_api,node_domain_models toneAmber
class node_webhooks,node_provider_adapters,node_geocoding,node_background_tasks,node_whatsapp toneMint
class node_postgres toneNeutral
```

## Components

### React SPA

Vercel-hosted role-specific portal.

- [SPA bootstrap & routes](https://github.com/meslopes/portal_entregador/blob/main/portal-frontend/src/main.jsx): React entry · `portal-frontend/src/main.jsx`
- [Role application shell](https://github.com/meslopes/portal_entregador/blob/main/portal-frontend/src/App.jsx): React shell · `portal-frontend/src/App.jsx`
- [Auth & API client](https://github.com/meslopes/portal_entregador/blob/main/portal-frontend/src/contexts/AuthContext.jsx): JWT client · Maintains session state and authenticated API access. · `portal-frontend/src/contexts/AuthContext.jsx`
- [Role route guards](https://github.com/meslopes/portal_entregador/blob/main/portal-frontend/src/components/ProtectedRoute.jsx): authorization · `portal-frontend/src/components/ProtectedRoute.jsx`
- [Praça scope](https://github.com/meslopes/portal_entregador/blob/main/portal-frontend/src/contexts/SquareContext.jsx): tenant context · Client-side selected praça boundary. · `portal-frontend/src/contexts/SquareContext.jsx`
- [Establishment orders](https://github.com/meslopes/portal_entregador/blob/main/portal-frontend/src/pages/client/NewOrderPage.jsx): client screens · Creates and monitors deliveries. · `portal-frontend/src/pages/client/NewOrderPage.jsx`
- [Driver delivery flow](https://github.com/meslopes/portal_entregador/blob/main/portal-frontend/src/pages/ActiveDeliveryPage.jsx): driver screens · Available and active delivery execution. · `portal-frontend/src/pages/ActiveDeliveryPage.jsx`
- [Tracking & route maps](https://github.com/meslopes/portal_entregador/blob/main/portal-frontend/src/pages/DriverRouteMap.jsx): map screens · `portal-frontend/src/pages/DriverRouteMap.jsx`
- [Admin operations](https://github.com/meslopes/portal_entregador/blob/main/portal-frontend/src/pages/admin/AdminDashboardPage.jsx): admin screens · Operational, praça, pricing, and live-tracking control. · `portal-frontend/src/pages/admin/AdminDashboardPage.jsx`

### Flask API

Render-hosted REST API and business logic.

- **Production API**: Gunicorn Flask app
- [API configuration](https://github.com/meslopes/portal_entregador/blob/main/portal-backend/src/config.py): configuration · Loads database and JWT production settings. · `portal-backend/src/config.py`
- [Identity & users](https://github.com/meslopes/portal_entregador/blob/main/portal-backend/src/routes/auth.py): Flask routes · `portal-backend/src/routes/auth.py`
- [Order dispatch](https://github.com/meslopes/portal_entregador/blob/main/portal-backend/src/routes/order.py): Flask routes · Persists orders, offers work, and advances delivery status. · `portal-backend/src/routes/order.py`
- [Driver activity](https://github.com/meslopes/portal_entregador/blob/main/portal-backend/src/routes/driver.py): Flask routes · `portal-backend/src/routes/driver.py`
- [Admin, finance & incentives](https://github.com/meslopes/portal_entregador/blob/main/portal-backend/src/routes/admin.py): Flask routes · Controls operations and calculates reporting, payments, and bonuses. · `portal-backend/src/routes/admin.py`
- [Delivery domain models](https://github.com/meslopes/portal_entregador/blob/main/portal-backend/src/models/portal_models.py): SQLAlchemy models · `portal-backend/src/models/portal_models.py`

### External services

Provider adapters and asynchronous work.

- [Provider webhooks](https://github.com/meslopes/portal_entregador/blob/main/portal-backend/src/routes/webhooks.py): Flask routes · Receives inbound provider events. · `portal-backend/src/routes/webhooks.py`
- [Payment & order adapters](https://github.com/meslopes/portal_entregador/blob/main/portal-backend/src/services/asaas_service.py): service adapters · Asaas billing plus iFood and Open Delivery ingestion. · `portal-backend/src/services/asaas_service.py`
- [Geocoding adapter](https://github.com/meslopes/portal_entregador/blob/main/portal-backend/src/services/geocoding.py): OpenStreetMap adapter · `portal-backend/src/services/geocoding.py`
- [Background processing](https://github.com/meslopes/portal_entregador/blob/main/portal-backend/src/utils/background_tasks.py): async worker · Decouples notifications and integration work from requests. · `portal-backend/src/utils/background_tasks.py`
- [WhatsApp Business](https://github.com/meslopes/portal_entregador/blob/main/portal-backend/src/services/whatsapp_service.py): messaging provider · `portal-backend/src/services/whatsapp_service.py`

### Other components

- **Render PostgreSQL**: database · Production system of record.

## Connections

- SPA bootstrap & routes → Role application shell: mounts
- Role application shell → Auth & API client: uses
- Role application shell → Role route guards: enforces access
- Role application shell → Praça scope: scopes views
- Role application shell → Establishment orders: serves
- Role application shell → Driver delivery flow: serves
- Role application shell → Tracking & route maps: serves
- Role application shell → Admin operations: serves
- Auth & API client → Production API: JWT REST calls
- Production API → Identity & users: routes
- Production API → Order dispatch: routes
- Production API → Driver activity: routes
- Production API → Admin, finance & incentives: routes
- Production API → Provider webhooks: routes inbound events
- API configuration → Production API: configures
- Identity & users → Delivery domain models: manages users
- Order dispatch → Delivery domain models: manages orders
- Driver activity → Delivery domain models: updates execution
- Admin, finance & incentives → Delivery domain models: operates & reports
- Delivery domain models → Render PostgreSQL: persists
- Order dispatch → Background processing: dispatches async work
- Provider webhooks → Background processing: queues processing
- Background processing → Payment & order adapters: integrates
- Background processing → WhatsApp Business: sends notifications
- Order dispatch → Geocoding adapter: resolves locations
- Admin, finance & incentives → Payment & order adapters: billing & ingestion
