Watch
1
0
Fork
You've already forked Seyyed_arc
0
forked from hesabix/arc
Seyyed_arc/hesabixAPI/app/services/telephony/telephony_service.py
Babak Alizadeh 135151de64 fix(telephony): repair screen-pop customer, lead, and contact actions
Open the real person dialog with the caller mobile, fix customer-360 links, and keep created leads on the call so popup buttons stop hitting 404 or saving the extension as a contact.
2026-11-15 14:58:12 +03:30

2324 lines
74 KiB
Python
Raw Permalink Blame History

This file contains ambiguous Unicode characters

This file contains Unicode characters that might be confused with other characters. If you think that this is intentional, you can safely ignore this warning. Use the Escape button to reveal them.

"""سرویس‌های اصلی افزونه تلفن: تنظیمات، PBX، داخلی، تماس، Click-to-Call."""
from __future__ import annotations
import secrets
import uuid
import json
from datetime import datetime, timedelta
from typing import Any, Dict, List, Optional, Set, Tuple
from sqlalchemy import and_, func, or_
from sqlalchemy.exc import IntegrityError
from sqlalchemy.orm import Session, joinedload
from adapters.db.models.crm import CrmActivity, Lead
from adapters.db.models.person import Person
from adapters.db.models.telephony import (
TelephonyCall,
TelephonyCallEvent,
TelephonyCommand,
TelephonyExtension,
TelephonyNumberAlias,
TelephonyPbxConnection,
TelephonySettings,
TelephonyTeamSupervisor,
TelephonyUserExtension,
)
from adapters.db.models.user import User
from app.core.responses import ApiError
from app.core.security import generate_api_key, hash_api_key
from app.core.telephony_plugin_dependency import check_telephony_plugin_active
from app.services.telephony.phone_normalizer import extract_digits, identity_candidates, normalize_iran_phone, numbers_match
from app.services.telephony.telephony_access import (
TelephonyScope,
exclusive_extension_taken,
list_assignable_users,
require_business_member,
require_call_in_scope,
resolve_scope,
upsert_team_supervisor,
user_display_name,
)
from app.services.realtime import realtime_manager
DEFAULT_PHONE_FIELDS = ["mobile", "mobile_2", "mobile_3", "phone"]
_NORMALIZED_PERSON_ATTR = {
"mobile": "mobile_normalized",
"mobile_2": "mobile_2_normalized",
"mobile_3": "mobile_3_normalized",
"phone": "phone_normalized",
}
def _iso(dt: Optional[datetime]) -> Optional[str]:
return dt.isoformat() if dt else None
def _now() -> datetime:
return datetime.utcnow()
# ─── Settings ───────────────────────────────────────────────────────────────
def get_or_create_settings(db: Session, business_id: int) -> TelephonySettings:
row = db.query(TelephonySettings).filter(TelephonySettings.business_id == business_id).first()
if row:
return row
row = TelephonySettings(
business_id=business_id,
primary_phone_fields=list(DEFAULT_PHONE_FIELDS),
created_at=_now(),
updated_at=_now(),
)
db.add(row)
db.commit()
db.refresh(row)
return row
def settings_to_dict(row: TelephonySettings) -> Dict[str, Any]:
return {
"id": row.id,
"business_id": row.business_id,
"primary_phone_fields": row.primary_phone_fields or list(DEFAULT_PHONE_FIELDS),
"number_normalization_rules": row.number_normalization_rules or {},
"screen_pop_enabled": bool(row.screen_pop_enabled),
"auto_create_activity": bool(row.auto_create_activity),
"post_call_form_required": bool(row.post_call_form_required),
"default_category": row.default_category,
"missed_call_create_task": bool(row.missed_call_create_task),
"missed_call_task_due_minutes": row.missed_call_task_due_minutes,
"recording_access_mode": row.recording_access_mode or "permission_based",
"multi_pbx_enabled": bool(row.multi_pbx_enabled),
"ui_preferences": row.ui_preferences or {},
"extra_settings": row.extra_settings or {},
"updated_at": _iso(row.updated_at),
}
def update_settings(db: Session, business_id: int, payload: Dict[str, Any]) -> Dict[str, Any]:
row = get_or_create_settings(db, business_id)
for key in (
"primary_phone_fields",
"number_normalization_rules",
"screen_pop_enabled",
"auto_create_activity",
"post_call_form_required",
"default_category",
"missed_call_create_task",
"missed_call_task_due_minutes",
"recording_access_mode",
"multi_pbx_enabled",
"ui_preferences",
"extra_settings",
):
if key in payload:
if key == "recording_access_mode":
mode = str(payload[key] or "permission_based").strip()
if mode not in ("permission_based", "all_operators", "owner_and_managers"):
raise ApiError(
"INVALID_RECORDING_ACCESS_MODE",
"INVALID_RECORDING_ACCESS_MODE",
http_status=400,
)
setattr(row, key, mode)
else:
setattr(row, key, payload[key])
row.updated_at = _now()
db.commit()
db.refresh(row)
return settings_to_dict(row)
# ─── PBX ────────────────────────────────────────────────────────────────────
def pbx_to_dict(row: TelephonyPbxConnection, *, include_token_hint: bool = True) -> Dict[str, Any]:
offline = False
if row.last_seen_at:
offline = (_now() - row.last_seen_at) > timedelta(minutes=3)
status = row.status
if status == "online" and offline:
status = "offline"
return {
"id": row.id,
"business_id": row.business_id,
"name": row.name,
"pbx_type": row.pbx_type,
"host_hint": row.host_hint,
"connector_token_prefix": row.connector_token_prefix if include_token_hint else None,
"connector_version": row.connector_version,
"last_seen_at": _iso(row.last_seen_at),
"status": status,
"last_error": row.last_error,
"settings": row.settings or {},
"is_active": bool(row.is_active),
"created_at": _iso(row.created_at),
"updated_at": _iso(row.updated_at),
}
def list_pbx(db: Session, business_id: int, *, include_token_hint: bool = True) -> List[Dict[str, Any]]:
rows = (
db.query(TelephonyPbxConnection)
.filter(TelephonyPbxConnection.business_id == business_id)
.order_by(TelephonyPbxConnection.id.asc())
.all()
)
return [pbx_to_dict(r, include_token_hint=include_token_hint) for r in rows]
def create_pbx(db: Session, business_id: int, payload: Dict[str, Any]) -> Dict[str, Any]:
name = (payload.get("name") or "").strip()
if not name:
raise ApiError("PBX_NAME_REQUIRED", "PBX_NAME_REQUIRED", http_status=400)
settings_row = get_or_create_settings(db, business_id)
existing_count = (
db.query(func.count(TelephonyPbxConnection.id))
.filter(TelephonyPbxConnection.business_id == business_id)
.scalar()
or 0
)
if existing_count and not settings_row.multi_pbx_enabled:
raise ApiError(
"MULTI_PBX_DISABLED",
"MULTI_PBX_DISABLED",
http_status=400,
)
api_key, key_hash = generate_api_key(prefix="hsx_tel_", length=36)
row = TelephonyPbxConnection(
business_id=business_id,
name=name,
pbx_type=(payload.get("pbx_type") or "issabel").strip(),
host_hint=(payload.get("host_hint") or None),
connector_token_hash=key_hash,
connector_token_prefix=api_key[:12],
settings=payload.get("settings") or {
"originate_context": "from-internal",
"originate_timeout_ms": 30000,
"softphone": {
"relay_enabled": True,
"direct_enabled": False,
"media_codec_prefs": ["pcm_ws_v1"],
"max_concurrent_softphone_sessions": 50,
"audiosocket_host": "127.0.0.1",
"audiosocket_port": 9092,
},
},
status="pending",
is_active=True,
created_at=_now(),
updated_at=_now(),
)
db.add(row)
db.commit()
db.refresh(row)
data = pbx_to_dict(row)
data["connector_token"] = api_key # فقط یک‌بار
data["install_hint"] = (
"توکن را فقط یک‌بار کپی کنید و در فایل تنظیمات Connector روی سرور Issabel قرار دهید."
)
return data
def rotate_pbx_token(db: Session, business_id: int, pbx_id: int) -> Dict[str, Any]:
row = (
db.query(TelephonyPbxConnection)
.filter(TelephonyPbxConnection.id == pbx_id, TelephonyPbxConnection.business_id == business_id)
.first()
)
if not row:
raise ApiError("PBX_NOT_FOUND", "PBX_NOT_FOUND", http_status=404)
api_key, key_hash = generate_api_key(prefix="hsx_tel_", length=36)
row.connector_token_hash = key_hash
row.connector_token_prefix = api_key[:12]
row.status = "pending"
row.updated_at = _now()
db.commit()
db.refresh(row)
data = pbx_to_dict(row)
data["connector_token"] = api_key
return data
def update_pbx(db: Session, business_id: int, pbx_id: int, payload: Dict[str, Any]) -> Dict[str, Any]:
row = (
db.query(TelephonyPbxConnection)
.filter(TelephonyPbxConnection.id == pbx_id, TelephonyPbxConnection.business_id == business_id)
.first()
)
if not row:
raise ApiError("PBX_NOT_FOUND", "PBX_NOT_FOUND", http_status=404)
for key in ("name", "pbx_type", "host_hint", "settings", "is_active"):
if key in payload:
setattr(row, key, payload[key])
row.updated_at = _now()
db.commit()
db.refresh(row)
return pbx_to_dict(row)
def authenticate_connector(
db: Session, *, token: str, business_id: int, pbx_id: int
) -> TelephonyPbxConnection:
if not token or not check_telephony_plugin_active(db, business_id):
raise ApiError("CONNECTOR_AUTH_FAILED", "CONNECTOR_AUTH_FAILED", http_status=401)
token_hash = hash_api_key(token)
row = (
db.query(TelephonyPbxConnection)
.filter(
TelephonyPbxConnection.id == pbx_id,
TelephonyPbxConnection.business_id == business_id,
TelephonyPbxConnection.connector_token_hash == token_hash,
TelephonyPbxConnection.is_active == True, # noqa: E712
)
.first()
)
if not row:
raise ApiError("INVALID_CONNECTOR_TOKEN", "INVALID_CONNECTOR_TOKEN", http_status=401)
_rate_limit_connector(int(pbx_id))
return row
_CONNECTOR_HITS: Dict[int, List[float]] = {}
def _rate_limit_connector(pbx_id: int, *, max_per_sec: int = 50) -> None:
import time
now = time.time()
window = _CONNECTOR_HITS.setdefault(pbx_id, [])
window[:] = [t for t in window if now - t < 1.0]
if len(window) >= max_per_sec:
raise ApiError("RATE_LIMITED", "RATE_LIMITED", http_status=429)
window.append(now)
def connector_heartbeat(
db: Session,
pbx: TelephonyPbxConnection,
payload: Dict[str, Any],
) -> Dict[str, Any]:
pbx.last_seen_at = _now()
pbx.connector_version = payload.get("connector_version") or pbx.connector_version
pbx.status = "online" if payload.get("status", "ok") == "ok" else "error"
if payload.get("status") != "ok":
pbx.last_error = str(payload.get("error") or "connector error")
else:
pbx.last_error = None
pbx.updated_at = _now()
ext_payload = payload.get("extensions")
if isinstance(ext_payload, list) and ext_payload:
sync_extensions(db, pbx.business_id, pbx.id, ext_payload, commit=False)
db.commit()
return {"ok": True, "pbx_id": pbx.id, "status": pbx.status}
# ─── Extensions & user mapping ──────────────────────────────────────────────
def extension_to_dict(row: TelephonyExtension) -> Dict[str, Any]:
return {
"id": row.id,
"business_id": row.business_id,
"pbx_id": row.pbx_id,
"extension": row.extension,
"display_name": row.display_name,
"team_code": getattr(row, "team_code", None),
"allow_shared": bool(getattr(row, "allow_shared", False)),
"queue_codes": row.queue_codes,
"is_active": bool(row.is_active),
"presence_status": row.presence_status,
"presence_updated_at": _iso(row.presence_updated_at),
}
def list_extensions(
db: Session,
business_id: int,
pbx_id: Optional[int] = None,
*,
scope: Optional[TelephonyScope] = None,
) -> List[Dict[str, Any]]:
q = db.query(TelephonyExtension).filter(TelephonyExtension.business_id == business_id)
if pbx_id:
q = q.filter(TelephonyExtension.pbx_id == pbx_id)
if scope is not None and not scope.view_all:
if scope.extension_ids:
q = q.filter(TelephonyExtension.id.in_(list(scope.extension_ids)))
else:
return []
rows = q.order_by(TelephonyExtension.extension.asc()).all()
return [extension_to_dict(r) for r in rows]
def upsert_extension(db: Session, business_id: int, payload: Dict[str, Any]) -> Dict[str, Any]:
pbx_id = int(payload.get("pbx_id") or 0)
ext = str(payload.get("extension") or "").strip()
if not pbx_id or not ext:
raise ApiError("PBX_AND_EXTENSION_REQUIRED", "PBX_AND_EXTENSION_REQUIRED", http_status=400)
pbx = (
db.query(TelephonyPbxConnection)
.filter(TelephonyPbxConnection.id == pbx_id, TelephonyPbxConnection.business_id == business_id)
.first()
)
if not pbx:
raise ApiError("PBX_NOT_FOUND", "PBX_NOT_FOUND", http_status=404)
row = (
db.query(TelephonyExtension)
.filter(TelephonyExtension.pbx_id == pbx_id, TelephonyExtension.extension == ext)
.first()
)
if not row:
row = TelephonyExtension(
business_id=business_id,
pbx_id=pbx_id,
extension=ext,
created_at=_now(),
updated_at=_now(),
)
db.add(row)
row.display_name = payload.get("display_name", row.display_name)
if "queue_codes" in payload:
row.queue_codes = payload.get("queue_codes")
if "is_active" in payload:
row.is_active = bool(payload.get("is_active"))
if "team_code" in payload:
code = payload.get("team_code")
row.team_code = str(code).strip() if code else None
if "allow_shared" in payload:
row.allow_shared = bool(payload.get("allow_shared"))
row.updated_at = _now()
db.commit()
db.refresh(row)
return extension_to_dict(row)
def sync_extensions(
db: Session,
business_id: int,
pbx_id: int,
items: List[Any],
*,
commit: bool = True,
) -> Dict[str, Any]:
pbx = (
db.query(TelephonyPbxConnection)
.filter(TelephonyPbxConnection.id == pbx_id, TelephonyPbxConnection.business_id == business_id)
.first()
)
if not pbx:
raise ApiError("PBX_NOT_FOUND", "PBX_NOT_FOUND", http_status=404)
upserted = 0
for raw in items:
if isinstance(raw, str):
payload = {"pbx_id": pbx_id, "extension": raw}
elif isinstance(raw, dict):
payload = dict(raw)
payload["pbx_id"] = pbx_id
else:
continue
ext = str(payload.get("extension") or "").strip()
if not ext:
continue
row = (
db.query(TelephonyExtension)
.filter(TelephonyExtension.pbx_id == pbx_id, TelephonyExtension.extension == ext)
.first()
)
if not row:
row = TelephonyExtension(
business_id=business_id,
pbx_id=pbx_id,
extension=ext,
created_at=_now(),
updated_at=_now(),
)
db.add(row)
if payload.get("display_name"):
row.display_name = str(payload.get("display_name"))[:120]
if "team_code" in payload:
code = payload.get("team_code")
row.team_code = str(code).strip() if code else None
if "is_active" in payload:
row.is_active = bool(payload.get("is_active"))
row.updated_at = _now()
upserted += 1
if commit:
db.commit()
return {"ok": True, "upserted": upserted, "pbx_id": pbx_id}
def user_extension_to_dict(
row: TelephonyUserExtension, *, user_name: Optional[str] = None, can_supervise_team: Optional[bool] = None
) -> Dict[str, Any]:
ext = row.extension_row
return {
"id": row.id,
"business_id": row.business_id,
"user_id": row.user_id,
"user_name": user_name,
"pbx_id": row.pbx_id,
"extension_id": row.extension_id,
"extension": ext.extension if ext else None,
"extension_display_name": ext.display_name if ext else None,
"team_code": getattr(ext, "team_code", None) if ext else None,
"allow_shared": bool(getattr(ext, "allow_shared", False)) if ext else False,
"is_primary": bool(row.is_primary),
"receive_screen_pop": bool(row.receive_screen_pop),
"can_click_to_call": bool(row.can_click_to_call),
"can_supervise_team": bool(can_supervise_team),
"caller_id_override": row.caller_id_override,
"endpoint_mode": getattr(row, "endpoint_mode", None) or "desk",
"allow_mode_fallback": bool(getattr(row, "allow_mode_fallback", True)),
"direct_sip_user": getattr(row, "direct_sip_user", None),
}
def list_user_extensions(
db: Session,
business_id: int,
user_id: Optional[int] = None,
*,
scope: Optional[TelephonyScope] = None,
) -> List[Dict[str, Any]]:
q = (
db.query(TelephonyUserExtension)
.options(joinedload(TelephonyUserExtension.extension_row))
.filter(TelephonyUserExtension.business_id == business_id)
)
if scope is not None and not scope.view_all:
if user_id and int(user_id) != int(scope.user_id) and not scope.can_supervise:
user_id = scope.user_id
if user_id:
q = q.filter(TelephonyUserExtension.user_id == user_id)
if scope.can_supervise and int(user_id) != int(scope.user_id) and scope.extension_ids:
q = q.filter(TelephonyUserExtension.extension_id.in_(list(scope.extension_ids)))
elif scope.can_supervise and scope.extension_ids:
q = q.filter(
or_(
TelephonyUserExtension.user_id == scope.user_id,
TelephonyUserExtension.extension_id.in_(list(scope.extension_ids)),
)
)
else:
q = q.filter(TelephonyUserExtension.user_id == scope.user_id)
elif user_id:
q = q.filter(TelephonyUserExtension.user_id == user_id)
rows = q.order_by(TelephonyUserExtension.id.asc()).all()
user_ids = {r.user_id for r in rows}
users = {u.id: u for u in db.query(User).filter(User.id.in_(list(user_ids))).all()} if user_ids else {}
sup_rows = (
db.query(TelephonyTeamSupervisor)
.filter(TelephonyTeamSupervisor.business_id == business_id)
.all()
)
sup_pairs = {(int(r.user_id), str(r.team_code)) for r in sup_rows}
out = []
for r in rows:
team = getattr(r.extension_row, "team_code", None) if r.extension_row else None
out.append(
user_extension_to_dict(
r,
user_name=user_display_name(users.get(r.user_id)),
can_supervise_team=bool(team and (int(r.user_id), str(team)) in sup_pairs),
)
)
return out
def upsert_user_extension(db: Session, business_id: int, payload: Dict[str, Any]) -> Dict[str, Any]:
user_id = int(payload.get("user_id") or 0)
extension_id = int(payload.get("extension_id") or 0)
if not user_id or not extension_id:
raise ApiError("USER_AND_EXTENSION_REQUIRED", "USER_AND_EXTENSION_REQUIRED", http_status=400)
ext = (
db.query(TelephonyExtension)
.filter(TelephonyExtension.id == extension_id, TelephonyExtension.business_id == business_id)
.first()
)
if not ext:
raise ApiError("EXTENSION_NOT_FOUND", "EXTENSION_NOT_FOUND", http_status=404)
user = require_business_member(db, business_id, user_id)
if payload.get("allow_shared") is not None:
ext.allow_shared = bool(payload.get("allow_shared"))
taken = exclusive_extension_taken(
db,
business_id=business_id,
extension_id=extension_id,
user_id=user_id,
allow_shared=bool(getattr(ext, "allow_shared", False)),
)
if taken:
raise ApiError(
"EXTENSION_IN_USE",
"EXTENSION_IN_USE",
http_status=409,
)
row = (
db.query(TelephonyUserExtension)
.filter(
TelephonyUserExtension.business_id == business_id,
TelephonyUserExtension.user_id == user_id,
TelephonyUserExtension.extension_id == extension_id,
)
.first()
)
if not row:
row = TelephonyUserExtension(
business_id=business_id,
user_id=user_id,
pbx_id=ext.pbx_id,
extension_id=extension_id,
created_at=_now(),
updated_at=_now(),
)
db.add(row)
is_primary = bool(payload.get("is_primary", True))
if is_primary:
db.query(TelephonyUserExtension).filter(
TelephonyUserExtension.business_id == business_id,
TelephonyUserExtension.user_id == user_id,
TelephonyUserExtension.id != (row.id or 0),
).update({"is_primary": False}, synchronize_session=False)
row.is_primary = is_primary
if "receive_screen_pop" in payload:
row.receive_screen_pop = bool(payload["receive_screen_pop"])
if "can_click_to_call" in payload:
row.can_click_to_call = bool(payload["can_click_to_call"])
if "caller_id_override" in payload:
row.caller_id_override = payload.get("caller_id_override")
if "endpoint_mode" in payload:
mode = str(payload.get("endpoint_mode") or "desk").strip().lower()
if mode not in ("relay", "direct", "desk"):
raise ApiError("INVALID_ENDPOINT_MODE_VALUES", "INVALID_ENDPOINT_MODE_VALUES", http_status=400)
row.endpoint_mode = mode
if "allow_mode_fallback" in payload:
row.allow_mode_fallback = bool(payload.get("allow_mode_fallback"))
if "direct_sip_user" in payload:
row.direct_sip_user = payload.get("direct_sip_user")
row.pbx_id = ext.pbx_id
row.updated_at = _now()
if "can_supervise_team" in payload:
team = (getattr(ext, "team_code", None) or payload.get("team_code") or "").strip()
upsert_team_supervisor(
db, business_id, user_id, team, enabled=bool(payload.get("can_supervise_team"))
)
db.commit()
db.refresh(row)
row = (
db.query(TelephonyUserExtension)
.options(joinedload(TelephonyUserExtension.extension_row))
.filter(TelephonyUserExtension.id == row.id)
.first()
)
team = getattr(row.extension_row, "team_code", None) if row and row.extension_row else None
sup = False
if team:
sup = (
db.query(TelephonyTeamSupervisor)
.filter(
TelephonyTeamSupervisor.business_id == business_id,
TelephonyTeamSupervisor.user_id == user_id,
TelephonyTeamSupervisor.team_code == str(team),
)
.first()
is not None
)
data = user_extension_to_dict(row, user_name=user_display_name(user), can_supervise_team=sup)
return data
def delete_user_extension(db: Session, business_id: int, link_id: int) -> None:
row = (
db.query(TelephonyUserExtension)
.filter(TelephonyUserExtension.id == link_id, TelephonyUserExtension.business_id == business_id)
.first()
)
if not row:
raise ApiError("EXTENSION_MAPPING_NOT_FOUND", "EXTENSION_MAPPING_NOT_FOUND", http_status=404)
db.delete(row)
db.commit()
def get_my_extension_context(db: Session, business_id: int, user_id: int, auth_ctx=None) -> Dict[str, Any]:
scope = resolve_scope(db, auth_ctx, business_id) if auth_ctx is not None else None
links = list_user_extensions(db, business_id, user_id=user_id, scope=scope)
primary = next((x for x in links if x.get("is_primary")), links[0] if links else None)
include_token = bool(scope.can_manage) if scope else False
pbx_list = list_pbx(db, business_id, include_token_hint=include_token)
settings = settings_to_dict(get_or_create_settings(db, business_id))
if scope is not None and not scope.can_manage:
settings.pop("extra_settings", None)
missed_today = (
db.query(func.count(TelephonyCall.id))
.filter(
TelephonyCall.business_id == business_id,
TelephonyCall.assigned_user_id == user_id,
TelephonyCall.status == "missed",
TelephonyCall.started_at >= datetime.utcnow().replace(hour=0, minute=0, second=0, microsecond=0),
)
.scalar()
or 0
)
from app.services.telephony import softphone_service as softphone_svc
from app.services.telephony.media_hub import media_hub
softphone_ctx: Dict[str, Any] = {
"endpoint_mode": (primary or {}).get("endpoint_mode") or "desk",
"supported_profiles": ["pcm_ws_v1"],
}
try:
softphone_ctx = softphone_svc.softphone_health(db, business_id, user_id)
except Exception:
softphone_ctx["media_hub"] = media_hub.health()
softphone_ctx["tunnel"] = None
return {
"plugin_active": True,
"settings": settings,
"extensions": links,
"primary": primary,
"pbx_connections": pbx_list,
"missed_today": int(missed_today),
"has_extension": bool(links),
"softphone": softphone_ctx,
"access": scope.to_dict() if scope else None,
"assignable_users": list_assignable_users(db, business_id) if scope and scope.can_manage else [],
}
# ─── Caller ID match ────────────────────────────────────────────────────────
def _business_phone_region(db: Session, business_id: int) -> str:
from app.services.customer_quick_entry_service import get_business_default_phone_region
return get_business_default_phone_region(db, int(business_id))
def _dedupe_persons(rows: List[Person]) -> List[Person]:
seen: set[int] = set()
out: List[Person] = []
for p in rows:
if p.id not in seen:
seen.add(p.id)
out.append(p)
return out
def _persons_by_normalized(
db: Session,
business_id: int,
e164: str,
fields: List[str],
) -> List[Person]:
ors = []
for field in fields:
attr = _NORMALIZED_PERSON_ATTR.get(field)
if not attr:
continue
col = getattr(Person, attr, None)
if col is not None:
ors.append(col == e164)
if not ors:
return []
return (
db.query(Person)
.filter(Person.business_id == business_id, or_(*ors))
.limit(10)
.all()
)
def match_caller(
db: Session,
business_id: int,
raw_number: Optional[str],
phone_fields: Optional[List[str]] = None,
) -> Dict[str, Any]:
region = _business_phone_region(db, business_id)
ident = identity_candidates(raw_number, default_region=region)
candidates = ident["candidates"]
if not candidates:
return {
"person": None,
"lead": None,
"persons": [],
"leads": [],
"is_anonymous": True,
"normalized": None,
"e164": None,
"ambiguous": False,
}
# aliases
alias = (
db.query(TelephonyNumberAlias)
.filter(
TelephonyNumberAlias.business_id == business_id,
TelephonyNumberAlias.number_normalized.in_(candidates),
)
.first()
)
if alias:
person = db.get(Person, alias.person_id) if alias.person_id else None
lead = db.get(Lead, alias.lead_id) if alias.lead_id else None
return {
"person": _person_brief(person) if person and person.business_id == business_id else None,
"lead": _lead_brief(lead) if lead and lead.business_id == business_id else None,
"persons": [],
"leads": [],
"is_anonymous": person is None and lead is None,
"normalized": ident["canonical"],
"e164": ident["e164"],
"match_method": "alias",
"ambiguous": False,
}
# داخلی کوتاه را با موبایل دفترچه تطبیق نده
if ident["is_short"]:
return {
"person": None,
"lead": None,
"persons": [],
"leads": [],
"is_anonymous": True,
"normalized": ident["canonical"],
"e164": None,
"match_method": None,
"ambiguous": False,
}
fields = phone_fields or DEFAULT_PHONE_FIELDS
uniq_persons: List[Person] = []
e164 = ident.get("e164")
if e164:
uniq_persons = _dedupe_persons(_persons_by_normalized(db, business_id, str(e164), fields))
if not uniq_persons:
ors = []
for field in fields:
col = getattr(Person, field, None)
if col is not None:
ors.append(col.in_(candidates))
if ors:
uniq_persons = _dedupe_persons(
db.query(Person)
.filter(Person.business_id == business_id, or_(*ors))
.limit(10)
.all()
)
# soft suffix match فقط اگر exact/E.164 چیزی پیدا نشد (حداقل ۱۰ رقم)
if not uniq_persons:
seen = {p.id for p in uniq_persons}
suffixes = [extract_digits(c)[-10:] for c in candidates if len(extract_digits(c)) >= 10]
suffixes = list({s for s in suffixes if len(s) == 10})
soft_ors = []
for field in fields:
col = getattr(Person, field, None)
if col is None:
continue
for s in suffixes:
soft_ors.append(col.like(f"%{s}"))
if soft_ors:
soft_persons = (
db.query(Person)
.filter(Person.business_id == business_id, or_(*soft_ors))
.limit(10)
.all()
)
for p in soft_persons:
vals = [getattr(p, f, None) for f in fields]
if any(numbers_match(raw_number, v) for v in vals if v):
if p.id not in seen:
seen.add(p.id)
uniq_persons.append(p)
leads = (
db.query(Lead)
.filter(Lead.business_id == business_id, Lead.mobile.in_(candidates))
.limit(10)
.all()
)
if not leads:
suffixes = [extract_digits(c)[-10:] for c in candidates if len(extract_digits(c)) >= 10]
soft = []
for s in {x for x in suffixes if len(x) == 10}:
soft.append(Lead.mobile.like(f"%{s}"))
if soft:
cand = db.query(Lead).filter(Lead.business_id == business_id, or_(*soft)).limit(10).all()
leads = [l for l in cand if numbers_match(raw_number, l.mobile)]
is_anon = not uniq_persons and not leads
return {
"person": _person_brief(uniq_persons[0]) if len(uniq_persons) == 1 else None,
"lead": _lead_brief(leads[0]) if len(leads) == 1 and not uniq_persons else None,
"persons": [_person_brief(p) for p in uniq_persons],
"leads": [_lead_brief(l) for l in leads],
"is_anonymous": is_anon,
"normalized": ident["canonical"],
"e164": ident["e164"],
"match_method": "auto" if not is_anon else None,
"ambiguous": len(uniq_persons) > 1 or (len(uniq_persons) == 0 and len(leads) > 1),
}
def _person_brief(p: Optional[Person]) -> Optional[Dict[str, Any]]:
if not p:
return None
name = (p.alias_name or "").strip()
if not name:
name = f"{p.first_name or ''} {p.last_name or ''}".strip() or p.company_name or f"#{p.id}"
return {
"id": p.id,
"name": name,
"company_name": p.company_name,
"mobile": p.mobile,
"phone": p.phone,
"mobile_2": p.mobile_2,
"mobile_3": p.mobile_3,
}
def _peer_canonical_phone(call: TelephonyCall) -> Optional[str]:
raw = call.from_number_normalized if call.direction == "inbound" else call.to_number_normalized
raw = raw or (call.from_number_raw if call.direction == "inbound" else call.to_number_raw)
canon = normalize_iran_phone(raw)["canonical"]
if canon:
return str(canon)[:20]
digits = extract_digits(raw or "")
if len(digits) >= 10 and digits[-10:].startswith("9"):
return ("0" + digits[-10:])[:20]
text = str(raw).strip() if raw else ""
return text[:20] if text else None
def _remember_call_match(call: TelephonyCall, *, person: Optional[Person] = None, lead: Optional[Lead] = None) -> None:
extra = dict(call.extra_info or {})
match = dict(extra.get("match") or {}) if isinstance(extra.get("match"), dict) else {}
if person is not None:
match["person"] = _person_brief(person)
if lead is not None:
match["lead"] = _lead_brief(lead)
match["is_anonymous"] = not (match.get("person") or match.get("lead"))
extra["match"] = match
call.extra_info = extra
def _lead_brief(l: Optional[Lead]) -> Optional[Dict[str, Any]]:
if not l:
return None
return {
"id": l.id,
"name": l.name,
"company_name": l.company_name,
"mobile": l.mobile,
"code": l.code,
}
# ─── Calls ───────────────────────────────────────────────────────────────────
_PUBLIC_EXTRA_BLOCKED = frozenset(
{"recording_url", "recording_path", "recording_file", "connector_token", "install_hint"}
)
def _public_extra(info: Any) -> Dict[str, Any]:
if not isinstance(info, dict):
return {}
return {k: v for k, v in info.items() if k not in _PUBLIC_EXTRA_BLOCKED}
def _peer_number(row: TelephonyCall) -> Optional[str]:
if getattr(row, "direction", None) == "inbound":
return row.from_number_normalized or row.from_number_raw
if getattr(row, "direction", None) == "outbound":
return row.to_number_normalized or row.to_number_raw
return (
row.from_number_normalized
or row.to_number_normalized
or row.from_number_raw
or row.to_number_raw
)
def _name_from_match_extra(row: TelephonyCall, key: str) -> Optional[str]:
extra = getattr(row, "extra_info", None) or {}
if not isinstance(extra, dict):
return None
match = extra.get("match")
if not isinstance(match, dict):
return None
brief = match.get(key)
if isinstance(brief, dict):
name = str(brief.get("name") or "").strip()
return name or None
return None
def _load_party_names(
db: Session, rows: List[TelephonyCall]
) -> Tuple[Dict[int, str], Dict[int, str]]:
pids = {int(r.person_id) for r in rows if getattr(r, "person_id", None)}
lids = {int(r.lead_id) for r in rows if getattr(r, "lead_id", None)}
persons: Dict[int, str] = {}
if pids:
for p in db.query(Person).filter(Person.id.in_(pids)).all():
brief = _person_brief(p)
if brief:
persons[int(p.id)] = str(brief["name"])
leads: Dict[int, str] = {}
if lids:
for lead in db.query(Lead).filter(Lead.id.in_(lids)).all():
brief = _lead_brief(lead)
if brief:
leads[int(lead.id)] = str(brief["name"])
return persons, leads
def call_to_dict(
row: TelephonyCall,
*,
person_name: Optional[str] = None,
lead_name: Optional[str] = None,
) -> Dict[str, Any]:
if not person_name:
person_name = _name_from_match_extra(row, "person")
if not lead_name:
lead_name = _name_from_match_extra(row, "lead")
peer = _peer_number(row)
party_name = person_name or lead_name or peer
return {
"id": row.id,
"business_id": row.business_id,
"pbx_id": row.pbx_id,
"asterisk_uniqueid": row.asterisk_uniqueid,
"asterisk_linkedid": row.asterisk_linkedid,
"direction": row.direction,
"status": row.status,
"from_number_raw": row.from_number_raw,
"from_number_normalized": row.from_number_normalized,
"to_number_raw": row.to_number_raw,
"to_number_normalized": row.to_number_normalized,
"extension": row.extension,
"queue_code": row.queue_code,
"transferred_from": row.transferred_from,
"transferred_to": row.transferred_to,
"started_at": _iso(row.started_at),
"answered_at": _iso(row.answered_at),
"ended_at": _iso(row.ended_at),
"duration_sec": row.duration_sec,
"talk_sec": row.talk_sec,
"hangup_cause": row.hangup_cause,
"recording_status": row.recording_status,
"recording_available": row.recording_status == "available",
"recording_url": None,
"person_id": row.person_id,
"person_name": person_name,
"lead_name": lead_name,
"party_name": party_name,
"peer_number": peer,
"lead_id": row.lead_id,
"deal_id": row.deal_id,
"document_id": row.document_id,
"crm_activity_id": row.crm_activity_id,
"assigned_user_id": row.assigned_user_id,
"category": row.category,
"outcome": row.outcome,
"note": row.note,
"match_method": row.match_method,
"is_anonymous": bool(row.is_anonymous),
"extra_info": _public_extra(row.extra_info),
"created_at": _iso(row.created_at),
"updated_at": _iso(row.updated_at),
}
def _hidden_crm_ids(db: Session, business_id: int, ctx: Any, entity_type: str, ids: List[int]) -> set[int]:
"""شناسه‌هایی که بینندهٔ فعلی حق دیدنشان را ندارد. مجموعهٔ خالی یعنی چیزی پنهان نیست."""
wanted = {int(item) for item in ids if item}
if ctx is None or not wanted:
return set()
from adapters.db.models.crm import Deal, Lead
from app.services.crm_enterprise_service import record_acl_enforced, restrict_visibility_query
if not record_acl_enforced(db, business_id, ctx):
return set()
model = Lead if entity_type == "lead" else Deal
visible = restrict_visibility_query(
db.query(model.id).filter(model.business_id == business_id, model.id.in_(wanted)),
db, business_id, ctx, model, entity_type,
)
seen = {int(row[0]) for row in visible.all()}
return wanted - seen
def _scrub_one_call(data: Dict[str, Any], hidden_leads: set[int], hidden_deals: set[int]) -> Dict[str, Any]:
if not hidden_leads and not hidden_deals:
return data
data = dict(data)
lead_id = data.get("lead_id")
if lead_id and int(lead_id) in hidden_leads:
if data.get("party_name") == data.get("lead_name"):
data["party_name"] = data.get("person_name") or data.get("peer_number")
data["lead_id"] = None
data["lead_name"] = None
if data.get("deal_id") and int(data["deal_id"]) in hidden_deals:
data["deal_id"] = None
extra = data.get("extra_info")
if isinstance(extra, dict) and isinstance(extra.get("match"), dict):
data["extra_info"] = {**extra, "match": _scrub_match_dict(extra["match"], hidden_leads)}
return data
def _scrub_match_dict(match: Dict[str, Any], hidden_leads: set[int]) -> Dict[str, Any]:
if not hidden_leads:
return match
cleaned = dict(match)
lead = cleaned.get("lead")
if isinstance(lead, dict) and int(lead.get("id") or 0) in hidden_leads:
cleaned["lead"] = None
leads = []
for item in cleaned.get("leads") or []:
if isinstance(item, dict) and int(item.get("id") or 0) in hidden_leads:
continue
leads.append(item)
cleaned["leads"] = leads
return cleaned
def scrub_calls_for_viewer(db: Session, business_id: int, ctx: Any, items: List[Dict[str, Any]]) -> List[Dict[str, Any]]:
if ctx is None or not items:
return items
lead_ids: List[int] = []
deal_ids: List[int] = []
for data in items:
if data.get("lead_id"):
lead_ids.append(int(data["lead_id"]))
if data.get("deal_id"):
deal_ids.append(int(data["deal_id"]))
extra = data.get("extra_info") if isinstance(data.get("extra_info"), dict) else None
match = extra.get("match") if isinstance(extra, dict) else None
if isinstance(match, dict):
lead = match.get("lead")
if isinstance(lead, dict) and lead.get("id"):
lead_ids.append(int(lead["id"]))
for item in match.get("leads") or []:
if isinstance(item, dict) and item.get("id"):
lead_ids.append(int(item["id"]))
hidden_leads = _hidden_crm_ids(db, business_id, ctx, "lead", lead_ids)
hidden_deals = _hidden_crm_ids(db, business_id, ctx, "deal", deal_ids)
if not hidden_leads and not hidden_deals:
return items
return [_scrub_one_call(item, hidden_leads, hidden_deals) for item in items]
def scrub_match_for_viewer(db: Session, business_id: int, ctx: Any, match: Dict[str, Any]) -> Dict[str, Any]:
if ctx is None or not match:
return match
lead_ids: List[int] = []
lead = match.get("lead")
if isinstance(lead, dict) and lead.get("id"):
lead_ids.append(int(lead["id"]))
for item in match.get("leads") or []:
if isinstance(item, dict) and item.get("id"):
lead_ids.append(int(item["id"]))
hidden = _hidden_crm_ids(db, business_id, ctx, "lead", lead_ids)
return _scrub_match_dict(match, hidden)
def list_calls(
db: Session,
business_id: int,
*,
direction: Optional[str] = None,
status: Optional[str] = None,
assigned_user_id: Optional[int] = None,
person_id: Optional[int] = None,
q: Optional[str] = None,
limit: int = 50,
offset: int = 0,
scope: Optional[TelephonyScope] = None,
auth_ctx=None,
) -> Dict[str, Any]:
query = db.query(TelephonyCall).filter(TelephonyCall.business_id == business_id)
resolved = scope or (resolve_scope(db, auth_ctx, business_id) if auth_ctx is not None else None)
if resolved is not None:
query = resolved.apply_call_filter(query)
if direction:
query = query.filter(TelephonyCall.direction == direction)
if status:
query = query.filter(TelephonyCall.status == status)
if assigned_user_id:
query = query.filter(TelephonyCall.assigned_user_id == assigned_user_id)
if person_id:
query = query.filter(TelephonyCall.person_id == person_id)
if q:
like = f"%{q.strip()}%"
query = query.filter(
or_(
TelephonyCall.from_number_raw.ilike(like),
TelephonyCall.from_number_normalized.ilike(like),
TelephonyCall.to_number_raw.ilike(like),
TelephonyCall.to_number_normalized.ilike(like),
TelephonyCall.extension.ilike(like),
TelephonyCall.note.ilike(like),
)
)
total = query.count()
rows = query.order_by(TelephonyCall.started_at.desc()).offset(offset).limit(min(limit, 200)).all()
pnames, lnames = _load_party_names(db, rows)
items = scrub_calls_for_viewer(
db,
business_id,
auth_ctx,
[
call_to_dict(
r,
person_name=pnames.get(int(r.person_id)) if r.person_id else None,
lead_name=lnames.get(int(r.lead_id)) if r.lead_id else None,
)
for r in rows
],
)
return {
"items": items,
"total": total,
"limit": limit,
"offset": offset,
}
def get_call(db: Session, business_id: int, call_id: int, *, auth_ctx=None, scope: Optional[TelephonyScope] = None) -> Dict[str, Any]:
if auth_ctx is not None:
row = require_call_in_scope(db, auth_ctx, business_id, call_id, scope=scope)
else:
row = (
db.query(TelephonyCall)
.filter(TelephonyCall.id == call_id, TelephonyCall.business_id == business_id)
.first()
)
if not row:
raise ApiError("CALL_NOT_FOUND", "CALL_NOT_FOUND", http_status=404)
pnames, lnames = _load_party_names(db, [row])
data = scrub_calls_for_viewer(
db,
business_id,
auth_ctx,
[
call_to_dict(
row,
person_name=pnames.get(int(row.person_id)) if row.person_id else None,
lead_name=lnames.get(int(row.lead_id)) if row.lead_id else None,
)
],
)[0]
events = (
db.query(TelephonyCallEvent)
.filter(TelephonyCallEvent.call_id == call_id)
.order_by(TelephonyCallEvent.occurred_at.asc())
.all()
)
data["events"] = [
{
"id": e.id,
"event_id": e.event_id,
"event_type": e.event_type,
"payload": _public_extra(e.payload),
"occurred_at": _iso(e.occurred_at),
}
for e in events
]
return data
def patch_call(
db: Session,
business_id: int,
call_id: int,
payload: Dict[str, Any],
*,
auth_ctx=None,
scope: Optional[TelephonyScope] = None,
) -> Dict[str, Any]:
if auth_ctx is not None:
row = require_call_in_scope(db, auth_ctx, business_id, call_id, scope=scope)
resolved = scope or resolve_scope(db, auth_ctx, business_id)
else:
row = (
db.query(TelephonyCall)
.filter(TelephonyCall.id == call_id, TelephonyCall.business_id == business_id)
.first()
)
if not row:
raise ApiError("CALL_NOT_FOUND", "CALL_NOT_FOUND", http_status=404)
resolved = None
keys = [
"note",
"category",
"outcome",
"person_id",
"lead_id",
"deal_id",
"document_id",
"match_method",
]
if resolved is None or resolved.can_manage or resolved.can_supervise:
keys.append("assigned_user_id")
if payload.get("lead_id"):
linked_lead = db.query(Lead).filter(Lead.id == int(payload["lead_id"]), Lead.business_id == business_id).first()
if linked_lead is None:
raise ApiError("NOT_FOUND", "سرنخ یافت نشد.", http_status=404)
if auth_ctx is not None:
from app.services.crm_enterprise_service import assert_can_view_record
assert_can_view_record(db, business_id, auth_ctx, "lead", linked_lead, write=True)
if payload.get("deal_id"):
from adapters.db.models.crm import Deal
from app.services.crm_enterprise_service import assert_can_view_record
linked_deal = db.query(Deal).filter(Deal.id == int(payload["deal_id"]), Deal.business_id == business_id).first()
if linked_deal is None:
raise ApiError("NOT_FOUND", "فرصت فروش یافت نشد.", http_status=404)
if auth_ctx is not None:
assert_can_view_record(db, business_id, auth_ctx, "deal", linked_deal, write=True)
for key in keys:
if key in payload:
setattr(row, key, payload[key])
if payload.get("person_id"):
row.is_anonymous = False
row.updated_at = _now()
db.commit()
db.refresh(row)
return scrub_calls_for_viewer(db, business_id, auth_ctx, [call_to_dict(row)])[0]
def _ensure_extension_catalog(db: Session, pbx: TelephonyPbxConnection, extension: str) -> None:
ext = str(extension or "").strip()
if not ext:
return
exists = (
db.query(TelephonyExtension.id)
.filter(TelephonyExtension.pbx_id == pbx.id, TelephonyExtension.extension == ext)
.first()
)
if exists:
return
db.add(
TelephonyExtension(
business_id=pbx.business_id,
pbx_id=pbx.id,
extension=ext,
created_at=_now(),
updated_at=_now(),
)
)
db.flush()
def _users_for_extension(db: Session, business_id: int, extension: Optional[str]) -> List[TelephonyUserExtension]:
if not extension:
return []
return (
db.query(TelephonyUserExtension)
.options(joinedload(TelephonyUserExtension.extension_row))
.join(TelephonyExtension, TelephonyExtension.id == TelephonyUserExtension.extension_id)
.filter(
TelephonyUserExtension.business_id == business_id,
TelephonyExtension.extension == extension,
TelephonyUserExtension.receive_screen_pop == True, # noqa: E712
)
.all()
)
async def _notify_users(user_ids: List[int], message: Dict[str, Any]) -> None:
for uid in set(user_ids):
try:
await realtime_manager.send_to_user(uid, message)
except Exception:
pass
def _notify_users_sync(user_ids: List[int], message: Dict[str, Any]) -> None:
import anyio
try:
anyio.from_thread.run(_notify_users, user_ids, message)
except Exception:
try:
import asyncio
asyncio.get_event_loop().create_task(_notify_users(user_ids, message))
except Exception:
pass
def _softphone_lead_audience(
db: Session,
business_id: int,
lead: Optional[Lead],
user_ids: List[int],
) -> Optional[Set[int]]:
"""None یعنی فیلتر لازم نیست. مجموعه یعنی فقط همین کاربران نام سرنخ را می‌بینند."""
if lead is None:
return None
from adapters.db.models.business_crm_settings import BusinessCrmSettings
from app.services.crm_enterprise_service import acl_actor, assert_can_view_record, record_acl_enforced
settings = db.query(BusinessCrmSettings).filter(BusinessCrmSettings.business_id == business_id).first()
if settings is None or not settings.record_acl_enabled:
return None
allowed: Set[int] = set()
for user_id in {int(item) for item in user_ids}:
ctx = acl_actor(db, user_id)
if not record_acl_enforced(db, business_id, ctx):
allowed.add(user_id)
continue
try:
assert_can_view_record(db, business_id, ctx, "lead", lead, write=False)
except ApiError:
continue
allowed.add(user_id)
return allowed
def _notify_softphone_incoming_sync(
*,
pbx_id: int,
extension: str,
call_id: int,
from_number: Optional[str] = None,
channel: Optional[str] = None,
person: Optional[Dict[str, Any]] = None,
lead: Optional[Dict[str, Any]] = None,
lead_visible_user_ids: Optional[Set[int]] = None,
ambiguous: bool = False,
) -> None:
import anyio
async def _run() -> None:
from app.services.telephony.media_hub import media_hub
await media_hub.notify_softphone_incoming(
pbx_id=pbx_id,
extension=extension,
call_id=call_id,
from_number=from_number,
channel=channel,
person=person,
lead=lead,
lead_visible_user_ids=lead_visible_user_ids,
ambiguous=ambiguous,
)
try:
anyio.from_thread.run(_run)
except Exception:
try:
import asyncio
loop = asyncio.get_event_loop()
if loop.is_running():
asyncio.run_coroutine_threadsafe(_run(), loop)
else:
loop.run_until_complete(_run())
except Exception:
pass
def process_connector_event(
db: Session,
pbx: TelephonyPbxConnection,
payload: Dict[str, Any],
) -> Dict[str, Any]:
event_id = str(payload.get("event_id") or secrets.token_hex(16))
existing = (
db.query(TelephonyCallEvent)
.filter(TelephonyCallEvent.pbx_id == pbx.id, TelephonyCallEvent.event_id == event_id)
.first()
)
if existing:
return {"ok": True, "duplicate": True, "call_id": existing.call_id}
event_type = str(payload.get("type") or payload.get("event_type") or "").strip()
uniqueid = str(payload.get("uniqueid") or "").strip()
if not event_type or not uniqueid:
raise ApiError("EVENT_TYPE_REQUIRED", "EVENT_TYPE_REQUIRED", http_status=400)
occurred_at = _now()
raw_at = payload.get("pbx_event_at")
if raw_at:
try:
occurred_at = datetime.fromisoformat(str(raw_at).replace("Z", "+00:00")).replace(tzinfo=None)
except Exception:
pass
call = (
db.query(TelephonyCall)
.filter(TelephonyCall.pbx_id == pbx.id, TelephonyCall.asterisk_uniqueid == uniqueid)
.first()
)
if not call and payload.get("linkedid"):
call = (
db.query(TelephonyCall)
.filter(TelephonyCall.pbx_id == pbx.id, TelephonyCall.asterisk_linkedid == str(payload.get("linkedid")))
.filter(TelephonyCall.ended_at.is_(None))
.order_by(TelephonyCall.id.desc())
.first()
)
if not call:
from app.services.telephony.ami_channels import match_call_by_ami_ids, match_softphone_relay_call
recent = (
db.query(TelephonyCall)
.filter(
TelephonyCall.pbx_id == pbx.id,
TelephonyCall.ended_at.is_(None),
TelephonyCall.status.in_(["ringing", "answered"]),
TelephonyCall.started_at >= _now() - timedelta(minutes=15),
)
.order_by(TelephonyCall.id.desc())
.limit(25)
.all()
)
call = match_softphone_relay_call(recent, payload) or match_call_by_ami_ids(
recent, uniqueid, payload.get("linkedid")
)
from app.services.telephony.ami_channels import (
append_ami_uniqueid,
is_softphone_outbound_call,
should_create_call_from_ami_event,
should_ignore_softphone_outbound_hangup,
should_skip_unmatched_ami_event,
)
settings = get_or_create_settings(db, pbx.business_id)
soft_out = is_softphone_outbound_call(call) if call else False
direction = str(payload.get("direction") or (call.direction if call else "inbound")).lower()
if soft_out:
# رویداد AMI طرفین Originate را برعکس می‌کند؛ فیلدهای Softphone را نگه دار
extension = call.extension
from_raw = call.from_number_raw
to_raw = call.to_number_raw
else:
extension = str(payload.get("extension") or (call.extension if call else "") or "").strip() or None
from_raw = payload.get("from") or payload.get("from_number") or (call.from_number_raw if call else None)
to_raw = payload.get("to") or payload.get("to_number") or (call.to_number_raw if call else None)
if extension:
_ensure_extension_catalog(db, pbx, extension)
from_norm = normalize_iran_phone(from_raw)["canonical"]
to_norm = normalize_iran_phone(to_raw)["canonical"]
created = False
if not call and event_type in ("call.answered", "answered"):
# Answer بدون تماس قبلی (مثلاً Local AudioSocket) نباید رکورد جعلی بسازد
return {"ok": True, "ignored": True, "reason": "answered_without_call"}
if not call and (
should_skip_unmatched_ami_event(event_type) or not should_create_call_from_ami_event(event_type)
):
return {"ok": True, "ignored": True, "reason": "event_without_call"}
if not call:
match = match_caller(
db,
pbx.business_id,
from_raw if direction == "inbound" else to_raw,
phone_fields=settings.primary_phone_fields or DEFAULT_PHONE_FIELDS,
)
assigned_user_id = None
links = _users_for_extension(db, pbx.business_id, extension)
if links:
assigned_user_id = links[0].user_id
call = TelephonyCall(
business_id=pbx.business_id,
pbx_id=pbx.id,
asterisk_uniqueid=uniqueid,
asterisk_linkedid=payload.get("linkedid"),
direction=direction if direction in ("inbound", "outbound", "internal") else "inbound",
status="ringing",
from_number_raw=str(from_raw) if from_raw else None,
from_number_normalized=from_norm,
to_number_raw=str(to_raw) if to_raw else None,
to_number_normalized=to_norm,
extension=extension,
queue_code=payload.get("queue"),
started_at=occurred_at,
person_id=(match.get("person") or {}).get("id") if match.get("person") else None,
lead_id=(match.get("lead") or {}).get("id") if match.get("lead") else None,
assigned_user_id=assigned_user_id,
match_method=match.get("match_method"),
is_anonymous=bool(match.get("is_anonymous")),
extra_info={"match": match},
created_at=_now(),
updated_at=_now(),
)
db.add(call)
db.flush()
created = True
else:
if not soft_out:
if from_raw:
call.from_number_raw = str(from_raw)
call.from_number_normalized = from_norm
if to_raw:
call.to_number_raw = str(to_raw)
call.to_number_normalized = to_norm
if extension:
call.extension = extension
if payload.get("queue"):
call.queue_code = payload.get("queue")
incoming_linkedid = str(payload.get("linkedid") or "").strip()
current_linkedid = str(call.asterisk_linkedid or "")
if incoming_linkedid and not current_linkedid.startswith("hsx-sf-"):
call.asterisk_linkedid = incoming_linkedid
# uniqueid ساختگی Softphone را به uniqueid واقعی Asterisk بچسبان تا Hangup بعدی پیدا شود
extra_bind = append_ami_uniqueid(call.extra_info, uniqueid)
already_bound = bool(extra_bind.get("asterisk_uniqueid_bound"))
if call.asterisk_uniqueid != uniqueid and not already_bound:
taken = (
db.query(TelephonyCall.id)
.filter(
TelephonyCall.pbx_id == pbx.id,
TelephonyCall.asterisk_uniqueid == uniqueid,
TelephonyCall.id != call.id,
)
.first()
)
if not taken:
try:
with db.begin_nested():
call.asterisk_uniqueid = uniqueid
extra_bind["asterisk_uniqueid_bound"] = True
call.extra_info = extra_bind
db.flush()
except IntegrityError:
call.extra_info = extra_bind
else:
call.extra_info = extra_bind
else:
call.extra_info = extra_bind
# state transitions
notify_type = None
skip_status = (not created) and should_ignore_softphone_outbound_hangup(call, payload, event_type)
if skip_status:
pass
elif event_type in ("call.ringing", "ringing"):
if call.status not in ("answered", "completed", "missed", "busy", "failed", "cancelled"):
call.status = "ringing"
if created or not is_softphone_outbound_call(call):
notify_type = "telephony.incoming_call" if call.direction == "inbound" else "telephony.outbound_ringing"
elif event_type in ("call.answered", "answered"):
call.status = "answered"
call.answered_at = call.answered_at or occurred_at
notify_type = "telephony.call_answered"
elif event_type in ("call.missed", "missed"):
call.status = "missed"
call.ended_at = occurred_at
notify_type = "telephony.call_missed"
elif event_type in ("call.busy", "busy"):
call.status = "busy"
call.ended_at = occurred_at
notify_type = "telephony.call_ended"
elif event_type in ("call.failed", "failed"):
call.status = "failed"
call.ended_at = occurred_at
notify_type = "telephony.call_ended"
elif event_type in ("call.ended", "ended", "hangup"):
if call.status == "answered":
call.status = "completed"
elif call.status == "ringing":
call.status = "missed"
notify_type = "telephony.call_missed"
call.ended_at = occurred_at
if call.started_at:
call.duration_sec = max(0, int((occurred_at - call.started_at).total_seconds()))
if call.answered_at:
call.talk_sec = max(0, int((occurred_at - call.answered_at).total_seconds()))
call.hangup_cause = payload.get("hangup_cause") or call.hangup_cause
notify_type = notify_type or "telephony.call_ended"
elif event_type in ("recording.ready",):
call.recording_status = "available"
call.recording_remote_path = payload.get("recording_path") or call.recording_remote_path
call.recording_url = payload.get("recording_url") or call.recording_url
call.updated_at = _now()
ev = TelephonyCallEvent(
business_id=pbx.business_id,
pbx_id=pbx.id,
call_id=call.id,
event_id=event_id,
event_type=event_type,
payload=payload,
occurred_at=occurred_at,
created_at=_now(),
)
db.add(ev)
# auto CRM activity on terminal states
if settings.auto_create_activity and call.status in ("completed", "missed", "busy", "failed") and not call.crm_activity_id:
try:
_ensure_crm_activity(db, call, user_id=call.assigned_user_id)
except Exception:
pass
# missed call follow-up task
if call.status == "missed" and settings.missed_call_create_task:
try:
_ensure_missed_call_task(db, call, settings)
except Exception:
pass
# presence updates
try:
from app.services.telephony.live_service import update_presence_from_event
if call.status == "ringing":
update_presence_from_event(db, pbx.business_id, call.extension, "ringing")
elif call.status == "answered":
update_presence_from_event(db, pbx.business_id, call.extension, "busy")
elif call.status in ("completed", "missed", "busy", "failed", "cancelled"):
update_presence_from_event(db, pbx.business_id, call.extension, "idle")
except Exception:
pass
try:
from app.services.telephony.recording_service import bump_metric, apply_recording_meta
bump_metric(db, pbx.business_id, f"events_{event_type.replace('.', '_')}")
bump_metric(db, pbx.business_id, "events_total")
if event_type in ("recording.ready",):
apply_recording_meta(db, call, payload)
except Exception:
pass
# store channel for later control (DestChannel برای Hangup واقعی لازم است)
if payload.get("channel") or payload.get("dest_channel"):
extra = dict(call.extra_info or {})
if payload.get("channel"):
extra["channel"] = payload.get("channel")
if payload.get("dest_channel"):
extra["dest_channel"] = payload.get("dest_channel")
call.extra_info = extra
db.commit()
db.refresh(call)
# Softphone Relay: زنگ با call_id به کلاینت آنلاین همان داخلی
if (
payload.get("softphone_relay")
and event_type in ("call.ringing", "ringing")
and call.extension
and call.status == "ringing"
):
person_brief = _person_brief(db.get(Person, call.person_id)) if call.person_id else None
lead_row = db.get(Lead, call.lead_id) if call.lead_id else None
if lead_row is not None and int(lead_row.business_id) != int(pbx.business_id):
lead_row = None
match_info = (call.extra_info or {}).get("match") if isinstance(call.extra_info, dict) else None
if lead_row is None and isinstance(match_info, dict) and isinstance(match_info.get("lead"), dict):
raw_lead_id = match_info["lead"].get("id")
if raw_lead_id:
candidate = db.get(Lead, int(raw_lead_id))
if candidate is not None and int(candidate.business_id) == int(pbx.business_id):
lead_row = candidate
from app.services.telephony.media_hub import media_hub
online_users = media_hub.extension_user_ids(pbx.id, str(call.extension))
_notify_softphone_incoming_sync(
pbx_id=pbx.id,
extension=str(call.extension),
call_id=int(call.id),
from_number=call.from_number_normalized or call.from_number_raw,
channel=str(payload.get("channel") or "") or None,
person=person_brief or ((match_info or {}).get("person") if isinstance(match_info, dict) else None),
lead=_lead_brief(lead_row),
lead_visible_user_ids=_softphone_lead_audience(db, pbx.business_id, lead_row, online_users),
ambiguous=bool(isinstance(match_info, dict) and match_info.get("ambiguous")),
)
# realtime
links = _users_for_extension(db, pbx.business_id, call.extension)
user_ids = [lnk.user_id for lnk in links if lnk.receive_screen_pop]
if call.assigned_user_id and call.assigned_user_id not in user_ids:
user_ids.append(call.assigned_user_id)
if notify_type and user_ids and (settings.screen_pop_enabled or notify_type != "telephony.incoming_call"):
person_brief = None
if call.person_id:
person_brief = _person_brief(db.get(Person, call.person_id))
msg = {
"type": notify_type,
"business_id": pbx.business_id,
"call_id": call.id,
"extension": call.extension,
"direction": call.direction,
"status": call.status,
"from_number": call.from_number_normalized or call.from_number_raw,
"to_number": call.to_number_normalized or call.to_number_raw,
"person": person_brief,
"is_anonymous": call.is_anonymous,
"deep_link": f"/business/{pbx.business_id}/telephony/calls/{call.id}",
}
_notify_users_sync(user_ids, msg)
return {"ok": True, "created": created, "call": call_to_dict(call)}
def _ensure_crm_activity(db: Session, call: TelephonyCall, user_id: Optional[int]) -> None:
uid = user_id or call.assigned_user_id
if not uid:
# fallback: business owner not resolved here — skip
return
code = f"TEL-{call.id}"
existing = (
db.query(CrmActivity)
.filter(CrmActivity.business_id == call.business_id, CrmActivity.code == code)
.first()
)
if existing:
call.crm_activity_id = existing.id
return
direction_fa = {"inbound": "ورودی", "outbound": "خروجی", "internal": "داخلی"}.get(call.direction, call.direction)
party = None
if call.person_id:
brief = _person_brief(db.get(Person, call.person_id))
party = brief.get("name") if brief else None
elif call.lead_id:
brief = _lead_brief(db.get(Lead, call.lead_id))
party = brief.get("name") if brief else None
peer = _peer_number(call) or call.from_number_normalized or call.from_number_raw or ""
subject = f"تماس {direction_fa} {party or peer}".strip()
act = CrmActivity(
business_id=call.business_id,
person_id=call.person_id,
lead_id=call.lead_id,
deal_id=call.deal_id,
code=code,
activity_type="call",
subject=subject[:255],
description=call.note,
activity_date=call.started_at or _now(),
created_by_user_id=uid,
assigned_to_user_id=uid,
status="done" if call.status in ("completed", "missed", "busy", "failed") else "open",
completed_at=call.ended_at,
outcome=call.outcome or call.status,
extra_info={"telephony_call_id": call.id, "direction": call.direction, "duration_sec": call.duration_sec},
telephony_call_id=call.id,
created_at=_now(),
updated_at=_now(),
)
db.add(act)
db.flush()
call.crm_activity_id = act.id
def _ensure_missed_call_task(db: Session, call: TelephonyCall, settings: TelephonySettings) -> None:
uid = call.assigned_user_id
if not uid:
return
code = f"TEL-MISS-{call.id}"
existing = (
db.query(CrmActivity)
.filter(CrmActivity.business_id == call.business_id, CrmActivity.code == code)
.first()
)
if existing:
return
due_min = int(settings.missed_call_task_due_minutes or 60)
due_at = (call.ended_at or _now()) + timedelta(minutes=due_min)
number = _peer_number(call) or call.from_number_normalized or call.from_number_raw or ""
party = None
if call.person_id:
brief = _person_brief(db.get(Person, call.person_id))
party = brief.get("name") if brief else None
elif call.lead_id:
brief = _lead_brief(db.get(Lead, call.lead_id))
party = brief.get("name") if brief else None
act = CrmActivity(
business_id=call.business_id,
person_id=call.person_id,
lead_id=call.lead_id,
code=code,
activity_type="call",
subject=f"پیگیری تماس از دست‌رفته {party or number}"[:255],
description="ایجاد خودکار پس از تماس از دست‌رفته",
activity_date=_now(),
created_by_user_id=uid,
assigned_to_user_id=uid,
is_task=True,
due_at=due_at,
status="open",
priority="high",
extra_info={"telephony_call_id": call.id, "auto_missed_followup": True},
telephony_call_id=call.id,
created_at=_now(),
updated_at=_now(),
)
db.add(act)
# ─── Screen pop context ─────────────────────────────────────────────────────
def screen_pop_context(db: Session, business_id: int, call_id: int, *, auth_ctx=None) -> Dict[str, Any]:
from app.services.person_service import calculate_person_balance
from adapters.db.models.check import Check, CheckStatus
if auth_ctx is not None:
call = require_call_in_scope(db, auth_ctx, business_id, call_id)
else:
call = (
db.query(TelephonyCall)
.filter(TelephonyCall.id == call_id, TelephonyCall.business_id == business_id)
.first()
)
if not call:
raise ApiError("CALL_NOT_FOUND", "CALL_NOT_FOUND", http_status=404)
match = (call.extra_info or {}).get("match") or match_caller(
db, business_id, call.from_number_raw if call.direction == "inbound" else call.to_number_raw
)
person_id = call.person_id or (match.get("person") or {}).get("id")
lead_id = call.lead_id or (match.get("lead") or {}).get("id")
balance = None
recent_docs: List[Dict[str, Any]] = []
overdue_checks: List[Dict[str, Any]] = []
open_deals: List[Dict[str, Any]] = []
person = None
lead_row = None
if lead_id:
lead_row = db.get(Lead, lead_id)
if lead_row and lead_row.business_id != business_id:
lead_row = None
elif lead_row and auth_ctx is not None:
hidden_leads = _hidden_crm_ids(db, business_id, auth_ctx, "lead", [int(lead_row.id)])
if int(lead_row.id) in hidden_leads:
lead_row = None
if person_id:
person = db.get(Person, person_id)
if person and person.business_id == business_id:
try:
amount, status = calculate_person_balance(db, person_id)
balance = {"amount": amount, "status": status}
except Exception:
balance = None
from adapters.db.models.document import Document
from adapters.db.models.document_line import DocumentLine
docs = (
db.query(Document)
.join(DocumentLine, DocumentLine.document_id == Document.id)
.filter(Document.business_id == business_id, DocumentLine.person_id == person_id)
.order_by(Document.document_date.desc())
.limit(5)
.all()
)
recent_docs = [
{
"id": d.id,
"code": d.code,
"document_type": d.document_type,
"document_date": _iso(d.document_date) if hasattr(d.document_date, "isoformat") else str(d.document_date),
"description": d.description,
}
for d in docs
]
now = _now()
checks = (
db.query(Check)
.filter(
Check.business_id == business_id,
Check.person_id == person_id,
Check.due_date <= now,
Check.status.in_(
[
CheckStatus.RECEIVED_ON_HAND,
CheckStatus.DEPOSITED,
CheckStatus.BOUNCED,
CheckStatus.RETURNED,
]
),
)
.order_by(Check.due_date.asc())
.limit(5)
.all()
)
overdue_checks = [
{
"id": ch.id,
"check_number": ch.check_number,
"amount": float(ch.amount or 0),
"due_date": _iso(ch.due_date),
"status": ch.status.value if ch.status else None,
}
for ch in checks
]
from adapters.db.models.crm import Deal
from app.services.crm_enterprise_service import restrict_visibility_query
deal_query = db.query(Deal).filter(
Deal.business_id == business_id, Deal.person_id == person_id, Deal.closed_at.is_(None)
)
if auth_ctx is not None:
deal_query = restrict_visibility_query(deal_query, db, business_id, auth_ctx, Deal, "deal")
deals = deal_query.order_by(Deal.created_at.desc()).limit(5).all()
open_deals = [
{"id": d.id, "title": d.title, "code": d.code, "amount": float(d.amount or 0)}
for d in deals
]
safe_match = scrub_match_for_viewer(db, business_id, auth_ctx, match if isinstance(match, dict) else {})
safe_call = scrub_calls_for_viewer(db, business_id, auth_ctx, [call_to_dict(call)])[0]
return {
"call": safe_call,
"person": _person_brief(person) if person else safe_match.get("person"),
"lead": _lead_brief(lead_row) if lead_row else safe_match.get("lead"),
"match": safe_match,
"balance": balance,
"recent_documents": recent_docs,
"overdue_checks": overdue_checks,
"open_deals": open_deals,
"settings": {
"post_call_form_required": get_or_create_settings(db, business_id).post_call_form_required,
},
}
# ─── Click to call ───────────────────────────────────────────────────────────
def click_to_call(
db: Session,
business_id: int,
user_id: int,
payload: Dict[str, Any],
) -> Dict[str, Any]:
destination = str(payload.get("destination") or payload.get("number") or "").strip()
if not destination:
raise ApiError("DESTINATION_REQUIRED", "DESTINATION_REQUIRED", http_status=400)
norm = normalize_iran_phone(destination)
dest_canonical = norm["canonical"] or extract_digits(destination)
if not dest_canonical:
raise ApiError("INVALID_DESTINATION", "INVALID_DESTINATION", http_status=400)
links = (
db.query(TelephonyUserExtension)
.options(joinedload(TelephonyUserExtension.extension_row))
.filter(
TelephonyUserExtension.business_id == business_id,
TelephonyUserExtension.user_id == user_id,
TelephonyUserExtension.can_click_to_call == True, # noqa: E712
)
.all()
)
if not links:
raise ApiError("NO_EXTENSION", "NO_EXTENSION", http_status=400)
link = None
wanted_ext_id = payload.get("extension_id")
if wanted_ext_id:
link = next((x for x in links if x.extension_id == int(wanted_ext_id)), None)
if not link:
link = next((x for x in links if x.is_primary), links[0])
ext = link.extension_row
pbx = db.get(TelephonyPbxConnection, link.pbx_id)
if not pbx or not pbx.is_active:
raise ApiError("PBX_OFFLINE", "PBX_OFFLINE", http_status=400)
settings_pbx = pbx.settings or {}
command_id = str(uuid.uuid4())
cmd_payload = {
"extension": ext.extension,
"destination": dest_canonical,
"caller_id": link.caller_id_override or settings_pbx.get("default_caller_id"),
"timeout_ms": int(settings_pbx.get("originate_timeout_ms") or 30000),
"context": settings_pbx.get("originate_context") or "from-internal",
}
uniqueid = f"out-{command_id}"
match = match_caller(db, business_id, dest_canonical)
call = TelephonyCall(
business_id=business_id,
pbx_id=pbx.id,
asterisk_uniqueid=uniqueid,
direction="outbound",
status="ringing",
from_number_raw=ext.extension,
from_number_normalized=ext.extension,
to_number_raw=destination,
to_number_normalized=dest_canonical,
extension=ext.extension,
started_at=_now(),
person_id=payload.get("person_id") or (match.get("person") or {}).get("id"),
lead_id=payload.get("lead_id") or (match.get("lead") or {}).get("id"),
assigned_user_id=user_id,
match_method=match.get("match_method") or ("manual" if payload.get("person_id") else None),
is_anonymous=bool(match.get("is_anonymous")) and not payload.get("person_id"),
extra_info={"originate_command_id": command_id, "match": match},
created_at=_now(),
updated_at=_now(),
)
db.add(call)
db.flush()
cmd = TelephonyCommand(
business_id=business_id,
pbx_id=pbx.id,
command_id=command_id,
command_type="originate",
payload=cmd_payload,
status="pending",
created_by_user_id=user_id,
call_id=call.id,
created_at=_now(),
expires_at=_now() + timedelta(minutes=2),
)
db.add(cmd)
db.commit()
db.refresh(call)
return {
"ok": True,
"command_id": command_id,
"call": call_to_dict(call),
"message": "دستور شماره‌گیری برای Connector ارسال شد.",
}
def poll_commands(db: Session, pbx: TelephonyPbxConnection, limit: int = 10) -> List[Dict[str, Any]]:
now = _now()
rows = (
db.query(TelephonyCommand)
.filter(
TelephonyCommand.pbx_id == pbx.id,
TelephonyCommand.status == "pending",
or_(TelephonyCommand.expires_at.is_(None), TelephonyCommand.expires_at > now),
)
.order_by(TelephonyCommand.created_at.asc())
.limit(min(limit, 50))
.all()
)
out = []
for row in rows:
row.status = "sent"
out.append(
{
"command_id": row.command_id,
"type": row.command_type,
**row.payload,
"call_id": row.call_id,
}
)
db.commit()
return out
def ack_command(db: Session, pbx: TelephonyPbxConnection, command_id: str, payload: Dict[str, Any]) -> Dict[str, Any]:
row = (
db.query(TelephonyCommand)
.filter(TelephonyCommand.pbx_id == pbx.id, TelephonyCommand.command_id == command_id)
.first()
)
if not row:
raise ApiError("COMMAND_NOT_FOUND", "COMMAND_NOT_FOUND", http_status=404)
ok = bool(payload.get("accepted", True))
row.status = "acked" if ok else "failed"
row.result_payload = payload
row.acked_at = _now()
db.commit()
return {"ok": True, "command_id": command_id, "status": row.status}
# ─── Create person / lead from call ──────────────────────────────────────────
def create_person_from_call(
db: Session, business_id: int, call_id: int, payload: Dict[str, Any], user_id: int, *, auth_ctx=None
) -> Dict[str, Any]:
if auth_ctx is not None:
call = require_call_in_scope(db, auth_ctx, business_id, call_id)
else:
call = (
db.query(TelephonyCall)
.filter(TelephonyCall.id == call_id, TelephonyCall.business_id == business_id)
.first()
)
if not call:
raise ApiError("CALL_NOT_FOUND", "CALL_NOT_FOUND", http_status=404)
number = call.from_number_normalized if call.direction == "inbound" else call.to_number_normalized
number = number or call.from_number_raw or call.to_number_raw
digits = extract_digits(number or "")
mobile_val = None
phone_val = payload.get("phone")
if len(digits) >= 10 and digits[-10:].startswith("9"):
mobile_val = "0" + digits[-10:]
elif number:
phone_val = phone_val or number
# کد بعدی شخص
max_code = (
db.query(func.max(Person.code)).filter(Person.business_id == business_id).scalar()
)
next_code = int(max_code or 0) + 1
person = Person(
business_id=business_id,
code=next_code,
alias_name=(payload.get("alias_name") or payload.get("name") or number or "مخاطب تلفنی").strip(),
first_name=payload.get("first_name"),
last_name=payload.get("last_name"),
company_name=payload.get("company_name"),
mobile=mobile_val or payload.get("mobile"),
phone=phone_val,
person_types=json.dumps(["مشتری"], ensure_ascii=False),
created_at=_now(),
updated_at=_now(),
)
db.add(person)
db.flush()
call.person_id = person.id
call.is_anonymous = False
call.match_method = "created_person"
call.updated_at = _now()
_remember_call_match(call, person=person)
db.commit()
db.refresh(call)
return {"call": call_to_dict(call), "person": _person_brief(person)}
def create_lead_from_call(
db: Session, business_id: int, call_id: int, payload: Dict[str, Any], user_id: int, *, auth_ctx=None
) -> Dict[str, Any]:
from adapters.db.models.crm import CrmProcessDefinition, CrmProcessStage
if auth_ctx is not None:
call = require_call_in_scope(db, auth_ctx, business_id, call_id)
else:
call = (
db.query(TelephonyCall)
.filter(TelephonyCall.id == call_id, TelephonyCall.business_id == business_id)
.first()
)
if not call:
raise ApiError("CALL_NOT_FOUND", "CALL_NOT_FOUND", http_status=404)
process = (
db.query(CrmProcessDefinition)
.filter(
CrmProcessDefinition.business_id == business_id,
CrmProcessDefinition.process_type == "lead_funnel",
CrmProcessDefinition.is_active == True, # noqa: E712
)
.order_by(CrmProcessDefinition.is_default.desc(), CrmProcessDefinition.id.asc())
.first()
)
if not process:
raise ApiError("NO_LEAD_FUNNEL", "NO_LEAD_FUNNEL", http_status=400)
stage = (
db.query(CrmProcessStage)
.filter(CrmProcessStage.process_definition_id == process.id)
.order_by(CrmProcessStage.order_index.asc())
.first()
)
if not stage:
raise ApiError("NO_LEAD_STAGE", "NO_LEAD_STAGE", http_status=400)
if call.lead_id:
existing = db.get(Lead, call.lead_id)
if existing and existing.business_id == business_id:
return {"call": call_to_dict(call), "lead": _lead_brief(existing)}
from datetime import date
from app.services.document_numbering_service import generate_document_code
number = _peer_canonical_phone(call)
name = (payload.get("name") or number or "سرنخ تلفنی").strip()
lead = Lead(
business_id=business_id,
process_definition_id=process.id,
stage_id=stage.id,
code=generate_document_code(db, business_id, "crm_lead", date.today()),
name=name,
company_name=payload.get("company_name"),
mobile=number,
source_code=payload.get("source_code") or "telephony",
description=payload.get("description") or call.note,
assigned_to_user_id=payload.get("assigned_to_user_id") or user_id,
created_by_user_id=user_id,
created_at=_now(),
updated_at=_now(),
)
db.add(lead)
db.flush()
call.lead_id = lead.id
call.is_anonymous = False
call.match_method = "created_lead"
call.updated_at = _now()
_remember_call_match(call, lead=lead)
db.commit()
db.refresh(call)
try:
from app.services.workflow.workflow_trigger_service import trigger_lead_created
trigger_lead_created(
db,
business_id,
lead_id=lead.id,
process_definition_id=lead.process_definition_id,
stage_id=lead.stage_id,
name=lead.name,
user_id=user_id,
)
except Exception:
pass
return {"call": call_to_dict(call), "lead": _lead_brief(lead)}
def dashboard_summary(db: Session, business_id: int, user_id: Optional[int] = None, auth_ctx=None) -> Dict[str, Any]:
today_start = datetime.utcnow().replace(hour=0, minute=0, second=0, microsecond=0)
scope = resolve_scope(db, auth_ctx, business_id) if auth_ctx is not None else None
base = db.query(TelephonyCall).filter(
TelephonyCall.business_id == business_id, TelephonyCall.started_at >= today_start
)
if scope is not None:
base = scope.apply_call_filter(base)
inbound = base.filter(TelephonyCall.direction == "inbound").count()
outbound = base.filter(TelephonyCall.direction == "outbound").count()
answered = base.filter(TelephonyCall.status.in_(["answered", "completed"])).count()
missed = base.filter(TelephonyCall.status == "missed").count()
active_q = db.query(TelephonyCall).filter(
TelephonyCall.business_id == business_id,
TelephonyCall.status.in_(["ringing", "answered"]),
TelephonyCall.ended_at.is_(None),
)
if scope is not None:
active_q = scope.apply_call_filter(active_q)
active = active_q.count()
include_token = bool(scope.can_manage) if scope else False
pbx_rows = list_pbx(db, business_id, include_token_hint=include_token)
my_missed = 0
uid = user_id or (scope.user_id if scope else None)
if uid:
my_missed = (
base.filter(TelephonyCall.assigned_user_id == uid, TelephonyCall.status == "missed").count()
)
return {
"today": {
"inbound": inbound,
"outbound": outbound,
"answered": answered,
"missed": missed,
"my_missed": my_missed,
},
"active_calls": active,
"pbx_connections": pbx_rows,
"access": scope.to_dict() if scope else None,
}