Skip to content

Repository files navigation

Audit Tools Backend

FastAPI backend for the Audit Tools platform. This repository serves two product namespaces from one codebase:

  • YEE: full User-backed authentication, onboarding, approvals, invites, dashboard, reporting, and submission workflows
  • Playspace: shared-core dashboard and audit APIs plus a lightweight account-based mobile auth bootstrap used by the current mobile client

What This Repo Owns

  • shared SQLAlchemy models and product-scoped database access
  • Alembic migrations for both yee and playspace
  • product REST routes under /yee/* and /playspace/*
  • YEE auth, onboarding, invite, reporting, and export flows
  • Playspace audit session, assignment, dashboard, and management flows
  • deterministic seed data for local development and integration tests

Product Split

The most important integration boundary in this repository is auth:

  • YEE auth: implemented with the users table in app/auth.py
  • Playspace auth: uses the same signed User session model for /playspace/auth/signup, /playspace/auth/login, /playspace/auth/me, and downstream Playspace product routes, with x-demo-* actor headers kept only as a temporary compatibility fallback in app/core/actors.py

That split is intentional for now. Do not assume a change in one product's auth flow is automatically safe for the other.

Current Status

Implemented today:

  • shared-core account, project, place (with address field), auditor-profile, assignment, and audit models
  • YEE real auth with email verification, approvals, invite acceptance, and session state
  • Playspace-only playspace_submissions storage with scoring-backed draft/submit flows
  • manager/admin Playspace dashboards with separate audit / survey / full-audit place rollups
  • manager multi-user auth: each manager profile now gets a dedicated User record
  • Playspace enum types for structured field values
  • YEE instrument metadata enrichment for section intros, comment prompts, and grouped question rendering
  • manager-scoped YEE edit APIs for projects and places
  • Playspace bug report submission and admin/manager review flows
  • seeded test data and product-scoped migration support

Still evolving:

  • richer settings and lifecycle management
  • production automation around migrations and release verification
  • removal of the remaining x-demo-* compatibility fallback once all clients stop sending it
  • final reporting polish and cap-score decisions

Repository Layout

audit-tools-backend/
├── alembic/
├── app/
│   ├── auth.py
│   ├── auth_security.py
│   ├── core/
│   ├── database.py
│   ├── email_service/
│   ├── main.py
│   ├── models.py
│   ├── products/
│   │   ├── playspace/
│   │   │   ├── instruments/
│   │   │   ├── jobs/
│   │   │   ├── routes/
│   │   │   ├── schemas/
│   │   │   └── services/
│   │   └── yee/
│   └── seed.py
├── docs/
├── tests/
├── .env.example
├── README.md
├── SCHEMA.md
└── STRUCTURE.md

Local Setup

  1. Create and activate a virtual environment:
python -m venv .venv
source .venv/bin/activate
python -m pip install --upgrade pip
python -m pip install -r requirements.txt
  1. Create a local env file:
cp .env.example .env
  1. Set product database URLs for each tier you use (dev, prod, test). You only need to configure these once - switch targets at runtime with ENVIRONMENT:
ENVIRONMENT=development

DEV_DATABASE_URL_YEE=postgresql+asyncpg://postgres:postgres@localhost:5432/audit_tools_yee
DEV_DATABASE_URL_PLAYSPACE=postgresql+asyncpg://postgres:postgres@localhost:5432/audit_tools_playspace
DATABASE_URL_YEE=postgresql+asyncpg://...
DATABASE_URL_PLAYSPACE=postgresql+asyncpg://...
TEST_DATABASE_URL_PLAYSPACE=postgresql+asyncpg://...
AUTH_TOKEN_SECRET_KEY=change-me
AUTH_ACCESS_TOKEN_TTL_DAYS=7
AUTH_EMAIL_VERIFY_TTL_HOURS=24
AUTH_VERIFY_URL_TEMPLATE=http://localhost:3000/verify-email?token={token}

Resolution order matches Alembic -x environment=...:

ENVIRONMENT Playspace URL keys tried (in order)
development (default) DEV_DATABASE_URL_PLAYSPACE → DATABASE_URL_PLAYSPACE
production DATABASE_URL_PLAYSPACE
test TEST_DATABASE_URL_PLAYSPACE → DATABASE_URL_PLAYSPACE
  1. Apply migrations to both product databases. Each product has its own Alembic branch (yee / playspace) sharing a common core base, so target the product-scoped branch head rather than the bare head:
alembic -x product=yee -x environment=development upgrade yee@head
alembic -x product=playspace -x environment=development upgrade playspace@head

For production or test databases:

alembic -x product=playspace -x environment=production upgrade playspace@head
alembic -x product=playspace -x environment=test upgrade playspace@head
  1. Seed demo data when needed:
./.venv/bin/python -m app.seed --allow-destructive

Or seed one product only:

./.venv/bin/python -m app.seed --allow-destructive --product yee
./.venv/bin/python -m app.seed --allow-destructive --product playspace
  1. Start the API:
uvicorn app.main:app --reload

Point the server at a different database tier without editing .env:

ENVIRONMENT=production uvicorn app.main:app --reload
ENVIRONMENT=test uvicorn app.main:app --reload

On startup the backend logs the active ENVIRONMENT and resolved host/database for each product.

Useful local URLs:

  • http://127.0.0.1:8000/health
  • http://127.0.0.1:8000/yee/auth/login
  • http://127.0.0.1:8000/yee/instrument
  • http://127.0.0.1:8000/playspace/auth/login
  • http://127.0.0.1:8000/playspace/instrument

Recent YEE API Additions

The latest YEE website iteration depends on a few backend behaviors that are important for future engineers to preserve:

  • GET /yee/instrument now returns section metadata used by the frontend to render:
    • section intro text
    • per-section optional comment prompts
    • block titles
    • item-kind hints used for grouped presence/condition question display
  • PATCH /yee/dashboard/projects/{project_id} allows manager-scoped project edits
  • PATCH /yee/dashboard/places/{place_id} allows manager-scoped place edits, including postal code and address updates

Those contracts are consumed directly by the Next.js frontend for:

  • spreadsheet-aligned YEE wording and domain instructions
  • conditional domain question rendering
  • manager project/place edit flows with persistence
  • review/report pages that depend on richer score and instrument context

Seeded Demo Credentials

The current seed flows use the shared demo password:

  • DemoPass123!

This applies to seeded auth-capable demo accounts used in local development and integration tests.

If a shared demo account drifts from that password in a non-destructive YEE environment, you can restore the seeded demo passwords without wiping the whole database:

./.venv/bin/python -m app.reset_demo_passwords

To restore only one account:

./.venv/bin/python -m app.reset_demo_passwords --email manager-demo@yee.local

The backend also protects the seeded YEE demo emails by default so invite acceptance and self-service password reset flows do not mutate those shared review accounts. You can override that list with PROTECTED_YEE_DEMO_EMAILS if your deployment needs different protected identities.

Testing

Fast checks:

./.venv/bin/python -m py_compile app/auth.py app/seed.py
./.venv/bin/pytest tests/test_auth_security.py

When changing PLAYSPACE_RELEASE_POLICY or YEE_RELEASE_POLICY version fields, update the matching pytest assertions in the same change. See .cursor/rules/mobile-release-policy-test-sync.mdc and .claude/skills/mobile-release-policy-test-sync/SKILL.md.

Playspace integration coverage uses a dedicated test database:

TEST_DATABASE_URL_PLAYSPACE=postgresql://... ./.venv/bin/pytest tests/products/playspace

Deployment Notes

  • Run migrations for both products before serving new code.
  • The checked-in render.yaml runs both product migrations automatically in its preDeployCommand (alembic -x product=yee upgrade yee@head && alembic -x product=playspace upgrade playspace@head) before the new release takes traffic. Keep that command in place when editing render.yaml. If you deploy outside Render, your own release process must run both migrations before traffic shifts.
  • Treat one-way compatibility migrations as production operations: back up first.

Release sequence on Render:

  1. Push code; Render builds, then preDeployCommand migrates yee then playspace.
  2. The new instance starts uvicorn and serves once /health passes.
  3. Verify auth and one product-specific flow per namespace.

For the full topology, env keys, cron job, and a manual (non-Render) runbook, see docs/deployment.md. For which clients consume each namespace, see docs/client-map.md.

Documentation Map

  • docs/architecture.md: product boundaries, runtime model, and request flows
  • docs/client-map.md: which clients consume /yee/* vs /playspace/*
  • docs/deployment.md: production setup and migration runbook
  • docs/scoring.md: YEE scoring behavior
  • docs/roles-and-permissions.md: role matrix
  • SCHEMA.md: current schema reference
  • STRUCTURE.md: code organization

High-Risk Areas

When changing this repository, double-check:

  • app/auth.py: product-aware auth behavior
  • app/models.py and alembic/versions/: schema/code alignment, especially playspace_submissions
  • app/core/actors.py: Playspace header-based actor resolution
  • app/products/playspace/seed_data.py and app/seed.py: demo credentials and seeded contracts
  • app/products/playspace/routes/, app/products/playspace/schemas/, and app/products/playspace/services/: Playspace API contracts consumed by mobile
  • tests/products/playspace/: API contract coverage for the mobile-facing surface
  • app/products/mobile_release_policy.py: COPA/YEE force-update floors; pair with tests/products/yee/test_mobile_release_policy.py and test_playspace_mobile_release_policy_is_public

Releases

Packages

Contributors

Languages