diff --git a/apps/api/src/routes/invoices.ts b/apps/api/src/routes/invoices.ts index 9bb8790..a5c0d95 100644 --- a/apps/api/src/routes/invoices.ts +++ b/apps/api/src/routes/invoices.ts @@ -102,6 +102,7 @@ invoicesRouter.get( paidAt: invoices.paidAt, notes: invoices.notes, stripePaymentIntentId: invoices.stripePaymentIntentId, + stripeRefundId: invoices.stripeRefundId, createdAt: invoices.createdAt, updatedAt: invoices.updatedAt, }) diff --git a/apps/web/src/pages/Invoices.tsx b/apps/web/src/pages/Invoices.tsx index 76dcbfd..246a9be 100644 --- a/apps/web/src/pages/Invoices.tsx +++ b/apps/web/src/pages/Invoices.tsx @@ -173,22 +173,21 @@ function InvoiceDetailModal({ const [error, setError] = useState(null); const [tipStr, setTipStr] = useState((invoice.tipCents / 100).toFixed(2)); const [paymentMethod, setPaymentMethod] = useState(invoice.paymentMethod ?? "cash"); - const [showRefundDialog, setShowRefundDialog] = useState(false); +const [showRefundDialog, setShowRefundDialog] = useState(false); const [refundType, setRefundType] = useState<"full" | "partial">("full"); - const [partialAmount, setPartialAmount] = useState(""); - const [stripeDetails, setStripeDetails] = useState<{ cardLast4: string | null; paymentStatus: string | null; stripeRefundId: string | null } | null>(null); + const [refundAmount, setRefundAmount] = useState(""); + const [refundError, setRefundError] = useState(null); + const [refunding, setRefunding] = useState(false); - // Fetch Stripe details when modal opens for paid invoices with a payment intent + // Fetch current staff role to determine manager access + const [staffMe, setStaffMe] = useState<{ role: string; isSuperUser: boolean } | null>(null); useEffect(() => { - if (invoice.status === "paid" && invoice.stripePaymentIntentId) { - fetch(`/api/invoices/${invoice.id}/stripe-details`) - .then((r) => r.ok ? r.json() : null) - .then((data) => { if (data) setStripeDetails(data); }) - .catch(() => {}); - } else { - setStripeDetails(null); - } - }, [invoice.id, invoice.status, invoice.stripePaymentIntentId]); + fetch("/api/staff/me") + .then((r) => r.json()) + .then((d) => setStaffMe(d)) + .catch(() => setStaffMe(null)); + }, []); + const isManager = staffMe && (staffMe.role === "manager" || staffMe.isSuperUser); // Tip split state: array of {staffId, staffName, pct} const linkedAppt = invoice.appointmentId @@ -292,35 +291,6 @@ function InvoiceDetailModal({ } } - async function issueRefund() { - const amountCents = refundType === "partial" - ? Math.round(parseFloat(partialAmount) * 100) - : undefined; - if (refundType === "partial" && (!amountCents || amountCents <= 0)) { - setError("Enter a valid refund amount"); - return; - } - setSaving(true); - setError(null); - try { - const res = await fetch(`/api/invoices/${invoice.id}/refund`, { - method: "POST", - headers: { "Content-Type": "application/json" }, - body: JSON.stringify(amountCents ? { amountCents } : {}), - }); - if (!res.ok) { - const err = (await res.json()) as { error?: string }; - throw new Error(err.error ?? `HTTP ${res.status}`); - } - setShowRefundDialog(false); - onUpdated(); - } catch (e: unknown) { - setError(e instanceof Error ? e.message : "Failed to issue refund"); - } finally { - setSaving(false); - } - } - if (loading) return

Loading…

; const tipCentsCalc = Math.round(parseFloat(tipStr) * 100) || 0; @@ -380,15 +350,15 @@ function InvoiceDetailModal({ /> {invoice.paidAt && } {invoice.paymentMethod && } - {stripeDetails && ( + {invoice.stripePaymentIntentId && ( <> - {stripeDetails.cardLast4 && ( - + {invoice.cardLast4 && ( + )} - {stripeDetails.paymentStatus && ( - + {invoice.paymentStatus && ( + )} - {stripeDetails.stripeRefundId && ( + {invoice.stripeRefundId && ( )} @@ -510,77 +480,92 @@ function InvoiceDetailModal({ )} {(invoice.status === "paid" || invoice.status === "void") && ( -
- {invoice.status === "paid" && invoice.stripePaymentIntentId && ( - +
+ {invoice.stripeRefundId && ( +
+ Refunded +
)} - +
+ {invoice.status === "paid" && invoice.stripePaymentIntentId && !invoice.stripeRefundId && isManager && ( + + )} + +
)} - {/* Refund Dialog */} {showRefundDialog && ( - setShowRefundDialog(false)}> -

Issue Refund

-

- Invoice total: {fmtMoney(invoice.totalCents)} -

-
-