Phase6: Customer Billing details
All checks were successful
Build and Push / build (push) Successful in 1m46s
All checks were successful
Build and Push / build (push) Successful in 1m46s
This commit is contained in:
@@ -116,8 +116,23 @@ export function CronControls({ initialRecent, initialLastSuccess }: Props) {
|
||||
});
|
||||
};
|
||||
|
||||
// Phase 6: surface failures prominently. Any run in the recent
|
||||
// window with a non-zero failure_count drives a top-of-page
|
||||
// banner — the row in the table is already red, but a banner
|
||||
// means the admin doesn't have to scroll to notice.
|
||||
const recentFailures = recent.filter((r) => r.failureCount > 0);
|
||||
const hasRecentFailures = recentFailures.length > 0;
|
||||
|
||||
return (
|
||||
<div className="space-y-8">
|
||||
{hasRecentFailures && (
|
||||
<div className="p-4 rounded-md border border-error bg-error/10 text-sm text-error">
|
||||
<p className="font-medium mb-1">{t("failureBannerTitle")}</p>
|
||||
<p className="text-xs">
|
||||
{t("failureBannerBody", { count: recentFailures.length })}
|
||||
</p>
|
||||
</div>
|
||||
)}
|
||||
<section className="grid gap-4 md:grid-cols-2">
|
||||
<Card>
|
||||
<h2 className="text-xs uppercase tracking-wider text-text-muted mb-2">
|
||||
@@ -192,7 +207,12 @@ export function CronControls({ initialRecent, initialLastSuccess }: Props) {
|
||||
</thead>
|
||||
<tbody>
|
||||
{recent.map((r) => (
|
||||
<tr key={r.id} className="border-t border-border align-top">
|
||||
<tr
|
||||
key={r.id}
|
||||
className={`border-t border-border align-top ${
|
||||
r.failureCount > 0 ? "bg-error/5" : ""
|
||||
}`}
|
||||
>
|
||||
<td className="py-2 text-xs font-mono">
|
||||
{fmtRelative(r.startedAt)}
|
||||
</td>
|
||||
|
||||
@@ -11,6 +11,17 @@ type CurrentResponse =
|
||||
| { draft: InvoiceDraft }
|
||||
| { error: string; code?: string };
|
||||
|
||||
interface Props {
|
||||
/**
|
||||
* Whether the viewing user has org-owner role. Drives the
|
||||
* "complete your billing details" CTA — only owners can edit
|
||||
* billing settings, so non-owners see a softer message asking
|
||||
* them to contact their org owner instead. The flag is computed
|
||||
* server-side and passed in to avoid a second API round-trip.
|
||||
*/
|
||||
isOwner: boolean;
|
||||
}
|
||||
|
||||
/**
|
||||
* Live running total for the current calendar month.
|
||||
*
|
||||
@@ -28,7 +39,7 @@ type CurrentResponse =
|
||||
* No polling — the page is static enough that an explicit
|
||||
* "refresh" link is good enough if the user wants newer numbers.
|
||||
*/
|
||||
export function RunningTotalWidget() {
|
||||
export function RunningTotalWidget({ isOwner }: Props) {
|
||||
const t = useTranslations("customerBilling");
|
||||
const fmt = useFormatter();
|
||||
const [data, setData] = useState<CurrentResponse | null>(null);
|
||||
@@ -62,13 +73,29 @@ export function RunningTotalWidget() {
|
||||
);
|
||||
}
|
||||
if (!data || "error" in data) {
|
||||
const noConfig =
|
||||
data && "code" in data && data.code === "COMPUTE_FAILED";
|
||||
return (
|
||||
<Card>
|
||||
<p className="text-sm text-text-secondary py-2">
|
||||
{data && "code" in data && data.code === "COMPUTE_FAILED"
|
||||
? t("noBillingConfig")
|
||||
: t("currentPeriodError")}
|
||||
{noConfig ? t("noBillingConfig") : t("currentPeriodError")}
|
||||
</p>
|
||||
{/* Phase 6: owner-only CTA. Non-owners can't edit billing
|
||||
settings, so we show them a "contact owner" hint instead
|
||||
— that's gentler than a button that 404s on click. */}
|
||||
{noConfig && isOwner && (
|
||||
<Link
|
||||
href="/settings/billing"
|
||||
className="inline-block mt-2 px-4 py-2 rounded-md bg-accent text-white text-sm font-medium hover:bg-accent-dim transition-colors"
|
||||
>
|
||||
{t("configureBillingCta")}
|
||||
</Link>
|
||||
)}
|
||||
{noConfig && !isOwner && (
|
||||
<p className="text-xs text-text-muted italic mt-2">
|
||||
{t("noBillingConfigNonOwner")}
|
||||
</p>
|
||||
)}
|
||||
</Card>
|
||||
);
|
||||
}
|
||||
|
||||
229
src/components/settings/billing-form.tsx
Normal file
229
src/components/settings/billing-form.tsx
Normal file
@@ -0,0 +1,229 @@
|
||||
"use client";
|
||||
|
||||
import { useState } from "react";
|
||||
import { useRouter } from "next/navigation";
|
||||
import { useTranslations } from "next-intl";
|
||||
import { Card } from "@/components/ui/card";
|
||||
import type { OrgBilling } from "@/types";
|
||||
|
||||
interface Props {
|
||||
initial: OrgBilling | null;
|
||||
}
|
||||
|
||||
/**
|
||||
* Customer billing settings form. Drives PUT /api/settings/billing
|
||||
* which upserts org_billing for the caller's org.
|
||||
*
|
||||
* Validation is the same regex as the server-side zod schema for
|
||||
* the country field (ISO 3166-1 alpha-2). Other fields are checked
|
||||
* for required + max-length client-side; the server is the
|
||||
* authority and re-validates everything.
|
||||
*
|
||||
* On success we router.refresh() the page so the server component
|
||||
* re-fetches and any "create now" -> "edit" wording flips.
|
||||
*/
|
||||
export function BillingSettingsForm({ initial }: Props) {
|
||||
const t = useTranslations("settingsBilling");
|
||||
const router = useRouter();
|
||||
const [form, setForm] = useState({
|
||||
companyName: initial?.companyName ?? "",
|
||||
streetAddress: initial?.streetAddress ?? "",
|
||||
postalCode: initial?.postalCode ?? "",
|
||||
city: initial?.city ?? "",
|
||||
country: initial?.country ?? "CH",
|
||||
vatNumber: initial?.vatNumber ?? "",
|
||||
billingEmail: initial?.billingEmail ?? "",
|
||||
notes: initial?.notes ?? "",
|
||||
});
|
||||
const [busy, setBusy] = useState(false);
|
||||
const [error, setError] = useState<string | null>(null);
|
||||
const [savedFlash, setSavedFlash] = useState(false);
|
||||
|
||||
const set =
|
||||
(field: keyof typeof form) =>
|
||||
(e: React.ChangeEvent<HTMLInputElement | HTMLTextAreaElement>) =>
|
||||
setForm((f) => ({ ...f, [field]: e.target.value }));
|
||||
|
||||
const submit = async () => {
|
||||
setError(null);
|
||||
setSavedFlash(false);
|
||||
// Client-side gate on required fields — the server re-validates.
|
||||
if (
|
||||
!form.companyName.trim() ||
|
||||
!form.streetAddress.trim() ||
|
||||
!form.postalCode.trim() ||
|
||||
!form.city.trim() ||
|
||||
!form.country.trim() ||
|
||||
!form.billingEmail.trim()
|
||||
) {
|
||||
setError(t("missingRequired"));
|
||||
return;
|
||||
}
|
||||
if (!/^[A-Za-z]{2}$/.test(form.country.trim())) {
|
||||
setError(t("invalidCountry"));
|
||||
return;
|
||||
}
|
||||
if (!/^[^@\s]+@[^@\s]+\.[^@\s]+$/.test(form.billingEmail.trim())) {
|
||||
setError(t("invalidEmail"));
|
||||
return;
|
||||
}
|
||||
setBusy(true);
|
||||
try {
|
||||
const res = await fetch("/api/settings/billing", {
|
||||
method: "PUT",
|
||||
headers: { "Content-Type": "application/json" },
|
||||
body: JSON.stringify({
|
||||
companyName: form.companyName.trim(),
|
||||
streetAddress: form.streetAddress.trim(),
|
||||
postalCode: form.postalCode.trim(),
|
||||
city: form.city.trim(),
|
||||
country: form.country.trim().toUpperCase(),
|
||||
vatNumber: form.vatNumber.trim() || null,
|
||||
billingEmail: form.billingEmail.trim(),
|
||||
notes: form.notes.trim() || null,
|
||||
}),
|
||||
});
|
||||
const data = await res.json().catch(() => ({}));
|
||||
if (!res.ok) {
|
||||
throw new Error(data.error ?? `HTTP ${res.status}`);
|
||||
}
|
||||
setSavedFlash(true);
|
||||
router.refresh();
|
||||
} catch (e: any) {
|
||||
setError(e?.message ?? String(e));
|
||||
} finally {
|
||||
setBusy(false);
|
||||
}
|
||||
};
|
||||
|
||||
return (
|
||||
<Card>
|
||||
<div className="space-y-4">
|
||||
<Field label={t("companyNameLabel")} required>
|
||||
<input
|
||||
type="text"
|
||||
value={form.companyName}
|
||||
onChange={set("companyName")}
|
||||
maxLength={200}
|
||||
className="w-full px-3 py-2 rounded-md bg-surface-2 border border-border focus:border-accent focus:outline-none text-sm"
|
||||
/>
|
||||
</Field>
|
||||
<Field label={t("streetAddressLabel")} required>
|
||||
<input
|
||||
type="text"
|
||||
value={form.streetAddress}
|
||||
onChange={set("streetAddress")}
|
||||
maxLength={200}
|
||||
className="w-full px-3 py-2 rounded-md bg-surface-2 border border-border focus:border-accent focus:outline-none text-sm"
|
||||
/>
|
||||
</Field>
|
||||
<div className="grid grid-cols-1 md:grid-cols-3 gap-4">
|
||||
<Field label={t("postalCodeLabel")} required>
|
||||
<input
|
||||
type="text"
|
||||
value={form.postalCode}
|
||||
onChange={set("postalCode")}
|
||||
maxLength={20}
|
||||
className="w-full px-3 py-2 rounded-md bg-surface-2 border border-border focus:border-accent focus:outline-none text-sm"
|
||||
/>
|
||||
</Field>
|
||||
<Field label={t("cityLabel")} required>
|
||||
<input
|
||||
type="text"
|
||||
value={form.city}
|
||||
onChange={set("city")}
|
||||
maxLength={100}
|
||||
className="w-full px-3 py-2 rounded-md bg-surface-2 border border-border focus:border-accent focus:outline-none text-sm"
|
||||
/>
|
||||
</Field>
|
||||
<Field
|
||||
label={t("countryLabel")}
|
||||
required
|
||||
hint={t("countryHint")}
|
||||
>
|
||||
<input
|
||||
type="text"
|
||||
value={form.country}
|
||||
onChange={(e) =>
|
||||
setForm((f) => ({
|
||||
...f,
|
||||
country: e.target.value.toUpperCase().slice(0, 2),
|
||||
}))
|
||||
}
|
||||
maxLength={2}
|
||||
className="w-full px-3 py-2 rounded-md bg-surface-2 border border-border focus:border-accent focus:outline-none text-sm uppercase font-mono"
|
||||
/>
|
||||
</Field>
|
||||
</div>
|
||||
<Field label={t("vatNumberLabel")} hint={t("vatNumberHint")}>
|
||||
<input
|
||||
type="text"
|
||||
value={form.vatNumber}
|
||||
onChange={set("vatNumber")}
|
||||
maxLength={40}
|
||||
placeholder="CHE-123.456.789 MWST"
|
||||
className="w-full px-3 py-2 rounded-md bg-surface-2 border border-border focus:border-accent focus:outline-none text-sm font-mono"
|
||||
/>
|
||||
</Field>
|
||||
<Field label={t("billingEmailLabel")} required hint={t("billingEmailHint")}>
|
||||
<input
|
||||
type="email"
|
||||
value={form.billingEmail}
|
||||
onChange={set("billingEmail")}
|
||||
maxLength={200}
|
||||
className="w-full px-3 py-2 rounded-md bg-surface-2 border border-border focus:border-accent focus:outline-none text-sm"
|
||||
/>
|
||||
</Field>
|
||||
<Field label={t("notesLabel")} hint={t("notesHint")}>
|
||||
<textarea
|
||||
value={form.notes}
|
||||
onChange={set("notes")}
|
||||
maxLength={2000}
|
||||
rows={3}
|
||||
className="w-full px-3 py-2 rounded-md bg-surface-2 border border-border focus:border-accent focus:outline-none text-sm"
|
||||
/>
|
||||
</Field>
|
||||
{error && (
|
||||
<p className="text-sm text-error">{error}</p>
|
||||
)}
|
||||
{savedFlash && (
|
||||
<p className="text-sm text-success">{t("saved")}</p>
|
||||
)}
|
||||
<div className="flex justify-end">
|
||||
<button
|
||||
onClick={submit}
|
||||
disabled={busy}
|
||||
className="px-4 py-2 rounded-md bg-accent text-white text-sm font-medium hover:bg-accent-dim transition-colors disabled:opacity-50 cursor-pointer"
|
||||
>
|
||||
{busy ? t("saving") : initial ? t("saveChanges") : t("createBilling")}
|
||||
</button>
|
||||
</div>
|
||||
</div>
|
||||
</Card>
|
||||
);
|
||||
}
|
||||
|
||||
function Field({
|
||||
label,
|
||||
required,
|
||||
hint,
|
||||
children,
|
||||
}: {
|
||||
label: string;
|
||||
required?: boolean;
|
||||
hint?: string;
|
||||
children: React.ReactNode;
|
||||
}) {
|
||||
return (
|
||||
<div>
|
||||
<label className="block text-xs uppercase tracking-wider text-text-muted mb-1">
|
||||
{label}
|
||||
{required && <span className="text-error ml-1">*</span>}
|
||||
</label>
|
||||
{children}
|
||||
{hint && (
|
||||
<p className="text-xs text-text-muted mt-1 italic">{hint}</p>
|
||||
)}
|
||||
</div>
|
||||
);
|
||||
}
|
||||
Reference in New Issue
Block a user