diff --git a/README.md b/README.md index 61e322d..3ee68d6 100644 --- a/README.md +++ b/README.md @@ -1,3 +1,22 @@ +# Admin billing create/edit plan hotfix (October 14, 2026) + +The Create plan form previously called `.trim()` on `max_products` and `yearly_credits`. Svelte's number inputs bind numeric values as numbers (or `undefined` when empty), causing `max_products.trim is not a function` and preventing custom Demo plans from being created. + +**Fix:** Both optional limits now normalize to positive/zero safe integers or `null` (unlimited/unset), monthly credits allow `0`, and invalid numeric values show a field-specific error without sending a request. The same correction applies to **Edit plan**. + +No migration, API update, or seed is needed. **Deploy the updated web frontend** and reload `/admin/billing`. + +Regression tests: + +```bash +cd apps/web +node --experimental-strip-types --test src/lib/admin-plan-form.test.ts +``` + +This package includes all earlier v8 changes. + +--- + # v8 fix — existing non-A1 language prompts This update addresses the remaining English/Croatian/Bulgarian mismatch in existing non-A1 categories. It includes the exact historical v3 locale snapshots, so the seed can identify the old five-section translations and upgrade them **without overriding custom company instructions**. It also updates any exact old structured description formula that would otherwise force the AI to output five H2 sections. diff --git a/apps/web/src/lib/admin-plan-form.test.ts b/apps/web/src/lib/admin-plan-form.test.ts new file mode 100644 index 0000000..c800a92 --- /dev/null +++ b/apps/web/src/lib/admin-plan-form.test.ts @@ -0,0 +1,67 @@ +import assert from "node:assert/strict"; +import { describe, it } from "node:test"; +import { normalizeAdminPlanForm, parsePlanNonnegativeInteger } from "./admin-plan-form.ts"; + +const demoForm = () => ({ + id: 0, + name: "Demo", + description: "Demo plan", + monthly_credits: 0, + yearly_credits: undefined, + max_products: 20, + is_custom: true, + term: "monthly" +}); + +describe("Admin billing plan numeric form input", () => { + it("creates the example custom Demo plan with Svelte-bound numeric values", () => { + assert.deepEqual(normalizeAdminPlanForm(demoForm()), { + id: undefined, + name: "Demo", + description: "Demo plan", + monthly_credits: 0, + yearly_credits: null, + max_products: 20, + is_custom: true, + term: "monthly" + }); + }); + + it("handles populated yearly credits as a number after editing", () => { + assert.deepEqual(normalizeAdminPlanForm({ + ...demoForm(), + id: 12, + yearly_credits: 500, + max_products: 100 + }), { + id: 12, + name: "Demo", + description: "Demo plan", + monthly_credits: 0, + yearly_credits: 500, + max_products: 100, + is_custom: true, + term: "monthly" + }); + }); + + it("allows clearing optional fields on edit rather than turning them into zero", () => { + const result = normalizeAdminPlanForm({ ...demoForm(), yearly_credits: "", max_products: undefined }); + assert.equal(result.yearly_credits, null); + assert.equal(result.max_products, null); + }); + + it("accepts valid numeric strings and zero", () => { + assert.equal(parsePlanNonnegativeInteger(" 25 ", "Max products", true), 25); + assert.equal(parsePlanNonnegativeInteger(0, "Monthly credits"), 0); + assert.equal(parsePlanNonnegativeInteger("0", "Yearly credits", true), 0); + }); + + it("rejects invalid input instead of silently sending NaN or negative caps", () => { + for (const invalid of [-1, 1.5, "abc", Infinity, NaN, "-2", "1e309", "9007199254740992"]) { + assert.throws(() => parsePlanNonnegativeInteger(invalid, "Max products", true)); + } + assert.throws(() => parsePlanNonnegativeInteger(undefined, "Monthly credits"), /required/); + assert.throws(() => normalizeAdminPlanForm({ ...demoForm(), name: " " }), /Plan name/); + }); +}); diff --git a/apps/web/src/lib/admin-plan-form.ts b/apps/web/src/lib/admin-plan-form.ts new file mode 100644 index 0000000..cb094a1 --- /dev/null +++ b/apps/web/src/lib/admin-plan-form.ts @@ -0,0 +1,66 @@ +/** Normalize Svelte number-input bindings before submitting billing plan changes. + * + * Svelte binds to a number (or undefined when cleared), + * not a string. Optional empty numeric fields must become null, not 0/NaN. + */ +export type PlanNumberInput = number | string | null | undefined; + +export type AdminPlanFormDraft = { + id: number; + name: string; + description: string; + monthly_credits: PlanNumberInput; + yearly_credits: PlanNumberInput; + max_products: PlanNumberInput; + is_custom: boolean; + term: string; +}; + +export type AdminPlanFormPayload = { + id?: number; + name: string; + description: string | null; + monthly_credits: number; + yearly_credits: number | null; + max_products: number | null; + is_custom: boolean; + term: string; +}; + +export function parsePlanNonnegativeInteger(value: PlanNumberInput, field: string): number; +export function parsePlanNonnegativeInteger(value: PlanNumberInput, field: string, optional: false): number; +export function parsePlanNonnegativeInteger(value: PlanNumberInput, field: string, optional: true): number | null; +export function parsePlanNonnegativeInteger( + value: PlanNumberInput, + field: string, + optional = false +): number | null { + const text = value == null ? "" : String(value).trim(); + if (!text) { + if (optional) return null; + throw new Error(`${field} is required.`); + } + const numeric = Number(text); + if (!Number.isSafeInteger(numeric) || numeric < 0) { + throw new Error(`${field} must be a non-negative whole number.`); + } + return numeric; +} + +export function normalizeAdminPlanForm(form: AdminPlanFormDraft): AdminPlanFormPayload { + const name = form.name.trim(); + if (!name) throw new Error("Plan name is required."); + const monthlyCredits = parsePlanNonnegativeInteger(form.monthly_credits, "Monthly credits"); + const yearlyCredits = parsePlanNonnegativeInteger(form.yearly_credits, "Yearly credits", true); + const maxProducts = parsePlanNonnegativeInteger(form.max_products, "Max products", true); + return { + id: form.id > 0 ? form.id : undefined, + name, + description: form.description.trim() || null, + monthly_credits: monthlyCredits, + yearly_credits: yearlyCredits, + max_products: maxProducts, + is_custom: form.is_custom, + term: form.term.trim() || "monthly" + }; +} diff --git a/apps/web/src/routes/admin/billing/+page.svelte b/apps/web/src/routes/admin/billing/+page.svelte index cfab2fe..baaab8f 100644 --- a/apps/web/src/routes/admin/billing/+page.svelte +++ b/apps/web/src/routes/admin/billing/+page.svelte @@ -23,6 +23,7 @@ type AdminBillingPlan } from "$lib/admin-billing-plans"; import { formatCredits } from "$lib/utils"; + import { normalizeAdminPlanForm } from "$lib/admin-plan-form"; import PageShell from "$lib/components/PageShell.svelte"; import Alert from "$lib/components/Alert.svelte"; import ForbiddenEmptyState from "$lib/components/ForbiddenEmptyState.svelte"; @@ -64,9 +65,9 @@ id: number; name: string; description: string; - monthly_credits: number; - yearly_credits: string; - max_products: string; + monthly_credits: string | number | undefined; + yearly_credits: string | number | undefined; + max_products: string | number | undefined; is_custom: boolean; term: string; }; @@ -230,18 +231,7 @@ error = ""; success = ""; try { - const yearlyRaw = planForm.yearly_credits.trim(); - const maxRaw = planForm.max_products.trim(); - await upsertAdminPlan({ - id: planForm.id > 0 ? planForm.id : undefined, - name: planForm.name, - description: planForm.description.trim() || null, - monthly_credits: Number(planForm.monthly_credits), - yearly_credits: yearlyRaw === "" ? null : Number(yearlyRaw), - max_products: maxRaw === "" ? null : Number(maxRaw), - is_custom: planForm.is_custom, - term: planForm.term - }); + await upsertAdminPlan(normalizeAdminPlanForm(planForm)); success = planFormMode === "edit" ? i18n.t("admin.billing.planUpdated") : i18n.t("admin.billing.planCreated"); planFormOpen = false; planForm = emptyPlanForm();