mirror of
https://dev.azure.com/globalhealthx/EMR/_git/helix-engage-server
synced 2026-04-11 18:08:16 +00:00
feat: widget config via admin-editable data/widget.json
Mirrors the existing theme config pattern so website widget settings can be
edited from the admin portal instead of baked into frontend env vars. Fixes
the current symptom where the staging widget is silently disabled because
VITE_WIDGET_KEY is missing from .env.production.
Backend (sidecar):
- src/config/widget.defaults.ts — WidgetConfig type + defaults
(enabled, key, siteId, url, allowedOrigins, hospitalName,
embed.loginPage, version, updatedAt)
- src/config/widget-config.service.ts — file-backed load / update /
rotate-key / reset with backups, mirroring ThemeService. On module init:
* first boot → auto-generates an HMAC-signed site key via
WidgetKeysService, persists both to data/widget.json and to Redis
* subsequent boots → re-registers the key in Redis if missing (handles
Redis flushes so validateKey() keeps working without admin action)
- src/config/widget-config.controller.ts — new endpoints under /api/config:
GET /api/config/widget public subset {enabled, key, url, embed}
GET /api/config/widget/admin full config for the settings UI
PUT /api/config/widget admin update (partial merge)
POST /api/config/widget/rotate-key revoke old siteId + mint a new key
POST /api/config/widget/reset reset to defaults + regenerate
- Move src/widget/widget-keys.service.ts → src/config/widget-keys.service.ts
(it's a config-layer concern now, not widget-layer). config-theme.module
becomes the owner, imports AuthModule for SessionService, and exports
WidgetKeysService + WidgetConfigService alongside ThemeService.
- widget.module stops providing WidgetKeysService (it imports ConfigThemeModule
already, so the guard + controller still get it via DI).
- .gitignore data/widget.json + data/widget-backups/ so each environment
auto-generates its own instance-specific key instead of sharing one via git.
TODO (flagged, out of scope for this pass):
- Protect admin endpoints with an auth guard when settings UI ships.
- Set WIDGET_SECRET env var in staging (currently falls back to the
hardcoded default in widget-keys.service.ts).
- Admin portal settings page for editing widget config (mirror branding-settings).
Co-Authored-By: Claude Opus 4.6 (1M context) <noreply@anthropic.com>
This commit is contained in:
50
src/config/widget.defaults.ts
Normal file
50
src/config/widget.defaults.ts
Normal file
@@ -0,0 +1,50 @@
|
||||
// Shape of the website-widget configuration, stored in data/widget.json.
|
||||
// Mirrors the theme config pattern — file-backed, versioned, admin-editable.
|
||||
export type WidgetConfig = {
|
||||
// Master feature flag. When false, the widget does not render anywhere.
|
||||
enabled: boolean;
|
||||
|
||||
// HMAC-signed site key the embed script passes as data-key. Auto-generated
|
||||
// on first boot if empty. Rotate via POST /api/config/widget/rotate-key.
|
||||
key: string;
|
||||
|
||||
// Stable site identifier derived from the key. Used for Redis lookup and
|
||||
// revocation. Populated alongside `key`.
|
||||
siteId: string;
|
||||
|
||||
// Public base URL where widget.js is hosted. Typically the sidecar host.
|
||||
// If empty, the embed page falls back to its own VITE_API_URL at fetch time.
|
||||
url: string;
|
||||
|
||||
// Origin allowlist. Empty array means any origin is accepted (test mode).
|
||||
// Set tight values in production: ['https://hospital.com'].
|
||||
allowedOrigins: string[];
|
||||
|
||||
// Display label attached to the site key in the CRM / admin listings.
|
||||
hospitalName: string;
|
||||
|
||||
// Embed toggles — where the widget should render. Kept as an object so we
|
||||
// can add other surfaces (public landing page, portal, etc.) without a
|
||||
// breaking schema change.
|
||||
embed: {
|
||||
// Show on the staff login page. Useful for testing without a public
|
||||
// landing page; turn off in production.
|
||||
loginPage: boolean;
|
||||
};
|
||||
|
||||
// Bookkeeping — incremented on every update, like the theme config.
|
||||
version?: number;
|
||||
updatedAt?: string;
|
||||
};
|
||||
|
||||
export const DEFAULT_WIDGET_CONFIG: WidgetConfig = {
|
||||
enabled: true,
|
||||
key: '',
|
||||
siteId: '',
|
||||
url: '',
|
||||
allowedOrigins: [],
|
||||
hospitalName: 'Global Hospital',
|
||||
embed: {
|
||||
loginPage: true,
|
||||
},
|
||||
};
|
||||
Reference in New Issue
Block a user