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
+44
View File
@@ -0,0 +1,44 @@
// Caller ID at the counter (plan step 3.5): the venue's phone rings → the server matches the
// customer and sends `call_incoming` to the counter screens (addressed to the devices). A popup
// shows who is calling (their card and last orders, or the number) with "New takeaway sale"
// (the usual reason a regular calls). Calls older than a minute (a reconnect replays events)
// don't pop up.
import { PhoneIncoming, ShoppingBag } from 'lucide-react'
import { useFormat, useTranslation } from '@xenia/i18n'
import { Button, Dialog } from '@xenia/ui'
export default function CallPopup({ call, onClose, onTakeaway }) {
const { t } = useTranslation('terminal')
const { formatMoney, formatDate } = useFormat()
const c = call.customer
return (
<Dialog open size="sm" onClose={onClose} title={t('pos.call.title')}
footer={<>
<Button variant="secondary" onClick={onClose} data-testid="call-close">{t('pos.dialog.close')}</Button>
<Button icon={<ShoppingBag size={16} />} onClick={onTakeaway} data-testid="call-takeaway">{t('pos.call.takeaway')}</Button>
</>}>
<div className="grid gap-3" data-testid="call-popup" data-call={call.call_id}>
<div className="flex items-center gap-3">
<span className="size-12 rounded-full bg-accent-soft text-accent-ink flex items-center justify-center shrink-0 text-title font-semibold">
{c ? c.name?.[0]?.toUpperCase() : <PhoneIncoming size={20} aria-hidden="true" />}
</span>
<div className="min-w-0">
<p className="text-heading font-semibold truncate" data-testid="call-name">{c ? c.name : t('pos.call.unknown')}</p>
<p className="text-label text-ink-2">{call.number}{c?.nickname ? ` · ${c.nickname}` : ''}</p>
</div>
</div>
{c?.notes && <p className="p-2 rounded-control bg-warning-soft text-warning-ink text-body">“{c.notes}”</p>}
{c && (
<div className="grid gap-1">
<p className="text-label text-ink-2">{t('pos.call.visits', { count: c.visit_count, spent: formatMoney(c.total_spent) })}</p>
{c.recent_orders.map(o => (
<div key={o.id} className="flex justify-between gap-2 p-2 rounded-control bg-surface-2 text-body">
<span>{formatDate(o.opened_at)}{o.table_name ? ` · ${o.table_name}` : ''}</span><span>{formatMoney(o.total)}</span>
</div>
))}
</div>
)}
</div>
</Dialog>
)
}
+15
View File
@@ -22,6 +22,8 @@ import { addLine, categoriesWithProducts, forSalesPoint, salesPointFor, defaultS
import { findOnline, ONLINE_LISTS } from './logic/online'
import { BOOKING_LIST, findBooking } from './logic/bookings'
import BookingPanel from './BookingPanel'
import CallPopup from './CallPopup'
import { isFresh } from './logic/calls'
import BookingsView from './BookingsView'
import { zoneCounts } from './logic/tables'
import OnlinePanel from './OnlinePanel'
@@ -79,6 +81,14 @@ function Counter({ menu, t }) {
ring()
if (mode !== 'online') toast.info(t('pos.online.arrived', { count: data?.count ?? 1 }))
})
// caller ID (3.5): the venue's phone rings; the server says who it is
const callerOn = useModule('caller_id')
const [call, setCall] = useState(null)
useLiveEvent('call_incoming', data => {
if (!callerOn || !isFresh(data)) return
ring()
setCall(data)
})
useLiveEvent('reservation_requested', data => {
if (!onlineOn) return
ring()
@@ -226,6 +236,11 @@ function Counter({ menu, t }) {
</>
)}
{call && (
<CallPopup call={call} onClose={() => setCall(null)}
onTakeaway={() => { setCall(null); setMode('sale'); leaveTable(); setOrderType('takeaway') }} />
)}
{dialog && (
<ProductDialog product={dialog} orderType={atTable ? 'here' : orderType} onClose={() => setDialog(null)}
onAdd={line => setCart(c => addLine(c, line))} />
+8
View File
@@ -0,0 +1,8 @@
// Caller ID at the counter (plan step 3.5). Pure: unit-tested in Node (tests/pos.test.mjs).
export const FRESH_MS = 60_000
/** Should this call pop up now? (A reconnect replays events: old calls stay quiet.) */
export function isFresh(call, now = Date.now()) {
return !!call && (!call.at || now - Date.parse(call.at) < FRESH_MS)
}