46 lines
1.4 KiB
Python
46 lines
1.4 KiB
Python
"""Mock WhatsApp endpoint for testing command parsing."""
|
|
|
|
from __future__ import annotations
|
|
|
|
from datetime import datetime, timezone
|
|
from typing import Annotated
|
|
|
|
from fastapi import APIRouter, Depends
|
|
from sqlalchemy import select
|
|
from sqlalchemy.ext.asyncio import AsyncSession
|
|
|
|
from app.core.database import get_db
|
|
from app.models.whatsapp_log import WhatsAppLog
|
|
from app.schemas.whatsapp import MockWhatsAppLogEntry, MockWhatsAppRequest, MockWhatsAppResponse
|
|
|
|
router = APIRouter(prefix="/api/whatsapp", tags=["whatsapp"])
|
|
|
|
|
|
@router.post("/mock", response_model=MockWhatsAppResponse)
|
|
async def mock_whatsapp(
|
|
body: MockWhatsAppRequest,
|
|
db: Annotated[AsyncSession, Depends(get_db)],
|
|
) -> MockWhatsAppResponse:
|
|
"""Accept a mock WhatsApp command and log it."""
|
|
log = WhatsAppLog(
|
|
command=body.command,
|
|
from_number=body.from_number,
|
|
ticket_id=body.ticket_id,
|
|
received_at=datetime.now(timezone.utc),
|
|
)
|
|
db.add(log)
|
|
await db.flush()
|
|
return MockWhatsAppResponse()
|
|
|
|
|
|
@router.get("/mock-log", response_model=list[MockWhatsAppLogEntry])
|
|
async def mock_whatsapp_log(
|
|
db: Annotated[AsyncSession, Depends(get_db)],
|
|
limit: int = 50,
|
|
) -> list[MockWhatsAppLogEntry]:
|
|
"""Return recent mock WhatsApp submissions."""
|
|
result = await db.execute(
|
|
select(WhatsAppLog).order_by(WhatsAppLog.received_at.desc()).limit(limit)
|
|
)
|
|
return list(result.scalars().all())
|