This commit is contained in:
debu-2 2026-10-14 06:04:27 +05:30
parent 84544e1153
commit a109df2deb
7 changed files with 51 additions and 11 deletions

View file

@ -20,6 +20,7 @@ from .whatsapp_onboarding import (
_load_phones,
_load_waba,
_owned_account,
meta_calls_allowed,
_quality_label,
_token_app_id,
_waba_ids_from_debug,
@ -613,6 +614,8 @@ async def _correct_waba_id(db: Session, account: WhatsAppBusinessAccount, token:
async def sync_account_templates(db: Session, account: WhatsAppBusinessAccount) -> str | None:
if not meta_calls_allowed(account):
return None
tokens = _candidate_tokens(db, account)
if not tokens:
if account.status == "disconnected":
@ -671,6 +674,7 @@ async def list_managed_templates(db: Session, user: UserAccount, account_id: str
accounts = db.scalars(query).all()
if account_id:
accounts = [account for account in accounts if str(account.id) == account_id]
accounts = [account for account in accounts if meta_calls_allowed(account)]
warnings = []
for account in accounts:
warning = await sync_account_templates(db, account)
@ -746,8 +750,14 @@ def _validate_identity(payload: dict[str, Any]) -> tuple[str, str, str]:
return name, language, category
def _require_connected(account: WhatsAppBusinessAccount) -> None:
if not meta_calls_allowed(account):
raise HTTPException(status_code=status.HTTP_409_CONFLICT, detail="Reconnect this WhatsApp account before managing templates.")
async def create_managed_template(db: Session, user: UserAccount, payload: dict[str, Any]) -> dict[str, Any]:
account = _owned_account(db, user, payload.get("account_id"))
_require_connected(account)
token = _token(db, account)
name, language, category = _validate_identity(payload)
existing = db.scalar(
@ -793,6 +803,7 @@ async def create_managed_template(db: Session, user: UserAccount, payload: dict[
async def resubmit_managed_template(db: Session, user: UserAccount, template_id: Any, payload: dict[str, Any]) -> dict[str, Any]:
row, account = _owned_template(db, user, template_id)
_require_connected(account)
if row.status not in {"REJECTED", "PAUSED", "APPROVED"}:
raise HTTPException(status_code=status.HTTP_409_CONFLICT, detail="This template can be edited after Meta rejects it, pauses it, or approves it.")
if not row.meta_template_id:
@ -812,6 +823,7 @@ async def resubmit_managed_template(db: Session, user: UserAccount, template_id:
async def delete_managed_template(db: Session, user: UserAccount, template_id: Any) -> dict[str, str]:
row, account = _owned_template(db, user, template_id)
_require_connected(account)
params: dict[str, Any] = {"name": row.name}
if row.meta_template_id:
params["hsm_id"] = row.meta_template_id
@ -823,6 +835,7 @@ async def delete_managed_template(db: Session, user: UserAccount, template_id: A
async def upload_header_media(db: Session, user: UserAccount, account_id: Any, filename: str, content_type: str, data: bytes) -> dict[str, str]:
account = _owned_account(db, user, account_id)
_require_connected(account)
token = _token(db, account)
kind = _MEDIA.get(content_type)
if kind is None: