diff --git a/frontend/components/bookings/BookingForm.tsx b/frontend/components/bookings/BookingForm.tsx index 1614fa5..5f1d235 100644 --- a/frontend/components/bookings/BookingForm.tsx +++ b/frontend/components/bookings/BookingForm.tsx @@ -111,21 +111,37 @@ export default function BookingForm() { : totalNaira; async function handleApplyPromo() { - if (!promoCodeInput.trim() || !totalAmount) return; + if (!promoCodeInput.trim() || !totalAmount || !workspaceId) return; setApplyingPromo(true); setPromoError(null); try { const res = await fetch(`${process.env.NEXT_PUBLIC_API_URL || "http://localhost:6001/api"}/promo-codes/validate`, { method: "POST", headers: { "Content-Type": "application/json" }, - body: JSON.stringify({ code: promoCodeInput.trim(), subtotalKobo: totalAmount }), + body: JSON.stringify({ + code: promoCodeInput.trim(), + workspaceId, + bookingAmount: totalAmount, + }), }); const json = await res.json(); - if (!res.ok || !json.valid) { - setPromoError(json.message || "Invalid promo code"); - } else { - setAppliedPromo({ code: promoCodeInput.trim(), discountAmountKobo: json.discountAmountKobo, finalAmountKobo: json.finalAmountKobo }); + if (!res.ok || !json?.valid) { + setPromoError(json?.message || "Invalid promo code"); + return; } + // Backend returns discountValue + finalAmount; convert to a kobo figure + // client-side for percentage discounts so we can display the actual savings. + const discountAmountKobo = + json.discountType === "PERCENTAGE" + ? Math.floor((totalAmount * (json.discountValue ?? 0)) / 100) + : json.discountValue ?? 0; + const finalAmountKobo = + json.finalAmount ?? Math.max(0, totalAmount - discountAmountKobo); + setAppliedPromo({ + code: promoCodeInput.trim(), + discountAmountKobo, + finalAmountKobo, + }); } catch { setPromoError("Could not validate promo code"); } finally { @@ -400,36 +416,6 @@ export default function BookingForm() { )} - {!appliedPromo && ( -
{promoError}
} -{promoError}
+ )} + > + ) : ( +