feat: add cloudonix cdr

This commit is contained in:
Abhishek Kumar 2026-01-29 17:45:46 +05:30
parent 91911769b0
commit 07558ec785
6 changed files with 211 additions and 10 deletions

View file

@ -372,6 +372,11 @@ class WorkflowRunModel(Base):
unique=True,
postgresql_where=text("public_access_token IS NOT NULL"),
),
Index(
"idx_workflow_runs_call_id",
text("(gathered_context->>'call_id')"),
postgresql_where=text("gathered_context->>'call_id' IS NOT NULL"),
),
)

View file

@ -4,6 +4,7 @@ from sqlalchemy.future import select
from api.db.base_client import BaseDBClient
from api.db.models import OrganizationConfigurationModel
from api.enums import OrganizationConfigurationKey
class OrganizationConfigurationClient(BaseDBClient):
@ -94,3 +95,29 @@ class OrganizationConfigurationClient(BaseDBClient):
"""Get the value of a configuration, returning default if not found."""
config = await self.get_configuration(organization_id, key)
return config.value if config else default
async def get_organization_id_by_telephony_domain(
self, domain: str
) -> Optional[int]:
"""Find organization ID by domain_id in telephony configuration.
Args:
domain: The telephony domain to search for (e.g., Cloudonix domain_id)
Returns:
Organization ID if found, None otherwise
"""
async with self.async_session() as session:
result = await session.execute(
select(OrganizationConfigurationModel).where(
OrganizationConfigurationModel.key
== OrganizationConfigurationKey.TELEPHONY_CONFIGURATION.value,
)
)
configs = result.scalars().all()
for config in configs:
if config.value and config.value.get("domain_id") == domain:
return config.organization_id
return None

View file

@ -468,3 +468,30 @@ class WorkflowRunClient(BaseDBClient):
)
)
return result.scalars().first()
async def get_workflow_run_by_call_id(
self, call_id: str
) -> Optional[WorkflowRunModel]:
"""Find workflow run by call_id stored in gathered_context.
Args:
call_id: The telephony call ID to search for
Returns:
The WorkflowRunModel if found, None otherwise
"""
async with self.async_session() as session:
# Use JSON text extraction to find matching call_id
# This leverages the idx_workflow_runs_call_id index
result = await session.execute(
select(WorkflowRunModel)
.options(
joinedload(WorkflowRunModel.workflow).joinedload(WorkflowModel.user)
)
.where(
WorkflowRunModel.gathered_context.op("->>")("call_id") == call_id
)
.order_by(WorkflowRunModel.created_at.desc())
.limit(1)
)
return result.scalars().first()