feature plus ux rearrangements

This commit is contained in:
JCEEE
2026-08-22 19:49:46 +01:00
parent f14f4e2ac1
commit 42b9a27ce6
44 changed files with 2020 additions and 538 deletions
+146
View File
@@ -154,6 +154,122 @@ async function ensureOtp(ids: Record<string, string>): Promise<void> {
});
}
// Platform access codes — the codes that enable access to the platform. They're
// global (not fam-scoped) and managed via the platform admin page (superuser
// only), so all rules are null like `otp`. A code grants a family a subscription
// for `duration` months (0 = continuous); `expiry` is months-after-createdAt
// (0 = never expires); `active` is a failsafe toggle. Entered at create-family
// and in admin settings.
async function ensureAccessCodes(): Promise<void> {
if (await getCollection("accesscodes")) {
await ensureAccessCodeFields();
await seedAccessCodes();
return;
}
await createCollection({
name: "accesscodes",
type: "base",
listRule: null,
viewRule: null,
createRule: null,
updateRule: null,
deleteRule: null,
fields: [
{ name: "value", type: "text", required: true, unique: true },
{ name: "name", type: "text", required: true },
{ name: "duration", type: "number", required: false },
{ name: "expiry", type: "number", required: false },
{ name: "active", type: "bool", required: false },
// When set (>0) this code is a TRIAL code: maps to Stripe
// trial_period_days at checkout instead of platform access.
{ name: "trialDays", type: "number", required: false },
{ name: "createdAt", type: "date", required: false },
],
});
await seedAccessCodes();
}
// Idempotent field-add for installs where accesscodes predates a field.
async function ensureAccessCodeFields(): Promise<void> {
const col = await getCollection("accesscodes");
if (!col) return;
const has = (n: string) => col.fields.some((f: any) => f.name === n);
if (!has("trialDays")) {
await updateCollection(col.id, {
...col,
fields: [...col.fields, { name: "trialDays", type: "number", required: false }],
});
}
}
// Idempotent seeds — developer code + an example trial code.
async function seedAccessCodes(): Promise<void> {
const t = await auth();
const seeds = [
{ value: "dev123", name: "developer", duration: 0, expiry: 0, active: true, trialDays: null },
{ value: "FAM3MONTHS", name: "3-month trial", duration: null, expiry: null, active: true, trialDays: 90 },
];
for (const seed of seeds) {
const res = await fetch(
`${PB_ENDPOINT}/api/collections/accesscodes/records?filter=value='${seed.value}'`,
{ headers: { Authorization: `Bearer ${t}` } },
);
const data = await res.json();
if (data?.items?.length) continue;
const created = await fetch(`${PB_ENDPOINT}/api/collections/accesscodes/records`, {
method: "POST",
headers: { "Content-Type": "application/json", Authorization: `Bearer ${t}` },
body: JSON.stringify({ ...seed, createdAt: new Date().toISOString() }),
});
const c = await created.json();
if (!created.ok) throw new Error(`Seed accesscode failed: ${JSON.stringify(c)}`);
console.log(` ✓ Seeded access code: ${seed.name} (${seed.value})`);
}
}
// Platform settings — a single global record holding the platform feature
// flags (replaces the per-fam fams.featureFlags). Publicly readable (empty
// list/view rules) so every client can deduce flags on app load; writes stay
// superuser-only (null rules), so like otp/accesscodes this lives in
// migrate.ts rather than SCHEMA_PLAN.
async function ensurePlatform(): Promise<void> {
if (!(await getCollection("platform"))) {
await createCollection({
name: "platform",
type: "base",
listRule: "",
viewRule: "",
createRule: null,
updateRule: null,
deleteRule: null,
fields: [
{ name: "label", type: "text", required: true },
{ name: "flags", type: "json", required: false },
],
});
}
await seedPlatform();
}
// Idempotent seed — create the singleton 'global' settings record if missing.
async function seedPlatform(): Promise<void> {
const t = await auth();
const res = await fetch(
`${PB_ENDPOINT}/api/collections/platform/records?filter=label='global'`,
{ headers: { Authorization: `Bearer ${t}` } },
);
const data = await res.json();
if (data?.items?.length) return;
const created = await fetch(`${PB_ENDPOINT}/api/collections/platform/records`, {
method: "POST",
headers: { "Content-Type": "application/json", Authorization: `Bearer ${t}` },
body: JSON.stringify({ label: "global", flags: { debug: false } }),
});
const c = await created.json();
if (!created.ok) throw new Error(`Seed platform failed: ${JSON.stringify(c)}`);
console.log(" ✓ Seeded platform settings (global)");
}
// Bootstrap the full schema on a fresh/wiped PocketBase. Idempotent — skips if
// `fams` already exists (data is disposable; there is no incremental migration
// history).
@@ -177,8 +293,38 @@ async function ensureSchema(): Promise<void> {
console.log("[migrate] Schema bootstrapped.");
}
// Add the access-gating fields to `fams` on installs where it already exists
// (fresh installs get them via SCHEMA_PLAN). Idempotent — only adds missing
// fields.
async function ensureFamFields(): Promise<void> {
const famsCol = await getCollection("fams");
if (!famsCol) return;
const has = (n: string) => famsCol.fields.some((f: any) => f.name === n);
const needed: any[] = [];
if (!has("active")) {
needed.push({ name: "active", type: "bool", required: false });
}
if (!has("paymentMode")) {
needed.push({ name: "paymentMode", type: "select", required: false, values: ["none", "code", "sub", "canceled"], maxSelect: 1 });
}
if (!has("accessCodeId")) {
needed.push({ name: "accessCodeId", type: "text", required: false });
}
if (!has("accessCodeEnteredAt")) {
needed.push({ name: "accessCodeEnteredAt", type: "date", required: false });
}
if (needed.length) {
await updateCollection(famsCol.id, { ...famsCol, fields: [...famsCol.fields, ...needed] });
}
}
export async function migrate(): Promise<void> {
console.log("[migrate] Checking PB collection schemas...");
await ensureSchema();
// Runs even when the schema already exists (unlike ensureSchema's early
// return) so new platform collections/fields/seed land on existing installs.
await ensureFamFields();
await ensureAccessCodes();
await ensurePlatform();
console.log("[migrate] Done");
}