mirror of
http://88.130.71.182:3000/BlitTech/contexta_be.git
synced 2026-06-12 23:23:21 +00:00
feat: add appointments, campaigns, admin, storage, tests and various updates
- Add new routers: admin, appointments, campaigns - Add storage service and logging config - Add migrations directory and test suite with pytest config - Add supabase_migration_features.sql - Update models, dependencies, config, and existing routers - Remove whatsapp_service (deleted) - Update pyproject.toml and uv.lock dependencies Co-Authored-By: Claude Sonnet 4.6 <noreply@anthropic.com>
This commit is contained in:
@@ -2,8 +2,9 @@ from fastapi import APIRouter, HTTPException, Depends, Query
|
||||
from app.database import get_supabase
|
||||
from app.dependencies import get_current_user
|
||||
from app.config import PLAN_LIMITS
|
||||
from app.models import InboxConversation, InboxMessage
|
||||
from app.models import InboxConversation, InboxMessage, ConversationStatusUpdate, AgentReplyCreate
|
||||
from typing import List, Optional
|
||||
import uuid
|
||||
import logging
|
||||
|
||||
logger = logging.getLogger(__name__)
|
||||
@@ -79,6 +80,8 @@ async def list_inbox_conversations(
|
||||
language=conv.get("language", "en"),
|
||||
message_count=conv.get("message_count", 0),
|
||||
first_message=first_message_text,
|
||||
status=conv.get("status", "open"),
|
||||
last_agent_reply_at=conv.get("last_agent_reply_at"),
|
||||
created_at=conv.get("created_at"),
|
||||
))
|
||||
|
||||
@@ -137,6 +140,67 @@ async def get_inbox_conversation(
|
||||
}
|
||||
|
||||
|
||||
@router.patch("/conversations/{conversation_id}/status")
|
||||
async def update_conversation_status(
|
||||
conversation_id: str,
|
||||
data: ConversationStatusUpdate,
|
||||
user=Depends(get_current_user),
|
||||
):
|
||||
"""Update conversation status (open, agent_handling, resolved)."""
|
||||
if data.status not in ("open", "agent_handling", "resolved"):
|
||||
raise HTTPException(status_code=400, detail="Invalid status")
|
||||
|
||||
supabase = get_supabase()
|
||||
_check_inbox_access(user.id, supabase)
|
||||
company_id = _get_user_company_id(user.id, supabase)
|
||||
|
||||
conv = supabase.table("conversations").select("*, chatbots(company_id)") \
|
||||
.eq("id", conversation_id).execute()
|
||||
if not conv.data:
|
||||
raise HTTPException(status_code=404, detail="Conversation not found")
|
||||
if conv.data[0].get("chatbots", {}).get("company_id") != company_id:
|
||||
raise HTTPException(status_code=403, detail="Not authorized")
|
||||
|
||||
supabase.table("conversations").update({"status": data.status}).eq("id", conversation_id).execute()
|
||||
return {"success": True, "status": data.status}
|
||||
|
||||
|
||||
@router.post("/conversations/{conversation_id}/reply")
|
||||
async def agent_reply(
|
||||
conversation_id: str,
|
||||
data: AgentReplyCreate,
|
||||
user=Depends(get_current_user),
|
||||
):
|
||||
"""Send an agent reply to a conversation."""
|
||||
supabase = get_supabase()
|
||||
_check_inbox_access(user.id, supabase)
|
||||
company_id = _get_user_company_id(user.id, supabase)
|
||||
|
||||
conv = supabase.table("conversations").select("*, chatbots(company_id)") \
|
||||
.eq("id", conversation_id).execute()
|
||||
if not conv.data:
|
||||
raise HTTPException(status_code=404, detail="Conversation not found")
|
||||
if conv.data[0].get("chatbots", {}).get("company_id") != company_id:
|
||||
raise HTTPException(status_code=403, detail="Not authorized")
|
||||
|
||||
msg_id = str(uuid.uuid4())
|
||||
supabase.table("messages").insert({
|
||||
"id": msg_id,
|
||||
"conversation_id": conversation_id,
|
||||
"role": "agent",
|
||||
"content": data.message,
|
||||
}).execute()
|
||||
|
||||
# Mark as agent_handling if not already, and record reply time
|
||||
current_status = conv.data[0].get("status", "open")
|
||||
update_data: dict = {"last_agent_reply_at": "now()"}
|
||||
if current_status == "open":
|
||||
update_data["status"] = "agent_handling"
|
||||
supabase.table("conversations").update(update_data).eq("id", conversation_id).execute()
|
||||
|
||||
return {"success": True, "message_id": msg_id}
|
||||
|
||||
|
||||
@router.delete("/conversations/{conversation_id}")
|
||||
async def delete_inbox_conversation(
|
||||
conversation_id: str,
|
||||
|
||||
Reference in New Issue
Block a user