This commit is contained in:
debu-2 2026-10-14 05:46:18 +05:30
parent dcae960928
commit a7e6ed2e4b
3 changed files with 107 additions and 14 deletions

View file

@ -169,24 +169,31 @@ async def _debug_token(db: Session, token: str) -> dict[str, Any]:
return payload if isinstance(payload, dict) else {}
def _waba_ids_from_debug(debug: dict[str, Any]) -> list[str]:
def _target_ids(debug: dict[str, Any], scope: str) -> list[str]:
found: list[str] = []
scopes = debug.get("granular_scopes")
if not isinstance(scopes, list):
return found
preferred = {"whatsapp_business_management", "whatsapp_business_messaging"}
for item in scopes:
if not isinstance(item, dict):
continue
if item.get("scope") not in preferred:
if not isinstance(item, dict) or item.get("scope") != scope:
continue
for target in item.get("target_ids") or []:
value = str(target)
value = str(target).strip()
if value and value not in found:
found.append(value)
return found
def _waba_ids_from_debug(debug: dict[str, Any]) -> list[str]:
"""whatsapp_business_management targets are WABA IDs."""
return _target_ids(debug, "whatsapp_business_management")
def _phone_ids_from_debug(debug: dict[str, Any]) -> list[str]:
"""whatsapp_business_messaging targets are phone number IDs, not WABA IDs."""
return _target_ids(debug, "whatsapp_business_messaging")
def _as_uuid(account_id: Any) -> uuid.UUID:
try:
return account_id if isinstance(account_id, uuid.UUID) else uuid.UUID(str(account_id))
@ -599,7 +606,15 @@ async def complete_signup(db: Session, user: UserAccount, payload: dict[str, Any
}
session_waba = str(payload.get("waba_id") or "").strip()
waba_ids = [session_waba] if session_waba else _waba_ids_from_debug(debug)
session_phone = str(payload.get("phone_number_id") or "").strip() or None
management_ids = _waba_ids_from_debug(debug)
phone_ids = _phone_ids_from_debug(debug)
if session_waba and session_waba in phone_ids and session_waba not in management_ids:
session_phone = session_phone or session_waba
session_waba = ""
waba_ids = [session_waba] if session_waba else management_ids
if not session_phone and len(phone_ids) == 1:
session_phone = phone_ids[0]
if not waba_ids:
_audit(db, user, "provision_failed", "Embedded Signup did not return a WhatsApp Business Account")
db.commit()
@ -622,7 +637,6 @@ async def complete_signup(db: Session, user: UserAccount, payload: dict[str, Any
db.flush()
_audit(db, user, "authorization_received", "Embedded Signup returned an authorization code", connection_id=connection.id)
session_phone = str(payload.get("phone_number_id") or "").strip() or None
accounts: list[WhatsAppBusinessAccount] = []
for waba_id in waba_ids:
accounts.append(await _provision_waba(db, user, connection, token, waba_id, session_phone))