fix plans
This commit is contained in:
1 parent
af670e79f0
commit
04794aea54
4 files changed
+157
-15
No files matched your search
@@ -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"
|
||||
};
|
||||
}
|
||||
Reference in new issue
Block a user