fix plans
This commit is contained in:
1 parent
af670e79f0
commit
04794aea54
4 files changed
+157
-15
No files matched your search
@@ -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
|
# 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.
|
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.
|
||||||
|
|||||||
@@ -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/);
|
||||||
|
});
|
||||||
|
});
|
||||||
@@ -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"
|
||||||
|
};
|
||||||
|
}
|
||||||
@@ -23,6 +23,7 @@
|
|||||||
type AdminBillingPlan
|
type AdminBillingPlan
|
||||||
} from "$lib/admin-billing-plans";
|
} from "$lib/admin-billing-plans";
|
||||||
import { formatCredits } from "$lib/utils";
|
import { formatCredits } from "$lib/utils";
|
||||||
|
import { normalizeAdminPlanForm } from "$lib/admin-plan-form";
|
||||||
import PageShell from "$lib/components/PageShell.svelte";
|
import PageShell from "$lib/components/PageShell.svelte";
|
||||||
import Alert from "$lib/components/Alert.svelte";
|
import Alert from "$lib/components/Alert.svelte";
|
||||||
import ForbiddenEmptyState from "$lib/components/ForbiddenEmptyState.svelte";
|
import ForbiddenEmptyState from "$lib/components/ForbiddenEmptyState.svelte";
|
||||||
@@ -64,9 +65,9 @@
|
|||||||
id: number;
|
id: number;
|
||||||
name: string;
|
name: string;
|
||||||
description: string;
|
description: string;
|
||||||
monthly_credits: number;
|
monthly_credits: string | number | undefined;
|
||||||
yearly_credits: string;
|
yearly_credits: string | number | undefined;
|
||||||
max_products: string;
|
max_products: string | number | undefined;
|
||||||
is_custom: boolean;
|
is_custom: boolean;
|
||||||
term: string;
|
term: string;
|
||||||
};
|
};
|
||||||
@@ -230,18 +231,7 @@
|
|||||||
error = "";
|
error = "";
|
||||||
success = "";
|
success = "";
|
||||||
try {
|
try {
|
||||||
const yearlyRaw = planForm.yearly_credits.trim();
|
await upsertAdminPlan(normalizeAdminPlanForm(planForm));
|
||||||
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
|
|
||||||
});
|
|
||||||
success = planFormMode === "edit" ? i18n.t("admin.billing.planUpdated") : i18n.t("admin.billing.planCreated");
|
success = planFormMode === "edit" ? i18n.t("admin.billing.planUpdated") : i18n.t("admin.billing.planCreated");
|
||||||
planFormOpen = false;
|
planFormOpen = false;
|
||||||
planForm = emptyPlanForm();
|
planForm = emptyPlanForm();
|
||||||
|
|||||||
Reference in new issue
Block a user