From 264d3f856852c23622325033cd3607290fb237b5 Mon Sep 17 00:00:00 2001 From: miigaa Date: Thu, 23 Jul 2026 22:11:05 +0800 Subject: [PATCH] FIxed 3 issues. - Database connections issue fixed - Caching only propertyId issue fixed - Round up logic added when return total_revenue By Myagmarsuren Tserenchimed. --- backend/app/core/database_pool.py | 8 ++++---- backend/app/services/cache.py | 2 +- backend/app/services/reservations.py | 8 ++++++-- 3 files changed, 11 insertions(+), 7 deletions(-) diff --git a/backend/app/core/database_pool.py b/backend/app/core/database_pool.py index d638dfcfe..de8279c18 100644 --- a/backend/app/core/database_pool.py +++ b/backend/app/core/database_pool.py @@ -1,6 +1,6 @@ import asyncio from sqlalchemy.ext.asyncio import create_async_engine, AsyncSession, async_sessionmaker -from sqlalchemy.pool import QueuePool +from sqlalchemy.pool import AsyncAdaptedQueuePool import logging from ..config import settings @@ -15,11 +15,11 @@ async def initialize(self): """Initialize database connection pool""" try: # Create async engine with connection pooling - database_url = f"postgresql+asyncpg://{settings.supabase_db_user}:{settings.supabase_db_password}@{settings.supabase_db_host}:{settings.supabase_db_port}/{settings.supabase_db_name}" + database_url = settings.database_url.replace("postgresql://", "postgresql+asyncpg://", 1) self.engine = create_async_engine( database_url, - poolclass=QueuePool, + poolclass=AsyncAdaptedQueuePool, pool_size=20, # Number of connections to maintain max_overflow=30, # Additional connections when needed pool_pre_ping=True, # Validate connections @@ -45,7 +45,7 @@ async def close(self): if self.engine: await self.engine.dispose() - async def get_session(self) -> AsyncSession: + def get_session(self) -> AsyncSession: """Get database session from pool""" if not self.session_factory: raise Exception("Database pool not initialized") diff --git a/backend/app/services/cache.py b/backend/app/services/cache.py index b81474957..672e3f9d6 100644 --- a/backend/app/services/cache.py +++ b/backend/app/services/cache.py @@ -10,7 +10,7 @@ async def get_revenue_summary(property_id: str, tenant_id: str) -> Dict[str, Any """ Fetches revenue summary, utilizing caching to improve performance. """ - cache_key = f"revenue:{property_id}" + cache_key = f"revenue:{tenant_id}:{property_id}" # Try to get from cache cached = await redis_client.get(cache_key) diff --git a/backend/app/services/reservations.py b/backend/app/services/reservations.py index 384bd00ab..55c1c93dd 100644 --- a/backend/app/services/reservations.py +++ b/backend/app/services/reservations.py @@ -1,7 +1,9 @@ from datetime import datetime -from decimal import Decimal +from decimal import Decimal, ROUND_HALF_UP from typing import Dict, Any, List +CENTS = Decimal("0.01") + async def calculate_monthly_revenue(property_id: str, month: int, year: int, db_session=None) -> Decimal: """ Calculates revenue for a specific month. @@ -65,7 +67,9 @@ async def calculate_total_revenue(property_id: str, tenant_id: str) -> Dict[str, row = result.fetchone() if row: - total_revenue = Decimal(str(row.total_revenue)) + # Round once, here, at the source of truth - sum the exact + # sub-cent amounts first, then round to cents a single time. + total_revenue = Decimal(str(row.total_revenue)).quantize(CENTS, rounding=ROUND_HALF_UP) return { "property_id": property_id, "tenant_id": tenant_id,