Merge pull request 'fix: fix the small issue of user_id not found' (#85) from fix/user_id into dev

Reviewed-on: freeleaps/freeleaps-service-hub#85
Reviewed-by: jingyao1991 <jingyao1991@noreply.gitea.freeleaps.mathmast.com>
This commit is contained in:
jingyao1991 2025-10-18 06:19:24 +00:00
commit e12fb4568e
2 changed files with 18 additions and 16 deletions

View File

@ -94,7 +94,7 @@ class UserProfileHandler:
async def create_provider_profile(self, user_id: str) -> ProviderProfileDoc:
provider_profile = await ProviderProfileDoc.find_one(
ProviderProfileDoc.user_id == user_id
{"user_id": user_id}
)
if provider_profile:
return provider_profile

View File

@ -10,7 +10,7 @@ from backend.models.user.models import (
AuthCodeDoc,
UsageLogDoc
)
from backend.models.user_profile.models import BasicProfileDoc
from backend.models.user_profile.models import BasicProfileDoc, ProviderProfileDoc
from backend.models.permission.models import PermissionDoc, RoleDoc, UserRoleDoc
from common.config.app_settings import app_settings
from common.log.module_logger import ModuleLogger
@ -25,7 +25,7 @@ import os
MAIN_CLIENT: Optional[AsyncIOMotorClient] = None
TENANT_CACHE: Optional['TenantDBCache'] = None
# Define document models
# Define document models
document_models = [
UsageLogDoc,
UserAccountDoc,
@ -34,6 +34,7 @@ document_models = [
UserMobileDoc,
AuthCodeDoc,
BasicProfileDoc,
ProviderProfileDoc,
PermissionDoc,
RoleDoc,
UserRoleDoc
@ -46,6 +47,7 @@ tenant_document_models = [
UserMobileDoc,
AuthCodeDoc,
BasicProfileDoc,
ProviderProfileDoc,
PermissionDoc,
RoleDoc,
UserRoleDoc
@ -58,7 +60,7 @@ class TenantDBCache:
Uses main_db.tenant_doc to resolve mongodb_uri; caches clients with LRU.
Database instances are created fresh each time from cached clients.
"""
def __init__(self, main_db: AsyncIOMotorDatabase, max_size: int = 64):
self.main_db = main_db
self.max_size = max_size
@ -69,13 +71,13 @@ class TenantDBCache:
async def get_initialized_db(self, product_id: str) -> AsyncIOMotorDatabase:
"""Get tenant database with Beanie already initialized"""
# fast-path: check if client is cached
cached_client = self._cache.get(product_id)
if cached_client:
await self.module_logger.log_info(f"Found cached client for {product_id}")
self._cache.move_to_end(product_id)
# Get fresh database instance from cached client
db = cached_client.get_default_database()
if db is not None:
@ -95,7 +97,7 @@ class TenantDBCache:
if cached_client:
await self.module_logger.log_info(f"Double-check found cached client for {product_id}")
self._cache.move_to_end(product_id)
# Get fresh database instance from cached client
db = cached_client.get_default_database()
if db is not None:
@ -149,7 +151,7 @@ class TenantDBCache:
detail=f"No default database found for tenant {product_id}",
headers={"X-Error-Message": f"No default database found for tenant {product_id}"}
)
# Initialize Beanie for this tenant database
await init_beanie(database=db, document_models=tenant_document_models)
await self.module_logger.log_info(f"Beanie initialization completed for new tenant database {product_id}")
@ -201,7 +203,7 @@ def register(app):
@app.on_event("startup")
async def start_database():
await initiate_database(app)
@app.on_event("shutdown")
async def shutdown_database():
await cleanup_database()
@ -217,9 +219,9 @@ async def check_database_initialized() -> ProbeResult:
async def initiate_database(app):
"""Initialize main database and tenant cache"""
global MAIN_CLIENT, TENANT_CACHE
module_logger = ModuleLogger(sender_id="DatabaseInit")
# 1) Create main/catalog client + DB
MAIN_CLIENT = AsyncIOMotorClient(app_settings.MONGODB_URI)
main_db = MAIN_CLIENT[app_settings.MONGODB_NAME]
@ -234,20 +236,20 @@ async def initiate_database(app):
# 4) Store on app state for middleware to access
app.state.main_db = main_db
app.state.tenant_cache = TENANT_CACHE
await module_logger.log_info("Database and tenant cache initialized successfully")
async def cleanup_database():
"""Cleanup database connections and cache"""
global MAIN_CLIENT, TENANT_CACHE
module_logger = ModuleLogger(sender_id="DatabaseCleanup")
if TENANT_CACHE:
await TENANT_CACHE.aclose()
if MAIN_CLIENT:
MAIN_CLIENT.close()
await module_logger.log_info("Database connections closed successfully")