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
@@ -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>
)
}