feat(modules): venue-module registry — entitled ∩ activated, requireModule, Setup panel
Groundwork for the Car Wash pilot (wiki/decisions/venue-modules.md, build-order
steps 1 + 3). No Car Wash code yet; validation is the first module behind the
seam, unchanged in behaviour.
- @parking/shared: MODULE_IDS, ModuleManifest, MODULES (parking required;
validation dependsOn parking), parseEntitledModules / resolveModuleActivation
/ effectiveModules as pure functions.
- DB: site_config.modules_json (migration 0026, hand-written + journal;
additive, nullable = everything entitled).
- Server: modules.ts (entitledModules from MODULES_ENTITLED env, activated
from site_config, effective set, requireModule preHandler → 403
module_disabled); modules/index.ts registers folder-based modules by
iterating the registry (modules/validation); site-config GET exposes
modules/modulesEntitled/modulesActivated, PUT takes the full desired set,
enforces entitlement + dependency rules (400 with reason) and signs one
config_change per module that actually flips; /api/auth/me carries the
effective set; validation routes guarded requireModule → requirePermission.
- Web: lib/modules.ts + modules/{index,validation}; router.tsx spreads
WEB_MODULES into nav + route tree (validate route no longer named there);
Setup → Site "Modules" panel (required shown disabled, dependencies as
hints, server refusal shown verbatim); validation sections + programs fetch
gated on the module; App invalidates the router whenever the session
changes (route-context consumers only re-read on navigation — the nav was
stale after a flip, and after every other setUser too).
- Lavazh validation station retired (STATIONS = ["bar"]; rows untouched).
- Deploy: MODULES_ENTITLED=parking,validation explicit in both booth stacks;
documented in .env.example.
- Tests: modules.test.ts (7); suite 329/329; web build clean; Playwright
round-trip on /setup/site verified live.
Claude-Session: https://claude.ai/code/session_01FWncR69HgGPuei1dLrW3cU
This commit is contained in:
@@ -0,0 +1,59 @@
|
||||
import type { FastifyReply, FastifyRequest } from "fastify";
|
||||
import { eq, siteConfig, type Db } from "@parking/db";
|
||||
import {
|
||||
effectiveModules,
|
||||
isModuleId,
|
||||
parseEntitledModules,
|
||||
type ModuleId,
|
||||
} from "@parking/shared";
|
||||
|
||||
// Venue modules — the server side of "entitled ∩ activated" (registry + rules live in
|
||||
// @parking/shared; design in wiki/decisions/venue-modules.md).
|
||||
//
|
||||
// entitled MODULES_ENTITLED env (vendor, Komodo stack) — unset = everything.
|
||||
// activated site_config.modules_json (site admin, Setup → Site) — null = everything
|
||||
// entitled.
|
||||
// effective what requireModule() enforces and what /api/auth/me + /api/site-config
|
||||
// hand the SPA so it can hide nav. The web only HIDES; this file ENFORCES.
|
||||
//
|
||||
// Both inputs are re-read per request: one env read and one single-row SELECT on the
|
||||
// site_config singleton — cheap, and it means a change takes effect on the next request
|
||||
// with no cache to invalidate (the same reason the presence-bypass flags aren't cached).
|
||||
|
||||
/** The modules this deployment is entitled to. Unknown ids in the env are ignored
|
||||
* (logged once at boot by registerModules). */
|
||||
export function entitledModules(): ModuleId[] {
|
||||
return parseEntitledModules(process.env.MODULES_ENTITLED).entitled;
|
||||
}
|
||||
|
||||
/** Parse the persisted activation list off a site_config row. null = never set. A
|
||||
* corrupt/unknown value is treated as "never set" rather than locking modules off. */
|
||||
export function activatedModulesOf(row: { modulesJson?: string | null } | undefined): ModuleId[] | null {
|
||||
const raw = row?.modulesJson;
|
||||
if (raw == null) return null;
|
||||
try {
|
||||
const parsed: unknown = JSON.parse(raw);
|
||||
if (!Array.isArray(parsed)) return null;
|
||||
return parsed.filter(isModuleId);
|
||||
} catch {
|
||||
return null;
|
||||
}
|
||||
}
|
||||
|
||||
/** The effective set for this site right now. */
|
||||
export function effectiveModulesFor(db: Db): ModuleId[] {
|
||||
const row = db.select({ modulesJson: siteConfig.modulesJson }).from(siteConfig).where(eq(siteConfig.id, 1)).get();
|
||||
return effectiveModules(entitledModules(), activatedModulesOf(row));
|
||||
}
|
||||
|
||||
/** preHandler: reject the call when `id` is not effective at this site. Compose it
|
||||
* BEFORE requirePermission in a preHandler array so a disabled module answers the
|
||||
* same way for every role — 403 with code "module_disabled" — and never reaches
|
||||
* the permission/CSRF path. */
|
||||
export function requireModule(db: Db, id: ModuleId) {
|
||||
return async (_req: FastifyRequest, _reply: FastifyReply): Promise<void> => {
|
||||
if (!effectiveModulesFor(db).includes(id)) {
|
||||
throw Object.assign(new Error(`module disabled: ${id}`), { statusCode: 403, code: "module_disabled" });
|
||||
}
|
||||
};
|
||||
}
|
||||
Reference in New Issue
Block a user