Skip to content

fix: tenant isolation and revenue accuracy on dashboard - #230

Open
mdhyaksa wants to merge 1 commit into
Base360-AI:mainfrom
mdhyaksa:fix/dashboard-tenant-revenue
Open

mdhyaksa wants to merge 1 commit into
Base360-AI:mainfrom
mdhyaksa:fix/dashboard-tenant-revenue

Conversation

@mdhyaksa

Copy link
Copy Markdown

Summary

Fixes the three issues reported on the revenue dashboard.

Cross-tenant data leak (Ocean Rentals)

  • Revenue cache key had no tenant. revenue:{property_id} was shared by tenant-a and tenant-b, which both own a prop-001. Whoever loaded first had their figures cached for 5 minutes and shown to the other company. Key is now revenue:{tenant_id}:{property_id}.
  • The mock fallback ignored the tenant. On any database error, calculate_total_revenue returned hardcoded per-property figures to every tenant. Removed; errors now surface.
  • Users without a tenant got someone else's data. TenantResolver defaulted unknown users to tenant-a, and the dashboard defaulted to "default_tenant". Both now refuse instead: no tenant / 403.
  • The property selector was hardcoded with every tenant's properties. A new GET /api/v1/dashboard/properties returns only the caller's properties, and the frontend uses it.

Wrong March totals (Sunset Properties)

  • The dashboard never reached Postgres, so it always served mock numbers. DatabasePool had four bugs:
    • It read non-existent supabase_db_* settings.
    • It used a sync QueuePool on an async engine.
    • get_session was async def but used with async with.
    • greenlet was missing (fixed with sqlalchemy[asyncio]).
    • It also created a new pool per request; it now uses the shared pool.
  • With real data, prop-001 shows 2250.00 / 4 bookings. The mock showed 1000.00 / 3 and dropped res-tz-1, which is 2024-02-29 23:30 UTC = 1 March in Europe/Paris.
  • calculate_monthly_revenue now computes month boundaries in the property's timezone and takes tenant_id.

Totals off by a few cents

  • Totals are stored as NUMERIC(10,3). They were converted to float and then rounded in JS with Math.round(x*100)/100. They are now rounded server-side with Decimal.quantize(..., ROUND_HALF_UP).

Test plan

  • docker compose up -d --build (the backend needs the new greenlet dependency)
  • docker compose exec redis redis-cli FLUSHALL (the old cache keys are tenant-less)
  • As sunset@propertyflow.com: the selector lists only Beach House Alpha, City Apartment Downtown and Country Villa Estate; prop-001 shows 2250.00, 4 bookings
  • As ocean@propertyflow.com: the selector lists only Mountain Lodge Beta, Lakeside Cottage and Urban Loft Modern; prop-001 shows 0.00, not Sunset's figures

🤖 Generated with Claude Code

- Cache revenue per tenant (key was property-only; prop-001 exists in two tenants)
- Fix DB pool so dashboard reads Postgres instead of hardcoded mock data:
  use settings.database_url, drop sync QueuePool, make get_session sync,
  share one pool, add sqlalchemy[asyncio] for greenlet
- Remove tenant-agnostic mock revenue fallback; surface DB errors
- Reject users without a tenant instead of defaulting to tenant-a
- Round revenue with Decimal ROUND_HALF_UP server-side
- Month bounds in calculate_monthly_revenue use property timezone
- Add /dashboard/properties scoped to tenant; frontend selector uses it
  instead of a hardcoded cross-tenant property list

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

1 participant