feat(connect): caller ID as an integration: calls on the signed-in live connection, matched on the server

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>
This commit is contained in:
2026-11-16 09:06:39 +02:00
co-authored by Claude Opus 5.5
parent 88a9703902
commit dabe807ce0
28 changed files with 609 additions and 501 deletions
+59 -88
View File
@@ -1,96 +1,67 @@
import { Phone, PhoneIncoming, ChevronRight, Wifi, WifiOff } from 'lucide-react'
// Integrations → Caller ID (plan step 3.5): the call log (GET /api/phone/calls, kept on the
// server, so it survives a reload and shows calls taken while the manager was closed), new calls
// live, a tap opens the caller's card; and how to connect the phone system (the UCM Action URL).
import { useCallback, useEffect, useState } from 'react'
import { PhoneIncoming } from 'lucide-react'
import { errorMessage } from '@xenia/api'
import { useFormat, useTranslation } from '@xenia/i18n'
import { EmptyState, Spinner } from '@xenia/ui'
import client from '../api/client'
import { useLiveEvent } from '../live/useLiveEvent'
import usePhoneStore from '../store/phoneStore'
function formatTime(date) {
return date.toLocaleTimeString('el-GR', { hour: '2-digit', minute: '2-digit' })
}
export default function PhonePage() {
const { log, setActiveCall } = usePhoneStore()
const { t } = useTranslation('manager')
const { formatDateTime } = useFormat()
const setActiveCall = usePhoneStore(s => s.setActiveCall)
const [calls, setCalls] = useState(null)
const [error, setError] = useState(null)
const load = useCallback(async () => {
try { setCalls((await client.get('/api/phone/calls', { params: { limit: 100 } })).data.calls); setError(null) } catch (err) { setError(errorMessage(err, t)) }
}, [t])
useEffect(() => { load() }, [load])
useLiveEvent('call_incoming', load)
useLiveEvent('ready', load)
const hook = `${window.location.protocol}//${window.location.hostname}/api/phone/call-event?caller=\${CALLERID}&ext=\${EXTEN}`
return (
<div style={{ padding: 32, fontFamily: "'DM Sans','Segoe UI',sans-serif", maxWidth: 680, margin: '0 auto' }}>
<div style={{ display: 'flex', alignItems: 'center', gap: 12, marginBottom: 28 }}>
<div style={{
width: 40, height: 40, borderRadius: 10,
background: 'linear-gradient(135deg,#064e3b,#065f46)',
border: '1px solid rgba(52,211,153,0.2)',
display: 'flex', alignItems: 'center', justifyContent: 'center',
}}>
<Phone size={18} color="#34d399" />
</div>
<div>
<h1 style={{ fontSize: 16, fontWeight: 700, color: '#0f172a', margin: 0, letterSpacing: '-0.01em' }}>
Εισερχόμενες Κλήσεις
</h1>
<p style={{ fontSize: 12, color: '#94a3b8', margin: '2px 0 0' }}>Grandstream UCM · Caller ID</p>
</div>
</div>
<div style={{ background: '#fff', border: '1px solid #e2e8f0', borderRadius: 16, overflow: 'hidden' }}>
<div style={{
padding: '12px 16px', borderBottom: '1px solid #f1f5f9',
display: 'flex', alignItems: 'center', justifyContent: 'space-between',
}}>
<span style={{ fontSize: 11, fontWeight: 600, color: '#94a3b8', textTransform: 'uppercase', letterSpacing: '0.08em' }}>
Πρόσφατες Κλήσεις
</span>
{log.length > 0 && (
<span style={{ fontSize: 11, color: '#cbd5e1' }}>{log.length} κλήσεις</span>
)}
</div>
{log.length === 0 ? (
<div style={{ padding: '48px 16px', textAlign: 'center', color: '#cbd5e1', fontSize: 13 }}>
Δεν έχουν ληφθεί κλήσεις ακόμα.
</div>
) : (
<ul style={{ listStyle: 'none', margin: 0, padding: 0 }}>
{log.map((entry, i) => (
<li
key={i}
onClick={() => setActiveCall(entry)}
style={{
display: 'flex', alignItems: 'center', gap: 12,
padding: '12px 16px',
borderBottom: i < log.length - 1 ? '1px solid #f8fafc' : 'none',
cursor: 'pointer', transition: 'background 0.1s',
}}
onMouseEnter={e => { e.currentTarget.style.background = '#f8fafc' }}
onMouseLeave={e => { e.currentTarget.style.background = 'transparent' }}
>
<div style={{
width: 34, height: 34, borderRadius: 10, flexShrink: 0,
background: entry.customer ? '#fef3c7' : '#f1f5f9',
display: 'flex', alignItems: 'center', justifyContent: 'center',
}}>
{entry.customer
? <span style={{ fontSize: 14, fontWeight: 700, color: '#d97706' }}>
{entry.customer.name?.[0]?.toUpperCase()}
</span>
: <PhoneIncoming size={14} color="#94a3b8" />
}
</div>
<div style={{ flex: 1, minWidth: 0 }}>
<div style={{ fontSize: 13, fontWeight: 600, color: '#1e293b' }}>
{entry.customer ? entry.customer.name : entry.caller || '—'}
</div>
<div style={{ fontSize: 11, color: '#94a3b8', marginTop: 1 }}>
{entry.customer ? entry.caller : 'Άγνωστος'}
</div>
</div>
<div style={{ display: 'flex', alignItems: 'center', gap: 6 }}>
<span style={{ fontSize: 11, color: '#cbd5e1', fontVariantNumeric: 'tabular-nums' }}>
{formatTime(entry.at)}
</span>
<ChevronRight size={12} color="#cbd5e1" />
</div>
</li>
))}
</ul>
)}
</div>
<div className="p-4 md:p-6 grid gap-6 max-w-3xl" data-testid="phone-page">
<header className="grid gap-1">
<h1 className="text-heading font-semibold text-ink">{t('calls.title')}</h1>
<p className="text-body text-ink-2">{t('calls.intro')}</p>
</header>
<section className="rounded-card border border-line bg-surface overflow-hidden" aria-labelledby="calls-h">
<h2 id="calls-h" className="px-4 py-3 border-b border-line text-label font-semibold text-ink-2">{t('calls.log')}</h2>
{error ? <div className="p-6"><EmptyState title={error} /></div>
: calls == null ? <div className="p-6 flex justify-center"><Spinner /></div>
: calls.length === 0 ? <div className="p-6"><EmptyState icon={<PhoneIncoming size={28} />} title={t('calls.none')} /></div>
: (
<ul className="divide-y divide-line" data-testid="call-log">
{calls.map(c => (
<li key={c.call_id}>
<button type="button" onClick={() => setActiveCall(c)} data-testid="call-row"
className="w-full px-4 py-3 flex items-center gap-3 text-left hover:bg-surface-2 transition-colors duration-fast">
<span className="size-9 rounded-full bg-surface-2 text-ink-2 flex items-center justify-center shrink-0 font-semibold">
{c.customer ? c.customer.name?.[0]?.toUpperCase() : <PhoneIncoming size={14} aria-hidden="true" />}
</span>
<span className="flex-1 min-w-0">
<span className="block text-body font-medium text-ink truncate">{c.customer ? c.customer.name : c.number}</span>
<span className="block text-label text-ink-2">{c.customer ? c.number : t('calls.unknown')}{c.ext ? ` · ${t('calls.ext', { ext: c.ext })}` : ''}</span>
</span>
<span className="text-label text-ink-3">{formatDateTime(c.at, 'datetimeShort')}</span>
</button>
</li>
))}
</ul>
)}
</section>
<section className="p-4 rounded-card border border-line bg-surface-2 grid gap-1.5">
<h2 className="text-label font-semibold text-ink">{t('calls.setup')}</h2>
<p className="text-label text-ink-2">{t('calls.setupBody')}</p>
<code className="text-label text-ink break-all bg-surface p-2 rounded-control border border-line">{hook}</code>
<p className="text-caption text-ink-3">{t('calls.setupSecret')}</p>
</section>
</div>
)
}