# cherryrambler/project--work architecture

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

- Interactive diagram: https://gitdiagram.com/cherryrambler/project--work
- Repository: https://github.com/cherryrambler/project--work

## Overview

#### 1. Purpose and deployment shape
Area Authorization System is a single FastAPI backend plus React/Vite client for RBAC-managed geographic areas and survey records. The former auth/area split is consolidated into one API and one PostgreSQL/PostGIS database, avoiding cross-service user synchronization.

- Backend entry point: `area-auth-api/app/main.py`
- Frontend entry point: `auth-frontend/src/main.jsx`
- Client composition/state: `auth-frontend/src/App.jsx`

#### 2. API composition and request boundary
`app/main.py` assembles versioned routers for authentication, authorized areas, and surveys. Routers are the HTTP boundary; Pydantic schemas validate request/response shapes before domain persistence.

- Authentication/session endpoints: `area-auth-api/app/routers/auth.py`
- Authorized-area and audit endpoints: `area-auth-api/app/routers/areas.py`
- Survey-record endpoints: `area-auth-api/app/routers/surveys.py`
- Contract definitions: `area-auth-api/app/schemas/auth.py`, `area-auth-api/app/schemas/area.py`, `area-auth-api/app/schemas/survey.py`

#### 3. Identity, sessions, and authorization
The security path is: public registration/login validates credentials, persists a device/login session with refresh token, and returns JWTs. Protected requests decode access JWTs, reload the user from the database, reject deactivated users immediately, then apply self-or-admin or role checks. Login failures support lockout; admins can activate/deactivate and unlock accounts.

- JWT/password and token logic: `area-auth-api/app/core/security.py`
- Current-user and role dependencies: `area-auth-api/app/core/dependencies.py`
- User/session persistence: `area-auth-api/app/models/user.py`, `area-auth-api/app/models/session.py`
- Endpoint orchestration: `area-auth-api/app/routers/auth.py`

#### 4. Geospatial authorization areas
Each user has at most one PostGIS polygon authorized area, keyed directly to the user and deleted on user deletion. Area mutations and reads enforce self-or-admin scope. Authenticated users can test whether a point falls within their own polygon. Shared geometry conversion/validation separates coordinate input from PostGIS model operations.

- Area routes and containment check: `area-auth-api/app/routers/areas.py`
- PostGIS area model: `area-auth-api/app/models/authorized_area.py`
- Geometry utilities: `area-auth-api/app/core/geo_utils.py`

#### 5. Survey-record lifecycle
Survey records are distinct from authorized areas: users can own many polygon-backed, village/plot-labeled records. Admins create, list, update, reassign, and delete them; record access and verification are self-or-admin according to the endpoint rules. Survey mutations emit audit events.

- Lifecycle endpoints: `area-auth-api/app/routers/surveys.py`
- Persistent record model: `area-auth-api/app/models/survey_record.py`
- Input/output validation: `area-auth-api/app/schemas/survey.py`

#### 6. Persistence, audit, and infrastructure
SQLAlchemy async database access targets PostgreSQL 14+ with PostGIS. The primary relational graph is users → sessions, authorized area, surveys, and audit events; session, area, and survey foreign keys cascade on user deletion. Audit logs deliberately retain a nullable non-FK user ID and capture action, resource, success, and request IP.

- DB engine/session and declarative base: `area-auth-api/app/db/session.py`
- Audit model/logger/actions: `area-auth-api/app/models/audit_log.py`, `area-auth-api/app/core/audit_logger.py`, `area-auth-api/app/core/audit_actions.py`
- Environment configuration: `area-auth-api/app/core/config.py`

#### 7. Frontend/API flow
The React client manages active auth state directly in `App.jsx`. API calls use a configured backend URL and localStorage tokens; a shared request layer coalesces refresh attempts on 401, retries the failed request with a new access token, and signals logout only when refresh fails. Dashboard UI displays and manages the caller’s area without external map tiles.

- Shared fetch, refresh, and API base handling: `auth-frontend/src/api/config.js`
- Auth/area request calls: `auth-frontend/src/api/auth.js`
- Login/dashboard views: `auth-frontend/src/pages/AuthPage.jsx`, `auth-frontend/src/pages/DashboardPage.jsx`

Main entry points: [`area-auth-api/app/routers/areas.py`](https://github.com/cherryrambler/project--work/blob/main/area-auth-api/app/routers/areas.py) (Area endpoints), [`area-auth-api/app/db/session.py`](https://github.com/cherryrambler/project--work/blob/main/area-auth-api/app/db/session.py) (Async DB access), [`area-auth-api/app/routers/auth.py`](https://github.com/cherryrambler/project--work/blob/main/area-auth-api/app/routers/auth.py) (Auth endpoints), [`area-auth-api/app/routers/surveys.py`](https://github.com/cherryrambler/project--work/blob/main/area-auth-api/app/routers/surveys.py) (Survey endpoints)

## Diagram (Mermaid)

```mermaid
flowchart TD

subgraph group_backend["FastAPI backend"]
  node_api["API assembly<br/>FastAPI app<br/>[main.py]"]
  node_auth_routes["Auth endpoints<br/>router<br/>[auth.py]"]
  node_area_routes["Area endpoints<br/>router<br/>[areas.py]"]
  node_survey_routes["Survey endpoints<br/>router<br/>[surveys.py]"]
  node_contracts["API contracts<br/>Pydantic schemas<br/>[auth.py]"]
  node_security{{"JWT and passwords<br/>security service<br/>[security.py]"}}
  node_authorization{{"Current-user guards<br/>FastAPI dependencies<br/>[dependencies.py]"}}
  node_geo["Geometry validation<br/>geospatial utility<br/>[geo_utils.py]"]
  node_audit["Audit logger<br/>audit service<br/>[audit_logger.py]"]
  node_db_access["Async DB access<br/>SQLAlchemy session<br/>[session.py]"]
end

subgraph group_frontend["React client"]
  node_web_app["Client composition<br/>React state<br/>[App.jsx]"]
  node_api_client["API request layer<br/>fetch client<br/>[config.js]"]
  node_auth_ui["Login view<br/>React page<br/>[AuthPage.jsx]"]
  node_dashboard["Area dashboard<br/>React page<br/>[DashboardPage.jsx]"]
end

subgraph group_data["PostgreSQL / PostGIS"]
  node_users_sessions[("Users and sessions<br/>identity models<br/>[user.py]")]
  node_authorized_areas[("Authorized areas<br/>PostGIS model<br/>[authorized_area.py]")]
  node_survey_records[("Survey records<br/>PostGIS model<br/>[survey_record.py]")]
  node_audit_logs[("Audit logs<br/>audit model<br/>[audit_log.py]")]
end

node_web_app -->|"renders"| node_auth_ui
node_web_app -->|"renders"| node_dashboard
node_web_app -->|"uses"| node_api_client
node_api_client -->|"HTTPS with JWT"| node_api
node_api -->|"mounts"| node_auth_routes
node_api -->|"mounts"| node_area_routes
node_api -->|"mounts"| node_survey_routes
node_auth_routes -->|"validates"| node_contracts
node_area_routes -->|"validates"| node_contracts
node_survey_routes -->|"validates"| node_contracts
node_auth_routes -->|"login and refresh"| node_security
node_auth_routes -->|"protects requests"| node_authorization
node_area_routes -->|"enforces scope"| node_authorization
node_survey_routes -->|"enforces scope"| node_authorization
node_area_routes -->|"converts polygons"| node_geo
node_auth_routes -->|"persists identity"| node_db_access
node_area_routes -->|"reads and writes"| node_db_access
node_survey_routes -->|"reads and writes"| node_db_access
node_db_access -->|"maps"| node_users_sessions
node_db_access -->|"maps"| node_authorized_areas
node_db_access -->|"maps"| node_survey_records
node_audit -->|"writes"| node_audit_logs
node_area_routes -->|"logs mutations"| node_audit
node_survey_routes -->|"logs mutations"| node_audit
node_users_sessions -->|"one user area; cascade delete"| node_authorized_areas
node_users_sessions -->|"owns many; cascade delete"| node_survey_records

click node_api "https://github.com/cherryrambler/project--work/blob/main/area-auth-api/app/main.py"
click node_auth_routes "https://github.com/cherryrambler/project--work/blob/main/area-auth-api/app/routers/auth.py"
click node_area_routes "https://github.com/cherryrambler/project--work/blob/main/area-auth-api/app/routers/areas.py"
click node_survey_routes "https://github.com/cherryrambler/project--work/blob/main/area-auth-api/app/routers/surveys.py"
click node_contracts "https://github.com/cherryrambler/project--work/blob/main/area-auth-api/app/schemas/auth.py"
click node_security "https://github.com/cherryrambler/project--work/blob/main/area-auth-api/app/core/security.py"
click node_authorization "https://github.com/cherryrambler/project--work/blob/main/area-auth-api/app/core/dependencies.py"
click node_geo "https://github.com/cherryrambler/project--work/blob/main/area-auth-api/app/core/geo_utils.py"
click node_audit "https://github.com/cherryrambler/project--work/blob/main/area-auth-api/app/core/audit_logger.py"
click node_db_access "https://github.com/cherryrambler/project--work/blob/main/area-auth-api/app/db/session.py"
click node_users_sessions "https://github.com/cherryrambler/project--work/blob/main/area-auth-api/app/models/user.py"
click node_authorized_areas "https://github.com/cherryrambler/project--work/blob/main/area-auth-api/app/models/authorized_area.py"
click node_survey_records "https://github.com/cherryrambler/project--work/blob/main/area-auth-api/app/models/survey_record.py"
click node_audit_logs "https://github.com/cherryrambler/project--work/blob/main/area-auth-api/app/models/audit_log.py"
click node_web_app "https://github.com/cherryrambler/project--work/blob/main/auth-frontend/src/App.jsx"
click node_api_client "https://github.com/cherryrambler/project--work/blob/main/auth-frontend/src/api/config.js"
click node_auth_ui "https://github.com/cherryrambler/project--work/blob/main/auth-frontend/src/pages/AuthPage.jsx"
click node_dashboard "https://github.com/cherryrambler/project--work/blob/main/auth-frontend/src/pages/DashboardPage.jsx"

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,node_auth_routes,node_area_routes,node_survey_routes,node_contracts,node_security,node_authorization,node_geo,node_audit,node_db_access toneBlue
class node_web_app,node_api_client,node_auth_ui,node_dashboard toneAmber
class node_users_sessions,node_authorized_areas,node_survey_records,node_audit_logs toneMint
```

## Components

### FastAPI backend

- [API assembly](https://github.com/cherryrambler/project--work/blob/main/area-auth-api/app/main.py): FastAPI app · Mounts versioned auth, area, and survey routers. · `area-auth-api/app/main.py`
- [Auth endpoints](https://github.com/cherryrambler/project--work/blob/main/area-auth-api/app/routers/auth.py): router · `area-auth-api/app/routers/auth.py`
- [Area endpoints](https://github.com/cherryrambler/project--work/blob/main/area-auth-api/app/routers/areas.py): router · Manages authorized areas, containment checks, and audit-facing actions. · `area-auth-api/app/routers/areas.py`
- [Survey endpoints](https://github.com/cherryrambler/project--work/blob/main/area-auth-api/app/routers/surveys.py): router · `area-auth-api/app/routers/surveys.py`
- [API contracts](https://github.com/cherryrambler/project--work/blob/main/area-auth-api/app/schemas/auth.py): Pydantic schemas · Validates authentication, area, and survey payloads. · `area-auth-api/app/schemas/auth.py`
- [JWT and passwords](https://github.com/cherryrambler/project--work/blob/main/area-auth-api/app/core/security.py): security service · Issues tokens and validates credentials. · `area-auth-api/app/core/security.py`
- [Current-user guards](https://github.com/cherryrambler/project--work/blob/main/area-auth-api/app/core/dependencies.py): FastAPI dependencies · Reloads users and enforces role or self-or-admin scope. · `area-auth-api/app/core/dependencies.py`
- [Geometry validation](https://github.com/cherryrambler/project--work/blob/main/area-auth-api/app/core/geo_utils.py): geospatial utility · Converts coordinate input for PostGIS operations. · `area-auth-api/app/core/geo_utils.py`
- [Audit logger](https://github.com/cherryrambler/project--work/blob/main/area-auth-api/app/core/audit_logger.py): audit service · Records mutation outcome, resource, action, and request IP. · `area-auth-api/app/core/audit_logger.py`
- [Async DB access](https://github.com/cherryrambler/project--work/blob/main/area-auth-api/app/db/session.py): SQLAlchemy session · `area-auth-api/app/db/session.py`

### React client

- [Client composition](https://github.com/cherryrambler/project--work/blob/main/auth-frontend/src/App.jsx): React state · Owns active authentication state and view composition. · `auth-frontend/src/App.jsx`
- [API request layer](https://github.com/cherryrambler/project--work/blob/main/auth-frontend/src/api/config.js): fetch client · Uses local tokens, coalesces refreshes, and retries 401s. · `auth-frontend/src/api/config.js`
- [Login view](https://github.com/cherryrambler/project--work/blob/main/auth-frontend/src/pages/AuthPage.jsx): React page · `auth-frontend/src/pages/AuthPage.jsx`
- [Area dashboard](https://github.com/cherryrambler/project--work/blob/main/auth-frontend/src/pages/DashboardPage.jsx): React page · Displays and manages the caller's area. · `auth-frontend/src/pages/DashboardPage.jsx`

### PostgreSQL / PostGIS

- [Users and sessions](https://github.com/cherryrambler/project--work/blob/main/area-auth-api/app/models/user.py): identity models · Device/login sessions retain refresh tokens and cascade with users. · `area-auth-api/app/models/user.py`
- [Authorized areas](https://github.com/cherryrambler/project--work/blob/main/area-auth-api/app/models/authorized_area.py): PostGIS model · One user-owned polygon per user. · `area-auth-api/app/models/authorized_area.py`
- [Survey records](https://github.com/cherryrambler/project--work/blob/main/area-auth-api/app/models/survey_record.py): PostGIS model · Many polygon-backed village or plot records per owner. · `area-auth-api/app/models/survey_record.py`
- [Audit logs](https://github.com/cherryrambler/project--work/blob/main/area-auth-api/app/models/audit_log.py): audit model · Retains nullable actor IDs without a user foreign key. · `area-auth-api/app/models/audit_log.py`

## Connections

- Client composition → Login view: renders
- Client composition → Area dashboard: renders
- Client composition → API request layer: uses
- API request layer → API assembly: HTTPS with JWT
- API assembly → Auth endpoints: mounts
- API assembly → Area endpoints: mounts
- API assembly → Survey endpoints: mounts
- Auth endpoints → API contracts: validates
- Area endpoints → API contracts: validates
- Survey endpoints → API contracts: validates
- Auth endpoints → JWT and passwords: login and refresh
- Auth endpoints → Current-user guards: protects requests
- Area endpoints → Current-user guards: enforces scope
- Survey endpoints → Current-user guards: enforces scope
- Area endpoints → Geometry validation: converts polygons
- Auth endpoints → Async DB access: persists identity
- Area endpoints → Async DB access: reads and writes
- Survey endpoints → Async DB access: reads and writes
- Async DB access → Users and sessions: maps
- Async DB access → Authorized areas: maps
- Async DB access → Survey records: maps
- Audit logger → Audit logs: writes
- Area endpoints → Audit logger: logs mutations
- Survey endpoints → Audit logger: logs mutations
- Users and sessions → Authorized areas: one user area; cascade delete
- Users and sessions → Survey records: owns many; cascade delete
