// Pure helpers for the Devices page (plan step 2.6d). Unit-tested in manager_dashboard/tests. /** Seen within this long = "Online" (the backend writes last_seen_at at most once a minute). */ export const ONLINE_WITHIN_MS = 3 * 60 * 1000 /** * A device's status for the list: * removed revoked * waiting paired here, but the device hasn't picked up its token yet * app_off its app is turned off or not licensed (the device shows "app disabled") * online seen in the last 3 minutes * offline seen before that (or never since pairing) */ export function deviceStatus(d, { appOn = true, now = Date.now() } = {}) { if (d.revoked_at) return 'removed' if (!d.claimed) return 'waiting' if (!appOn) return 'app_off' const seen = d.last_seen_at ? Date.parse(d.last_seen_at) : NaN return Number.isFinite(seen) && now - seen <= ONLINE_WITHIN_MS ? 'online' : 'offline' } /** Pairing codes are typed by people: upper-case, no spaces or dashes, at most 6 characters. */ export function normalizeCode(input) { return String(input || '').toUpperCase().replace(/[^A-Z0-9]/g, '').slice(0, 6) } /** * How many devices of each app are paired (not removed), and the license limit: * { kds: { used: 1, max: 2 }, signage: { used: 0, max: null } } (max null = unlimited) * `modules` = GET /api/platform/modules → modules. */ export function deviceUsage(devices, modules) { const out = {} for (const m of modules) { if (!m.device_app) continue const limit = m.limits.find(l => l.key === 'devices') out[m.key] = { used: 0, max: limit ? limit.value ?? null : null } } for (const d of devices) { if (!d.revoked_at && out[d.app_key]) out[d.app_key].used++ } return out } /** Is there room for one more device of this app? (when moving a device, it doesn't count against itself) */ export function hasRoom(usage, appKey, { moving = false } = {}) { const u = usage[appKey] if (!u || u.max == null) return true return u.used - (moving ? 1 : 0) < u.max } /** Devices another device may link to: not itself, not removed. */ export function linkTargets(devices, selfId = null) { return devices.filter(d => !d.revoked_at && d.id !== selfId) } // ── per-device settings (plan step 3.1a) ──────────────────────────────────── // `fields` = the app's device_app.config from GET /api/platform/modules: // [{ key, type: 'bool'|'int'|'str'|'enum'|'ref', default, choices, min, max, choices_from }] /** The form's starting values: the device's config (already effective) or the defaults. */ export function configInitial(fields = [], current = null) { return Object.fromEntries(fields.map(f => [f.key, current && f.key in current ? current[f.key] : f.default])) } /** A form control's raw value → the value sent to the API (selects and inputs give strings). */ export function configValue(field, raw) { if (field.type === 'bool') return !!raw if (field.type === 'int') return raw === '' || raw == null ? null : Number(raw) if (field.type === 'ref') return raw === '' || raw == null ? null : Number(raw) if (field.type === 'enum') return raw return raw == null ? '' : String(raw) } /** Fields in sections, in manifest order of each group's first field: [{group, fields}] * (fields without a group form their own section, 3.2a). */ export function configGroups(fields = []) { const out = [] for (const f of fields) { const g = f.group ?? null let sec = out.find(s => s.group === g) if (!sec) { sec = { group: g, fields: [] }; out.push(sec) } sec.fields.push(f) } return out } /** Options of a `ref` field: prep zones, categories, printers, customer display looks, or live devices of one app ("devices:"). */ export function refOptions(field, { zones = [], categories = [], printers = [], looks = [], playlists = [], slides = [], schedules = [], tables = [], devices = [], selfId = null } = {}) { if (field.choices_from === 'tables') return tables.filter(x => x.is_active !== false).map(x => ({ value: String(x.id), label: x.label || String(x.number) })) if (field.choices_from === 'signage_schedules') return schedules.map(s => ({ value: String(s.id), label: s.name })) if (field.choices_from === 'signage_playlists') return playlists.map(p => ({ value: String(p.id), label: p.name })) if (field.choices_from === 'signage_slides') return slides.map(p => ({ value: String(p.id), label: p.name })) if (field.choices_from === 'display_looks') return looks.map(l => ({ value: String(l.id), label: l.name })) if (field.choices_from === 'prep_zones') return zones.map(z => ({ value: String(z.id), label: z.name })) if (field.choices_from === 'categories') return categories.map(c => ({ value: String(c.id), label: c.name })) if (field.choices_from === 'printers') return printers.filter(p => p.is_active !== false).map(p => ({ value: String(p.id), label: p.name })) if (field.choices_from?.startsWith('devices:')) { const app = field.choices_from.slice('devices:'.length) return devices.filter(d => !d.revoked_at && d.app_key === app && d.id !== selfId).map(d => ({ value: String(d.id), label: d.name })) } return [] }