fix plans

This commit is contained in:
greeneclipse committed 2026-10-14 21:23:56 +02:00
1 parent af670e79f0
commit 04794aea54
4 files changed
+157 -15

No files matched your search

+67
View File
@@ -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/);
});
});
+66
View File
@@ -0,0 +1,66 @@
/** Normalize Svelte number-input bindings before submitting billing plan changes.
*
* Svelte binds <input type="number"> 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"
};
}
+5 -15
View File
@@ -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();