File size: 3,001 Bytes
4cf98c9
 
 
0db2958
4cf98c9
 
 
 
 
 
75c718c
 
0db2958
 
 
 
 
75c718c
 
 
 
 
4cf98c9
 
75c718c
 
4cf98c9
 
 
 
 
 
 
 
 
 
 
 
 
75c718c
 
4cf98c9
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
0db2958
 
 
75c718c
 
4cf98c9
 
 
 
 
 
 
 
 
 
 
 
 
 
0db2958
 
 
75c718c
 
4cf98c9
 
 
 
 
 
 
 
 
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
import os
import asyncio
import asyncpg
import re
from dotenv import load_dotenv

load_dotenv()

DATABASE_URL = os.getenv("DATABASE_URL")

_pool = None

_UUID_RE = re.compile(r'^[0-9a-f]{8}-[0-9a-f]{4}-[0-9a-f]{4}-[0-9a-f]{4}-[0-9a-f]{12}$', re.IGNORECASE)

def _is_valid_uuid(val: str) -> bool:
    return bool(_UUID_RE.match(str(val) if val else ''))

async def get_db_pool():
    global _pool
    if _pool is None:
        _pool = await asyncpg.create_pool(DATABASE_URL)
    return _pool

async def get_tenant_config(tenant_id: str):
    pool = await get_db_pool()
    async with pool.acquire() as conn:
        row = await conn.fetchrow(
            """
            SELECT t.*, ac.system_prompt_gu, ac.system_prompt_hi, ac.system_prompt_en, 
                   ac.greeting_gu, ac.greeting_hi, ac.greeting_en
            FROM tenants t
            JOIN agent_configs ac ON t.id = ac.tenant_id
            WHERE t.id = $1
            """,
            tenant_id
        )
        return dict(row) if row else None

async def save_call(call_data: dict):
    pool = await get_db_pool()
    async with pool.acquire() as conn:
        await conn.execute(
            """
            INSERT INTO calls (
                tenant_id, caller_number, duration_seconds, language_used, 
                mode_used, was_intercepted, transcript, summary, status, call_sid
            ) VALUES ($1, $2, $3, $4, $5, $6, $7, $8, $9, $10)
            """,
            call_data.get("tenant_id"),
            call_data.get("caller_number"),
            call_data.get("duration_seconds"),
            call_data.get("language_used"),
            call_data.get("mode_used"),
            call_data.get("was_intercepted"),
            call_data.get("transcript"),
            call_data.get("summary"),
            call_data.get("status"),
            call_data.get("call_sid")
        )

async def save_lead(lead_data: dict):
    # Skip if tenant_id is not a valid UUID (e.g. demo-tenant-123)
    if not _is_valid_uuid(lead_data.get("tenant_id")):
        return
    pool = await get_db_pool()
    async with pool.acquire() as conn:
        await conn.execute(
            """
            INSERT INTO leads (tenant_id, call_id, name, phone, note, status)
            VALUES ($1, $2, $3, $4, $5, $6)
            """,
            lead_data.get("tenant_id"),
            lead_data.get("call_id"),
            lead_data.get("name"),
            lead_data.get("phone"),
            lead_data.get("note"),
            lead_data.get("status", "new")
        )

async def get_leads(tenant_id: str):
    # Skip if tenant_id is not a valid UUID (e.g. demo-tenant-123)
    if not _is_valid_uuid(tenant_id):
        return []
    pool = await get_db_pool()
    async with pool.acquire() as conn:
        rows = await conn.fetch(
            """
            SELECT * FROM leads 
            WHERE tenant_id = $1 
            ORDER BY created_at DESC
            """,
            tenant_id
        )
        return [dict(r) for r in rows]