Improve backend

This commit is contained in:
Urtzi Alfaro
2025-11-18 07:17:17 +01:00
parent d36f2ab9af
commit 5c45164c8e
61 changed files with 9846 additions and 495 deletions

View File

@@ -59,9 +59,9 @@ class WhatsAppBusinessService:
async def _get_whatsapp_credentials(self, tenant_id: str) -> Dict[str, str]:
"""
Get WhatsApp credentials for a tenant
Get WhatsApp credentials for a tenant (Shared Account Model)
Tries tenant-specific settings first, falls back to global config
Uses global master account credentials with tenant-specific phone number
Args:
tenant_id: Tenant ID
@@ -69,48 +69,53 @@ class WhatsAppBusinessService:
Returns:
Dictionary with access_token, phone_number_id, business_account_id
"""
# Try to fetch tenant-specific settings
# Always use global master account credentials
access_token = self.global_access_token
business_account_id = self.global_business_account_id
phone_number_id = self.global_phone_number_id # Default fallback
# Try to fetch tenant-specific phone number
if self.tenant_client:
try:
notification_settings = await self.tenant_client.get_notification_settings(tenant_id)
if notification_settings and notification_settings.get('whatsapp_enabled'):
tenant_access_token = notification_settings.get('whatsapp_access_token', '').strip()
tenant_phone_id = notification_settings.get('whatsapp_phone_number_id', '').strip()
tenant_business_id = notification_settings.get('whatsapp_business_account_id', '').strip()
# Use tenant credentials if all are configured
if tenant_access_token and tenant_phone_id:
# Use tenant's assigned phone number if configured
if tenant_phone_id:
phone_number_id = tenant_phone_id
logger.info(
"Using tenant-specific WhatsApp credentials",
tenant_id=tenant_id
"Using tenant-assigned WhatsApp phone number with shared account",
tenant_id=tenant_id,
phone_number_id=phone_number_id
)
return {
'access_token': tenant_access_token,
'phone_number_id': tenant_phone_id,
'business_account_id': tenant_business_id
}
else:
logger.info(
"Tenant WhatsApp enabled but credentials incomplete, falling back to global",
"Tenant WhatsApp enabled but no phone number assigned, using default",
tenant_id=tenant_id
)
else:
logger.info(
"Tenant WhatsApp not enabled, using default phone number",
tenant_id=tenant_id
)
except Exception as e:
logger.warning(
"Failed to fetch tenant notification settings, using global config",
"Failed to fetch tenant notification settings, using default phone number",
error=str(e),
tenant_id=tenant_id
)
# Fallback to global configuration
logger.info(
"Using global WhatsApp credentials",
tenant_id=tenant_id
"Using shared WhatsApp account",
tenant_id=tenant_id,
phone_number_id=phone_number_id
)
return {
'access_token': self.global_access_token,
'phone_number_id': self.global_phone_number_id,
'business_account_id': self.global_business_account_id
'access_token': access_token,
'phone_number_id': phone_number_id,
'business_account_id': business_account_id
}
async def send_message(