Plan step 3.5.
- The public /api/phone/ws socket is removed (anyone on the LAN could listen to every
caller). GET /api/phone/call-event stays public for the Grandstream UCM but is LAN-only in
the proxy (the 443 blocks' LAN-only location now covers it, in nginx-proxy/nginx.conf and
install.sh), with an optional shared secret (setting caller_id.secret → &key=).
- Calls are logged (phone_calls, migration 0023), matched to a customer on the server
(services/caller_id.py: Greek number rules moved from the manager) and published as
`call_incoming` {call_id, at, number, ext, customer card: visits, spent, last 3 orders} to
users with caller_id.use and to the counter POS screens. GET /api/phone/calls (the log),
GET /api/customers/lookup?phone=. Calls older than a minute don't pop up (replays).
- Manager: PhoneListener on the live connection and the shared Dialog (no more downloading
every customer to match); Integrations → Caller ID shows the server's log live and the UCM
Action URL. Counter: CallPopup with "New takeaway sale".
- Tests: tests/test_caller_id.py (10), terminal unit test, e2e terminal and manager.
Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
82 lines
3.6 KiB
Python
82 lines
3.6 KiB
Python
"""Caller ID: incoming calls from the venue's phone system (Grandstream UCM), plan step 3.5.
|
|
|
|
GET /api/phone/call-event?caller=…&ext=…[&key=…] the UCM's "Action URL" on every incoming call.
|
|
No sign-in (the phone system has none), so: LAN-only in the proxy (nginx-proxy/nginx.conf and
|
|
the copy in install.sh), and an optional shared secret (setting caller_id.secret; when set,
|
|
`key` must match). The call is logged (phone_calls), matched to a customer on the server
|
|
(services/caller_id.py), and published on the main live connection as `call_incoming`
|
|
{call_id, at, number, ext, customer} to the users holding caller_id.use and to the counter
|
|
POS screens. Nothing on the LAN can listen without signing in (the old public
|
|
/api/phone/ws socket is gone).
|
|
GET /api/phone/calls?limit= the call log (caller_id.use), newest first.
|
|
"""
|
|
from __future__ import annotations
|
|
|
|
import secrets
|
|
from typing import Optional
|
|
|
|
from fastapi import APIRouter, Depends, Query
|
|
from sqlalchemy.orm import Session
|
|
|
|
from core.access.deps import public, require
|
|
from core.errors import XeniaError
|
|
from database import get_db
|
|
|
|
router = APIRouter()
|
|
KEY = "caller_id.use"
|
|
SECRET = "caller_id.secret"
|
|
|
|
|
|
def _setting(db, key: str) -> str:
|
|
from models.settings import PosSettings
|
|
row = db.query(PosSettings).filter(PosSettings.key == key).first()
|
|
return (row.value or "") if row else ""
|
|
|
|
|
|
def call_out(db, call, customer=None) -> dict:
|
|
from services import caller_id
|
|
from models.customers import Customer
|
|
if customer is None and call.customer_id:
|
|
customer = db.get(Customer, call.customer_id)
|
|
return {"call_id": call.id, "at": call.at.isoformat() if call.at else None, "number": call.number, "ext": call.ext,
|
|
"customer": caller_id.card(db, customer) if customer is not None else None}
|
|
|
|
|
|
def _audience(db) -> tuple[list[int], list[int]]:
|
|
from core.access.queries import users_with_key
|
|
from models.device import Device
|
|
users = [u.id for u in users_with_key(db, KEY)]
|
|
pos = [i for (i,) in db.query(Device.id).filter(Device.app_key == "pos_counter", Device.revoked_at.is_(None))]
|
|
return users, pos
|
|
|
|
|
|
@router.get("/call-event", dependencies=[Depends(public())])
|
|
def call_event(caller: str = Query(..., max_length=40, description="Caller ID number from the UCM"),
|
|
ext: str = Query("", max_length=20, description="Dialled extension"),
|
|
key: Optional[str] = Query(None, max_length=200),
|
|
db: Session = Depends(get_db)):
|
|
"""Example: GET /api/phone/call-event?caller=2106001234&ext=100"""
|
|
from models.phone_call import PhoneCall
|
|
from services import caller_id
|
|
from services.ws_bus import broadcast_sync
|
|
secret = _setting(db, SECRET)
|
|
if secret and not (key and secrets.compare_digest(key, secret)):
|
|
raise XeniaError("caller_id.key_invalid")
|
|
customer = caller_id.match(db, caller)
|
|
call = PhoneCall(number=caller.strip(), ext=ext.strip() or None, customer_id=customer.id if customer else None)
|
|
db.add(call)
|
|
db.commit()
|
|
db.refresh(call)
|
|
payload = call_out(db, call, customer)
|
|
users, pos = _audience(db)
|
|
if users or pos:
|
|
broadcast_sync("call_incoming", payload, user_ids=users, device_ids=pos)
|
|
return {"status": "ok", "call_id": call.id, "matched": customer is not None}
|
|
|
|
|
|
@router.get("/calls")
|
|
def call_log(limit: int = Query(50, ge=1, le=500), db: Session = Depends(get_db), user=Depends(require(KEY))):
|
|
from models.phone_call import PhoneCall
|
|
calls = db.query(PhoneCall).order_by(PhoneCall.at.desc(), PhoneCall.id.desc()).limit(limit).all()
|
|
return {"calls": [call_out(db, c) for c in calls]}
|