template
This commit is contained in:
parent
84544e1153
commit
a109df2deb
7 changed files with 51 additions and 11 deletions
|
|
@ -227,6 +227,11 @@ def _serialize_phone(phone: WhatsAppPhoneAsset) -> dict[str, Any]:
|
|||
}
|
||||
|
||||
|
||||
def meta_calls_allowed(account: WhatsAppBusinessAccount) -> bool:
|
||||
"""A disconnected account keeps its saved records and is not sent to Meta."""
|
||||
return account.status != "disconnected"
|
||||
|
||||
|
||||
def serialize_account(account: WhatsAppBusinessAccount) -> dict[str, Any]:
|
||||
phones = [_serialize_phone(phone) for phone in account.phones]
|
||||
primary = next((phone for phone in phones if phone["status"] == "connected"), phones[0] if phones else None)
|
||||
|
|
@ -906,6 +911,8 @@ async def connect_with_token(
|
|||
|
||||
async def refresh_account(db: Session, user: UserAccount, account_id: Any) -> dict[str, Any]:
|
||||
account = _owned_account(db, user, account_id)
|
||||
if not meta_calls_allowed(account):
|
||||
return serialize_account(account)
|
||||
connection = account.connection
|
||||
token = connection.access_token if connection else None
|
||||
if connection is None or not token or connection.status == "disconnected":
|
||||
|
|
@ -933,6 +940,8 @@ async def register_phone_number(
|
|||
pin: str | None,
|
||||
) -> dict[str, Any]:
|
||||
account = _owned_account(db, user, account_id)
|
||||
if not meta_calls_allowed(account):
|
||||
raise HTTPException(status_code=status.HTTP_409_CONFLICT, detail="Reconnect this WhatsApp account before registering the number")
|
||||
connection = account.connection
|
||||
token = connection.access_token if connection else None
|
||||
if connection is None or not token or connection.status == "disconnected":
|
||||
|
|
@ -970,6 +979,8 @@ async def register_phone_number(
|
|||
|
||||
async def disconnect_account(db: Session, user: UserAccount, account_id: Any) -> dict[str, Any]:
|
||||
account = _owned_account(db, user, account_id)
|
||||
if not meta_calls_allowed(account):
|
||||
return serialize_account(account)
|
||||
connection = account.connection
|
||||
token = connection.access_token if connection else None
|
||||
if token:
|
||||
|
|
@ -1012,7 +1023,7 @@ def account_detail(db: Session, user: UserAccount, account_id: Any) -> dict[str,
|
|||
|
||||
async def list_templates(db: Session, user: UserAccount, account_id: Any) -> dict[str, Any]:
|
||||
account = _owned_account(db, user, account_id)
|
||||
if account.templates_status != "active" or account.connection is None or not account.connection.access_token:
|
||||
if not meta_calls_allowed(account) or account.templates_status != "active" or account.connection is None or not account.connection.access_token:
|
||||
return {"available": False, "templates": []}
|
||||
data = await _graph(
|
||||
"GET",
|
||||
|
|
@ -1054,6 +1065,8 @@ async def create_marketing_template(
|
|||
) -> dict[str, Any]:
|
||||
"""Submit a marketing template to Meta for the connected WhatsApp account."""
|
||||
account = _owned_account(db, user, account_id)
|
||||
if not meta_calls_allowed(account):
|
||||
raise HTTPException(status_code=status.HTTP_409_CONFLICT, detail="Reconnect this WhatsApp account before creating a template.")
|
||||
connection = account.connection
|
||||
token = connection.access_token if connection else None
|
||||
if connection is None or not token or connection.status == "disconnected":
|
||||
|
|
@ -1187,7 +1200,7 @@ def _messaging_phone(account: WhatsAppBusinessAccount) -> tuple[WhatsAppPhoneAss
|
|||
connection = account.connection
|
||||
token = connection.access_token if connection else None
|
||||
phone = next((item for item in account.phones if item.status == "connected"), None)
|
||||
if phone is None or account.messaging_status != "active" or not token:
|
||||
if not meta_calls_allowed(account) or phone is None or account.messaging_status != "active" or not token:
|
||||
raise HTTPException(
|
||||
status_code=status.HTTP_409_CONFLICT,
|
||||
detail=account.last_error or "Messaging is unavailable until this number is connected on Cloud API",
|
||||
|
|
@ -1198,6 +1211,8 @@ def _messaging_phone(account: WhatsAppBusinessAccount) -> tuple[WhatsAppPhoneAss
|
|||
async def enable_customer_delivery(db: Session, user: UserAccount, account_id: Any, app_secret: str) -> dict[str, Any]:
|
||||
"""Point this account's replies and delivery updates at the workspace."""
|
||||
account = _owned_account(db, user, account_id)
|
||||
if not meta_calls_allowed(account):
|
||||
raise HTTPException(status_code=status.HTTP_409_CONFLICT, detail="Reconnect this WhatsApp account before replies can arrive here.")
|
||||
connection = account.connection
|
||||
token = connection.access_token if connection else None
|
||||
if connection is None or not token or connection.status == "disconnected":
|
||||
|
|
@ -1383,6 +1398,8 @@ async def send_account_broadcast(
|
|||
|
||||
async def download_account_media(db: Session, user: UserAccount, account_id: Any, media_id: str) -> tuple[bytes, str]:
|
||||
account = _owned_account(db, user, account_id)
|
||||
if not meta_calls_allowed(account):
|
||||
raise HTTPException(status_code=status.HTTP_409_CONFLICT, detail="Reconnect this WhatsApp account before opening media")
|
||||
token = account.connection.access_token if account.connection else None
|
||||
if not token:
|
||||
raise HTTPException(status_code=status.HTTP_409_CONFLICT, detail="Reconnect this WhatsApp account before opening media")
|
||||
|
|
|
|||
Loading…
Add table
Add a link
Reference in a new issue