feat(cloud): Alembic, license token v2 with modules, plan presets, module editor

Platform v2 step 1.7 (APPS_PLATFORM D-3, D-20, D-21, D-22, §5.2).
- Alembic replaces create_all + _run_migrations. 0001_cloud_baseline is
  exactly the v1 production schema (incl. the dead menu_hours_en/_gr), checked
  against a frozen fixture; schema.py refuses an unstamped v1 DB, so prod is
  stamped once at cutover (DEPLOYMENT_GUIDE Part 6)
- 0002_v2_licensing: sites.modules (JSON), sites.module_catalog(_at),
  plan_presets; existing sites get every module v1 had (KDS unlimited); the
  same map is seeded as preset "Everything (v1 sites)"; dead columns dropped
- license token v: 2 with a normalized `modules` map
- heartbeat v2: request {version, local_ip, module_catalog} (catalogue stored
  per site); response {license_token, latest_version, waiter_domain,
  site_numeric_id} - the unsigned v1 license fields are gone
- sites API: create with a preset, PUT /{id}/modules (validated),
  POST /{id}/apply-preset/{preset}; /api/presets CRUD
- sysadmin panel: Licensed modules card per site (built from the reported
  catalogue, fallback list), preset picker on register, Presets page
- .dockerignore: local *.db, .env and tests no longer baked into the image
- tests (new): baseline = v1, unstamped refused, stamp+upgrade of a v1 DB,
  drift, token v2 shape/signature, module edits, presets; CL-2/CL-7 rules

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
This commit is contained in:
2026-11-12 19:27:47 +02:00
co-authored by Claude Opus 5.5
parent 048b46af41
commit 48fd10df77
30 changed files with 1306 additions and 61 deletions
+12 -3
View File
@@ -4,6 +4,7 @@ import LoginPage from './pages/LoginPage'
import SitesPage from './pages/SitesPage'
import SiteDetailPage from './pages/SiteDetailPage'
import RegisterSitePage from './pages/RegisterSitePage'
import PresetsPage from './pages/PresetsPage'
function Layout({ children }) {
const { logout } = useAuthStore()
@@ -32,11 +33,16 @@ function Layout({ children }) {
</button>
<div className="flex items-center gap-4">
<span className={`text-xs px-2.5 py-1 rounded font-mono ${
location.pathname.startsWith('/sites') ? 'text-cyan-400 bg-cyan-500/10' : 'text-gray-500'
<button onClick={() => navigate('/sites')} className={`text-xs px-2.5 py-1 rounded font-mono ${
location.pathname.startsWith('/sites') ? 'text-cyan-400 bg-cyan-500/10' : 'text-gray-500 hover:text-gray-300'
}`}>
Sites
</span>
</button>
<button onClick={() => navigate('/presets')} className={`text-xs px-2.5 py-1 rounded font-mono ${
location.pathname.startsWith('/presets') ? 'text-cyan-400 bg-cyan-500/10' : 'text-gray-500 hover:text-gray-300'
}`}>
Presets
</button>
<button
onClick={handleLogout}
className="text-xs text-gray-500 hover:text-gray-300 transition-colors"
@@ -75,6 +81,9 @@ export default function App() {
<Route path="/sites/:siteId" element={
<RequireAuth><Layout><SiteDetailPage /></Layout></RequireAuth>
} />
<Route path="/presets" element={
<RequireAuth><Layout><PresetsPage /></Layout></RequireAuth>
} />
<Route path="*" element={<Navigate to="/sites" replace />} />
</Routes>
)
@@ -0,0 +1,99 @@
// Edits a license module map: { key: { limits?: {name: int|null}, exp?: ISO } } (APPS_PLATFORM §5.1).
// Core modules are always licensed and aren't shown. A limit field left empty uses the
// module's licensed default; "∞" sends null (unlimited). Expiry is optional, per module.
import { KIND_LABELS } from '../moduleCatalog'
function LimitInput({ limit, value, onChange }) {
const unlimited = value === null
const shown = value === undefined || value === null ? '' : String(value)
const fallback = limit.licensed_default === null || limit.licensed_default === undefined ? '∞' : limit.licensed_default
return (
<label className="flex items-center gap-1.5 text-xs text-gray-400">
<span className="font-mono">{limit.key}</span>
<input
type="number" min="0" disabled={unlimited} value={shown} placeholder={`default ${fallback}`}
onChange={e => onChange(e.target.value === '' ? undefined : Math.max(0, parseInt(e.target.value, 10) || 0))}
className="w-24 bg-gray-800 border border-gray-700 rounded px-2 py-1 text-gray-200 disabled:opacity-40"
/>
<button type="button" onClick={() => onChange(unlimited ? undefined : null)}
className={`px-1.5 py-0.5 rounded border ${unlimited ? 'border-cyan-500 text-cyan-300' : 'border-gray-700 text-gray-500'}`}
title="Unlimited">∞</button>
</label>
)
}
export default function ModuleMapEditor({ catalog, value, onChange }) {
const map = value || {}
const groups = ['feature', 'app', 'integration']
function setEntry(key, entry) {
const next = { ...map }
if (entry === undefined) delete next[key]
else next[key] = entry
onChange(next)
}
function setLimit(key, name, v) {
const entry = { ...(map[key] || {}) }
const limits = { ...(entry.limits || {}) }
if (v === undefined) delete limits[name]
else limits[name] = v
if (Object.keys(limits).length) entry.limits = limits
else delete entry.limits
setEntry(key, entry)
}
function setExp(key, date) {
const entry = { ...(map[key] || {}) }
if (date) entry.exp = `${date}T23:59:59+00:00`
else delete entry.exp
setEntry(key, entry)
}
return (
<div className="space-y-4">
{groups.map(kind => {
const mods = catalog.filter(m => m.kind === kind)
if (!mods.length) return null
return (
<div key={kind}>
<div className="text-[11px] uppercase tracking-wider text-gray-500 mb-2">{KIND_LABELS[kind]}</div>
<div className="space-y-1.5">
{mods.map(m => {
const on = m.key in map
const entry = map[m.key] || {}
return (
<div key={m.key} className={`rounded-lg border px-3 py-2 ${on ? 'border-cyan-800 bg-cyan-950/30' : 'border-gray-800'}`}>
<div className="flex items-center justify-between gap-3 flex-wrap">
<label className="flex items-center gap-2 text-sm text-gray-200 cursor-pointer">
<input type="checkbox" checked={on} onChange={e => setEntry(m.key, e.target.checked ? {} : undefined)} />
<span>{m.name}</span>
<span className="font-mono text-[11px] text-gray-500">{m.key}</span>
</label>
{on && (
<label className="flex items-center gap-1.5 text-xs text-gray-400">
expires
<input type="date" value={entry.exp ? entry.exp.slice(0, 10) : ''}
onChange={e => setExp(m.key, e.target.value)}
className="bg-gray-800 border border-gray-700 rounded px-2 py-1 text-gray-200" />
</label>
)}
</div>
{on && (m.limits || []).length > 0 && (
<div className="flex gap-4 flex-wrap mt-2 pl-6">
{m.limits.map(l => (
<LimitInput key={l.key} limit={l} value={entry.limits?.[l.key]}
onChange={v => setLimit(m.key, l.key, v)} />
))}
</div>
)}
</div>
)
})}
</div>
</div>
)
})}
</div>
)
}
@@ -0,0 +1,86 @@
// Licensed modules of one site (APPS_PLATFORM D-3, §5.2): apply a preset, then edit per site.
// The site receives the map in its next heartbeat, inside the signed v2 license token.
import { useEffect, useState } from 'react'
import toast from 'react-hot-toast'
import client from '../api/client'
import { FALLBACK_CATALOG } from '../moduleCatalog'
import ModuleMapEditor from './ModuleMapEditor'
export default function SiteModulesCard({ site, onSaved }) {
const [map, setMap] = useState(site.modules || {})
const [presets, setPresets] = useState([])
const [presetId, setPresetId] = useState('')
const [saving, setSaving] = useState(false)
const reported = Array.isArray(site.module_catalog) && site.module_catalog.length > 0
const catalog = reported ? site.module_catalog : FALLBACK_CATALOG
const dirty = JSON.stringify(map) !== JSON.stringify(site.modules || {})
useEffect(() => { setMap(site.modules || {}) }, [site])
useEffect(() => {
client.get('/api/presets/').then(r => setPresets(r.data)).catch(() => {})
}, [])
async function save() {
setSaving(true)
try {
const { data } = await client.put(`/api/sites/${site.site_id}/modules`, { modules: map })
onSaved(data)
toast.success('Modules saved — the site picks them up on its next heartbeat')
} catch (e) {
toast.error(e.response?.data?.detail || 'Could not save modules')
} finally {
setSaving(false)
}
}
async function applyPreset() {
if (!presetId) return
setSaving(true)
try {
const { data } = await client.post(`/api/sites/${site.site_id}/apply-preset/${presetId}`)
onSaved(data)
toast.success('Preset applied')
} catch {
toast.error('Could not apply preset')
} finally {
setSaving(false)
}
}
return (
<div className="bg-gray-900 border border-gray-700 rounded-xl p-4 mb-4">
<div className="flex items-center justify-between mb-1 gap-3 flex-wrap">
<h2 className="text-xs font-semibold text-gray-500 uppercase tracking-wider">Licensed modules</h2>
<div className="flex items-center gap-2">
<select value={presetId} onChange={e => setPresetId(e.target.value)}
className="bg-gray-800 border border-gray-700 rounded px-2 py-1 text-xs text-gray-200">
<option value="">Preset…</option>
{presets.map(p => <option key={p.id} value={p.id}>{p.name}</option>)}
</select>
<button onClick={applyPreset} disabled={!presetId || saving}
className="text-xs px-2.5 py-1 rounded border border-gray-700 text-gray-300 hover:border-cyan-600 disabled:opacity-40">
Apply
</button>
</div>
</div>
<p className="text-xs text-gray-500 mb-3">
{reported
? `Catalogue reported by the site${site.module_catalog_at ? ` (${new Date(site.module_catalog_at).toLocaleString()})` : ''}.`
: 'The site has not reported its catalogue yet — showing the standard list.'}{' '}
Core and catalogue are always included. Unlicensed modules fall back to the free tier.
</p>
<ModuleMapEditor catalog={catalog} value={map} onChange={setMap} />
<div className="flex justify-end gap-2 mt-4">
{dirty && (
<button onClick={() => setMap(site.modules || {})} className="text-xs px-3 py-1.5 text-gray-400 hover:text-gray-200">
Discard
</button>
)}
<button onClick={save} disabled={!dirty || saving}
className="text-xs px-3 py-1.5 rounded bg-cyan-600 text-white hover:bg-cyan-500 disabled:opacity-40">
{saving ? 'Saving…' : 'Save modules'}
</button>
</div>
</div>
)
}
+33
View File
@@ -0,0 +1,33 @@
// Fallback module catalogue (APPS_PLATFORM §4.2) for sites that haven't reported theirs yet
// and for editing presets. A site's own catalogue (heartbeat `module_catalog`, D-20) always
// wins, because it lists exactly what that site's code can run.
// Keep in step with client-services/local_backend/modules/*/manifest.py.
export const FALLBACK_CATALOG = [
{ key: 'core', kind: 'core', name: 'Core', limits: [] },
{ key: 'catalog', kind: 'core', name: 'Catalogue', limits: [] },
{ key: 'history', kind: 'feature', name: 'Order history', limits: [{ key: 'history_days', free: 31, licensed_default: null }] },
{ key: 'ordering', kind: 'feature', name: 'Ordering', limits: [] },
{ key: 'reports', kind: 'feature', name: 'Reports', limits: [{ key: 'history_days', free: 0, licensed_default: null }] },
{ key: 'pricing', kind: 'feature', name: 'Pricing', limits: [] },
{ key: 'tabs', kind: 'feature', name: 'Tabs', limits: [] },
{ key: 'crm', kind: 'feature', name: 'Customers', limits: [] },
{ key: 'expenses', kind: 'feature', name: 'Expenses', limits: [] },
{ key: 'inventory', kind: 'feature', name: 'Inventory', limits: [] },
{ key: 'staff_planning', kind: 'feature', name: 'Staff planning', limits: [] },
{ key: 'notes', kind: 'feature', name: 'Notes', limits: [] },
{ key: 'chat', kind: 'feature', name: 'Staff chat', limits: [] },
{ key: 'reservations', kind: 'feature', name: 'Reservations', limits: [] },
{ key: 'connect_menu', kind: 'feature', name: 'QR menu', limits: [] },
{ key: 'connect_orders', kind: 'feature', name: 'Online orders', limits: [] },
{ key: 'connect_remote', kind: 'feature', name: 'Remote manager', limits: [] },
{ key: 'waiter_app', kind: 'app', name: 'Waiter app', limits: [] },
{ key: 'kds', kind: 'app', name: 'Kitchen display', limits: [{ key: 'devices', free: 0, licensed_default: 1 }] },
{ key: 'pos_counter', kind: 'app', name: 'Counter POS', limits: [{ key: 'devices', free: 0, licensed_default: 1 }] },
{ key: 'customer_display', kind: 'app', name: 'Customer display', limits: [{ key: 'devices', free: 0, licensed_default: 1 }] },
{ key: 'signage', kind: 'app', name: 'Digital signage', limits: [{ key: 'devices', free: 0, licensed_default: 1 }] },
{ key: 'menu_tablet', kind: 'app', name: 'Menu tablet', limits: [{ key: 'devices', free: 0, licensed_default: 1 }] },
{ key: 'caller_id', kind: 'integration', name: 'Caller ID', limits: [] },
{ key: 'fiscal', kind: 'integration', name: 'Fiscal device', limits: [] },
]
export const KIND_LABELS = { feature: 'Features', app: 'Apps', integration: 'Integrations' }
+96
View File
@@ -0,0 +1,96 @@
// Plan presets (APPS_PLATFORM D-3, §5.2): named module maps applied to sites.
// A preset only fills in a site's map; after applying, each site is edited on its own.
import { useEffect, useState } from 'react'
import toast from 'react-hot-toast'
import client from '../api/client'
import { FALLBACK_CATALOG } from '../moduleCatalog'
import ModuleMapEditor from '../components/ModuleMapEditor'
const EMPTY = { id: null, name: '', description: '', modules: {} }
export default function PresetsPage() {
const [presets, setPresets] = useState([])
const [editing, setEditing] = useState(null)
const [saving, setSaving] = useState(false)
async function load() {
try {
setPresets((await client.get('/api/presets/')).data)
} catch {
toast.error('Could not load presets')
}
}
useEffect(() => { load() }, [])
async function save() {
if (!editing.name.trim()) { toast.error('Give the preset a name'); return }
setSaving(true)
const body = { name: editing.name.trim(), description: editing.description || null, modules: editing.modules }
try {
if (editing.id) await client.put(`/api/presets/${editing.id}`, body)
else await client.post('/api/presets/', body)
toast.success('Preset saved')
setEditing(null)
load()
} catch (e) {
toast.error(e.response?.data?.detail || 'Could not save preset')
} finally {
setSaving(false)
}
}
async function remove(p) {
if (!window.confirm(`Delete preset "${p.name}"? Sites keep their modules.`)) return
await client.delete(`/api/presets/${p.id}`)
load()
}
return (
<div>
<div className="flex items-center justify-between mb-4">
<h1 className="text-lg font-semibold text-white">Plan presets</h1>
{!editing && (
<button onClick={() => setEditing({ ...EMPTY })}
className="text-xs px-3 py-1.5 rounded bg-cyan-600 text-white hover:bg-cyan-500">New preset</button>
)}
</div>
{editing ? (
<div className="bg-gray-900 border border-gray-700 rounded-xl p-4">
<div className="grid gap-3 sm:grid-cols-2 mb-4">
<input value={editing.name} onChange={e => setEditing({ ...editing, name: e.target.value })} placeholder="Name"
className="bg-gray-800 border border-gray-700 rounded px-3 py-2 text-sm text-gray-200" />
<input value={editing.description || ''} onChange={e => setEditing({ ...editing, description: e.target.value })}
placeholder="Description (optional)" className="bg-gray-800 border border-gray-700 rounded px-3 py-2 text-sm text-gray-200" />
</div>
<ModuleMapEditor catalog={FALLBACK_CATALOG} value={editing.modules}
onChange={modules => setEditing({ ...editing, modules })} />
<div className="flex justify-end gap-2 mt-4">
<button onClick={() => setEditing(null)} className="text-xs px-3 py-1.5 text-gray-400 hover:text-gray-200">Cancel</button>
<button onClick={save} disabled={saving}
className="text-xs px-3 py-1.5 rounded bg-cyan-600 text-white hover:bg-cyan-500 disabled:opacity-40">
{saving ? 'Saving…' : 'Save preset'}
</button>
</div>
</div>
) : (
<div className="space-y-2">
{presets.length === 0 && <p className="text-sm text-gray-500">No presets yet.</p>}
{presets.map(p => (
<div key={p.id} className="bg-gray-900 border border-gray-700 rounded-xl p-4 flex items-start justify-between gap-4">
<div className="min-w-0">
<div className="text-sm text-white font-medium">{p.name}</div>
{p.description && <div className="text-xs text-gray-500">{p.description}</div>}
<div className="text-xs text-gray-400 mt-1 font-mono break-words">{Object.keys(p.modules).join(', ') || '(nothing)'}</div>
</div>
<div className="flex gap-3 shrink-0">
<button onClick={() => setEditing({ ...p })} className="text-xs text-cyan-400 hover:text-cyan-300">Edit</button>
<button onClick={() => remove(p)} className="text-xs text-red-400 hover:text-red-300">Delete</button>
</div>
</div>
))}
</div>
)}
</div>
)
}
+17 -1
View File
@@ -1,4 +1,4 @@
import { useState } from 'react'
import { useEffect, useState } from 'react'
import { useNavigate } from 'react-router-dom'
import client from '../api/client'
@@ -9,7 +9,10 @@ export default function RegisterSitePage() {
owner_name: '',
contact_email: '',
license_expires_at: '',
preset: 'Everything (v1 sites)',
})
const [presets, setPresets] = useState([])
useEffect(() => { client.get('/api/presets/').then(r => setPresets(r.data)).catch(() => {}) }, [])
const [loading, setLoading] = useState(false)
const [error, setError] = useState('')
const [created, setCreated] = useState(null) // { site_id, secret_key, name }
@@ -30,6 +33,7 @@ export default function RegisterSitePage() {
owner_name: form.owner_name.trim(),
contact_email: form.contact_email.trim(),
license_expires_at: new Date(form.license_expires_at).toISOString(),
preset: form.preset || null, // fills the site's licensed modules; editable afterwards
})
setCreated(data)
} catch (err) {
@@ -179,6 +183,18 @@ export default function RegisterSitePage() {
/>
</div>
<div>
<label className="block text-xs font-medium text-gray-400 mb-1.5 uppercase tracking-wider">Licensed modules (preset)</label>
<select
value={form.preset}
onChange={e => update('preset', e.target.value)}
className="w-full bg-gray-800 border border-gray-600 text-white rounded-lg px-3 py-2.5 text-sm focus:outline-none focus:ring-1 focus:ring-cyan-500"
>
{presets.map(p => <option key={p.id} value={p.name}>{p.name}</option>)}
<option value="">None (free tier until modules are set)</option>
</select>
</div>
{error && (
<p className="text-red-400 text-sm bg-red-900/20 border border-red-800/50 rounded-lg px-3 py-2">{error}</p>
)}
@@ -1,4 +1,5 @@
import { useState, useEffect, useCallback } from 'react'
import SiteModulesCard from '../components/SiteModulesCard'
import { useParams, useNavigate } from 'react-router-dom'
import toast from 'react-hot-toast'
import { QRCodeCanvas } from 'qrcode.react'
@@ -306,6 +307,9 @@ export default function SiteDetailPage() {
</div>
</div>
{/* Licensed modules (platform v2) */}
<SiteModulesCard site={site} onSaved={setSite} />
{/* Heartbeat */}
<div className="bg-gray-900 border border-gray-700 rounded-xl p-4 mb-4">
<h2 className="text-xs font-semibold text-gray-500 uppercase tracking-wider mb-3">Heartbeat</h2>