diff --git a/apps/events/CHANGELOG.md b/apps/events/CHANGELOG.md index 020f4c3..3e738f8 100644 --- a/apps/events/CHANGELOG.md +++ b/apps/events/CHANGELOG.md @@ -7,6 +7,14 @@ and this project follows [Semantic Versioning](https://semver.org/). ## [Unreleased] +## [1.29.1] - 2026-10-21 + +### Changed + +- The Coupon codes page matches the Bundle deals page and works on phones. Codes show as cards with the discount, events, uses, restrictions, dates and limits. On phones, the edit form and the list of uses open full-width from the bottom, and the discount type is picked from three buttons. +- The coupons list hides codes that are off, expired or used up unless you tick "Show codes that are off, expired or used up". Search also matches event names. +- When creating or editing a code, the Events list shows upcoming events only, soonest first, with a search box and a "Show past events" option. Past events already on the code stay listed so they can be unticked. + ## [1.29.0] - 2026-10-21 Run `npm run prisma:deploy` in the backend when deploying. This release adds a migration. diff --git a/apps/events/frontend/src/app/dashboard/supervisor/coupons/page.tsx b/apps/events/frontend/src/app/dashboard/supervisor/coupons/page.tsx index 5cbfa8a..46ddced 100644 --- a/apps/events/frontend/src/app/dashboard/supervisor/coupons/page.tsx +++ b/apps/events/frontend/src/app/dashboard/supervisor/coupons/page.tsx @@ -5,8 +5,7 @@ import { useAuth } from "@/hooks/useAuth"; import { useRouter } from "next/navigation"; import { apiFetch } from "@/lib/api"; import { useDismissingState } from "@/hooks/useDismissingState"; -import { TicketPercent, Plus, X, Search } from "lucide-react"; -import { Table, TableHeader, TableBody, TableRow, TableHead, TableCell } from "@/components/ui/table"; +import { TicketPercent, Plus, X, Search, Pencil, Power, Users, CalendarClock, ListChecks } from "lucide-react"; import { errorMessage } from "@/lib/utils"; import { BackLink } from "@/components/shared/BackLink"; @@ -17,6 +16,7 @@ type EventLite = { id: string; title: string; startDate: string; + endDate?: string; eventOptions: { id: string; name: string; variants?: { id: string; name: string }[] }[]; }; @@ -122,17 +122,19 @@ function bodyFromDraft(d: Draft) { }; } -const fmtDate = (iso: string | null) => (iso ? new Date(iso).toLocaleDateString(undefined, { day: "numeric", month: "short", year: "numeric" }) : ""); +const fmtDate = (iso: string | null | undefined) => (iso ? new Date(iso).toLocaleDateString(undefined, { day: "numeric", month: "short", year: "numeric" }) : ""); const fmtRand = (n: number) => `R ${(n || 0).toFixed(2)}`; const inputCls = "w-full border rounded-lg px-3 py-2 text-sm focus:outline-none focus:ring-2 focus:ring-brand-400"; +const isPast = (ev: EventLite) => new Date(ev.endDate || ev.startDate).getTime() < Date.now(); -function couponStatus(c: Coupon): { label: string; cls: string } { +type Status = { key: "active" | "scheduled" | "off" | "expired" | "used"; label: string; cls: string }; +function couponStatus(c: Coupon): Status { const now = Date.now(); - if (!c.active) return { label: "Off", cls: "bg-gray-100 text-gray-600" }; - if (c.validUntil && new Date(c.validUntil).getTime() < now) return { label: "Expired", cls: "bg-gray-100 text-gray-600" }; - if (c.validFrom && new Date(c.validFrom).getTime() > now) return { label: "Scheduled", cls: "bg-amber-100 text-amber-800" }; - if (c.maxUses && c.uses >= c.maxUses) return { label: "Used up", cls: "bg-gray-100 text-gray-600" }; - return { label: "Active", cls: "bg-green-100 text-green-800" }; + if (!c.active) return { key: "off", label: "Off", cls: "bg-gray-100 text-gray-600" }; + if (c.validUntil && new Date(c.validUntil).getTime() < now) return { key: "expired", label: "Expired", cls: "bg-gray-100 text-gray-600" }; + if (c.maxUses && c.uses >= c.maxUses) return { key: "used", label: "Used up", cls: "bg-gray-100 text-gray-600" }; + if (c.validFrom && new Date(c.validFrom).getTime() > now) return { key: "scheduled", label: "Scheduled", cls: "bg-amber-100 text-amber-800" }; + return { key: "active", label: "Active", cls: "bg-green-100 text-green-800" }; } export default function CouponsPage() { @@ -151,7 +153,7 @@ export default function CouponsPage() { const [error, setError] = useDismissingState(null); const [info, setInfo] = useDismissingState(null); const [query, setQuery] = useState(""); - const [showOff, setShowOff] = useState(false); + const [showInactive, setShowInactive] = useState(false); const [editing, setEditing] = useState<{ id: string | null; draft: Draft } | null>(null); const [viewing, setViewing] = useState<{ coupon: Coupon; redemptions: Redemption[] } | null>(null); @@ -175,10 +177,17 @@ export default function CouponsPage() { useEffect(() => { if (token && canView) load(); }, [token, canView, load]); + // Codes that can still be used first; off, expired and used-up ones only when asked for. const visible = useMemo(() => { const q = query.trim().toLowerCase(); - return coupons.filter(c => (showOff || c.active) && (!q || c.code.toLowerCase().includes(q) || (c.description || "").toLowerCase().includes(q))); - }, [coupons, query, showOff]); + const order: Record = { active: 0, scheduled: 1, used: 2, expired: 3, off: 4 }; + return coupons + .map(c => ({ c, status: couponStatus(c) })) + .filter(({ c, status }) => (showInactive || status.key === "active" || status.key === "scheduled") + && (!q || c.code.toLowerCase().includes(q) || (c.description || "").toLowerCase().includes(q) || c.events.some(e => e.event.title.toLowerCase().includes(q)))) + .sort((a, b) => order[a.status.key] - order[b.status.key]); + }, [coupons, query, showInactive]); + const hiddenCount = coupons.filter(c => !["active", "scheduled"].includes(couponStatus(c).key)).length; const toggleActive = async (c: Coupon) => { try { @@ -199,9 +208,11 @@ export default function CouponsPage() { } }; + const startNew = () => setEditing({ id: null, draft: blankDraft() }); + return ( -
-
+
+
@@ -214,8 +225,8 @@ export default function CouponsPage() {
{canView && ( @@ -230,72 +241,46 @@ export default function CouponsPage() { {canView && (
-
-
+
+
- setQuery(e.target.value)} /> + setQuery(e.target.value)} />
-
- - - - Code - Discount - Events - Uses - Valid - Status - Actions - - - - {visible.map(c => { - const status = couponStatus(c); - return ( - - -
{c.code}
- {c.description &&
{c.description}
} - {(c.restrictRoles.length > 0 || c.allowedUsers.length > 0 || c.restrictDomain) && ( -
- Restricted: {c.allowedUsers.length ? `${c.allowedUsers.length} person${c.allowedUsers.length === 1 ? "" : "s"}` : c.restrictRoles.length ? c.restrictRoles.join(", ") : `@${c.restrictDomain}`} -
- )} -
- -
{c.summary}
- {c.options.length > 0 &&
{c.options.map(o => o.eventOption.name + (o.variant ? ` (${o.variant.name})` : "")).join(", ")}
} -
- {c.events.length ? c.events.map(e => e.event.title).join(", ") : All events} - - {c.uses}{c.maxUses ? ` / ${c.maxUses}` : ""} -
{fmtRand(c.totalDiscount)} given
-
- - {c.validFrom || c.validUntil ? `${c.validFrom ? fmtDate(c.validFrom) : "Now"} – ${c.validUntil ? fmtDate(c.validUntil) : "no end"}` : "Always"} - - {status.label} - - - - - -
- ); - })} - {!fetching && visible.length === 0 && ( - No coupon codes yet. - )} -
-
-
+ {!fetching && visible.length === 0 && ( +
+
+ +
+

{coupons.length === 0 ? "No coupon codes yet" : "No codes match"}

+

e.g. "STAFF50": 50% off for staff, or "WELCOME20": R20 off for first-time visitors.

+ {coupons.length === 0 && ( + + )} +
+ )} + + {visible.length > 0 && ( +
+ {visible.map(({ c, status }) => ( + openUses(c)} + onEdit={() => setEditing({ id: c.id, draft: draftFromCoupon(c) })} + onToggle={() => toggleActive(c)} + /> + ))} +
+ )}
)} @@ -311,47 +296,119 @@ export default function CouponsPage() { )} {viewing && ( -
setViewing(null)}> -
e.stopPropagation()}> -
-

Uses of {viewing.coupon.code}

- -
-
- {viewing.redemptions.length === 0 ? ( -

This code hasn't been used yet.

- ) : ( - - - PersonEventDiscountWhen - - - {viewing.redemptions.map(r => ( - - -
{r.user.name}
-
{r.user.email}
- {r.appliedBy &&
Applied by {r.appliedBy.name}
} -
- {r.registration.event.title} - - {fmtRand(r.discountAmount)} - {r.releasedAt &&
Released (cancelled)
} -
- {fmtDate(r.createdAt)} -
- ))} -
-
- )} -
-
-
+ Uses of {viewing.coupon.code}} onClose={() => setViewing(null)}> + {viewing.redemptions.length === 0 ? ( +

This code hasn't been used yet.

+ ) : ( +
    + {viewing.redemptions.map(r => ( +
  • +
    +
    {r.user.name}
    +
    {r.user.email}
    +
    {r.registration.event.title} · {fmtDate(r.createdAt)}
    + {r.appliedBy &&
    Applied by {r.appliedBy.name}
    } +
    +
    +
    {fmtRand(r.discountAmount)}
    + {r.releasedAt &&
    Released (cancelled)
    } +
    +
  • + ))} +
+ )} +
)}
); } +/** A dialog that slides up full-width on phones and sits centred on bigger screens. */ +function Sheet({ title, subtitle, onClose, children, footer, onSubmit }: { + title: React.ReactNode; + subtitle?: React.ReactNode; + onClose: () => void; + children: React.ReactNode; + footer?: React.ReactNode; + onSubmit?: (e: React.FormEvent) => void; +}) { + const Box = onSubmit ? "form" : "div"; + return ( +
+ e.stopPropagation()}> +
+
+

{title}

+ {subtitle &&

{subtitle}

} +
+ +
+
{children}
+ {footer &&
{footer}
} +
+
+ ); +} + +function CouponCard({ coupon: c, status, onUses, onEdit, onToggle }: { coupon: Coupon; status: Status; onUses: () => void; onEdit: () => void; onToggle: () => void }) { + const restricted = c.allowedUsers.length + ? `${c.allowedUsers.length} ${c.allowedUsers.length === 1 ? "person" : "people"}` + : c.restrictRoles.length ? c.restrictRoles.map(r => (r === "user" ? "members" : r)).join(", ") : c.restrictDomain ? `@${c.restrictDomain}` : null; + const dates = c.validFrom || c.validUntil ? `${c.validFrom ? fmtDate(c.validFrom) : "Now"} – ${c.validUntil ? fmtDate(c.validUntil) : "no end date"}` : null; + const limits = [ + c.maxUses ? `${c.maxUses} uses in total` : null, + c.maxUsesPerUser ? `${c.maxUsesPerUser} per person` : null, + c.minQuantity ? `min ${c.minQuantity} tickets` : null, + ].filter(Boolean).join(" · "); + const live = status.key === "active" || status.key === "scheduled"; + + return ( +
+
+
+
{c.code}
+ {c.description &&
{c.description}
} +
+ {status.label} +
+ +
+ {c.summary} + {c.options.length > 0 && ( + on {c.options.map(o => o.eventOption.name + (o.variant ? ` (${o.variant.name})` : "")).join(", ")} + )} +
+ +
+ {c.events.length === 0 + ? All events + : c.events.map(e => ( + {e.event.title} + ))} +
+ +
+
Used {c.uses}{c.maxUses ? ` / ${c.maxUses}` : ""}× · {fmtRand(c.totalDiscount)} given
+ {restricted &&
Only for {restricted}
} + {dates &&
{dates}
} + {limits &&
{limits}
} +
+ +
+ + + +
+
+ ); +} + function CouponModal({ token, events, couponId, initial, onClose, onSaved }: { token: string; events: EventLite[]; @@ -365,8 +422,22 @@ function CouponModal({ token, events, couponId, initial, onClose, onSaved }: { const [error, setError] = useState(null); const [userQuery, setUserQuery] = useState(""); const [userResults, setUserResults] = useState<{ id: string; name: string; email: string }[]>([]); + const [eventQuery, setEventQuery] = useState(""); + const [showPast, setShowPast] = useState(false); const set = (key: K, value: Draft[K]) => setD(prev => ({ ...prev, [key]: value })); + // Upcoming events soonest first. Past events only when asked for, but ones already + // picked always stay in the list so they can be unticked. + const eventChoices = useMemo(() => { + const q = eventQuery.trim().toLowerCase(); + const upcoming = events.filter(e => !isPast(e)).sort((a, b) => new Date(a.startDate).getTime() - new Date(b.startDate).getTime()); + const past = events.filter(isPast).sort((a, b) => new Date(b.startDate).getTime() - new Date(a.startDate).getTime()); + return [...upcoming, ...past] + .filter(e => showPast || !isPast(e) || d.eventIds.includes(e.id)) + .filter(e => !q || e.title.toLowerCase().includes(q)); + }, [events, eventQuery, showPast, d.eventIds]); + const pastCount = events.filter(isPast).length; + // Ticket options only make sense within the chosen events. const scopedEvents = useMemo(() => events.filter(e => d.eventIds.includes(e.id)), [events, d.eventIds]); @@ -415,184 +486,200 @@ function CouponModal({ token, events, couponId, initial, onClose, onSaved }: { } }; + const label = "block text-sm font-medium text-gray-700 mb-1"; + const optional = (optional); + return ( -
-
e.stopPropagation()}> -
-

{couponId ? "Edit code" : "New coupon code"}

- -
- -
-
-
- - set("code", e.target.value.toUpperCase().replace(/[^A-Z0-9_-]/g, ""))} placeholder="STAFF50" required maxLength={32} /> -

Letters, numbers, - and _. Not case-sensitive when entered.

-
-
- - set("description", e.target.value)} placeholder="Staff discount for Camp 2026" /> -
-
- -
-
- - -
- {d.type !== "comp" && ( -
- - set("value", e.target.value)} required /> -
- )} -
- -
-
- -
- -

Leave all unticked for every event.

-
- {events.map(ev => ( - - ))} - {events.length === 0 &&

No events.

} -
-
- - {scopedEvents.length > 0 && ( -
- -

Leave all unticked to discount the whole registration.

-
- {scopedEvents.map(ev => ( -
-
{ev.title}
- {ev.eventOptions.map(o => ( -
- - {(o.variants || []).map(v => ( - - ))} -
- ))} -
- ))} -
-
- )} - -
-
- - set("validFrom", e.target.value)} /> -
-
- - set("validUntil", e.target.value)} /> -
-
- -
-
- - set("maxUses", e.target.value)} placeholder="Unlimited" /> -
-
- - set("maxUsesPerUser", e.target.value)} placeholder="Unlimited" /> -
-
- - set("minQuantity", e.target.value)} placeholder="None" /> -
-
- -
- - - {d.restrictTo !== "anyone" && ( -

Restricted codes need the person to be logged in (or registered by staff), so they can't be shared in group chats.

- )} - - {d.restrictTo === "roles" && ( -
- {(["user", "staff", "supervisor", "admin"] as Role[]).map(r => ( - - ))} -
- )} - - {d.restrictTo === "domain" && ( - set("restrictDomain", e.target.value)} placeholder="church.org" required /> - )} - - {d.restrictTo === "users" && ( -
-
- {d.users.map(u => ( - - {u.name} - - - ))} -
- setUserQuery(e.target.value)} placeholder="Search by name, email or phone" /> - {userResults.length > 0 && ( -
- {userResults.filter(r => !d.users.some(u => u.id === r.id)).map(r => ( - - ))} -
- )} -
- )} -
- - - - {error &&
{error}
} -
- -
+ + + )} + > +
+
+
+ + set("code", e.target.value.toUpperCase().replace(/[^A-Z0-9_-]/g, ""))} placeholder="STAFF50" required maxLength={32} /> +

Letters, numbers, - and _. Not case-sensitive when entered.

+
+
+ + set("description", e.target.value)} placeholder="Staff discount for Camp 2026" /> +

Only staff see this.

+
- -
+ +
+ +
+ {([["percent", "Percentage", "e.g. 20% off"], ["fixed", "Amount off", "e.g. R50 off"], ["comp", "Free", "100% off"]] as [CouponType, string, string][]).map(([t, name, hint]) => ( + + ))} +
+ {d.type !== "comp" && ( +
+ + set("value", e.target.value)} required /> +
+ )} + +
+ +
+ +

+ {d.eventIds.length === 0 ? "None picked: the code works for every event." : `${d.eventIds.length} picked. The code only works for ${d.eventIds.length === 1 ? "this event" : "these events"}.`} +

+
+
+ + setEventQuery(e.target.value)} /> +
+ {pastCount > 0 && ( + + )} +
+
+ {eventChoices.map(ev => ( + + ))} + {eventChoices.length === 0 &&

{eventQuery ? "No events match." : "No upcoming events."}

} +
+
+ + {scopedEvents.length > 0 && ( +
+ +

Leave all unticked to discount the whole registration.

+
+ {scopedEvents.map(ev => ( +
+
{ev.title}
+ {ev.eventOptions.map(o => ( +
+ + {(o.variants || []).map(v => ( + + ))} +
+ ))} +
+ ))} +
+
+ )} + +
+
+ + set("validFrom", e.target.value)} /> +
+
+ + set("validUntil", e.target.value)} /> +
+
+ +
+
+ + set("maxUses", e.target.value)} placeholder="Unlimited" /> +
+
+ + set("maxUsesPerUser", e.target.value)} placeholder="Unlimited" /> +
+
+ + set("minQuantity", e.target.value)} placeholder="None" /> +
+
+ +
+ + + {d.restrictTo !== "anyone" && ( +

Restricted codes need the person to be logged in (or registered by staff), so they can't be shared in group chats.

+ )} + + {d.restrictTo === "roles" && ( +
+ {(["user", "staff", "supervisor", "admin"] as Role[]).map(r => ( + + ))} +
+ )} + + {d.restrictTo === "domain" && ( + set("restrictDomain", e.target.value)} placeholder="church.org" required /> + )} + + {d.restrictTo === "users" && ( +
+
+ {d.users.map(u => ( + + {u.name} + + + ))} +
+ setUserQuery(e.target.value)} placeholder="Search by name, email or phone" /> + {userResults.length > 0 && ( +
+ {userResults.filter(r => !d.users.some(u => u.id === r.id)).map(r => ( + + ))} +
+ )} +
+ )} +
+ + + + {error &&
{error}
} +
+ ); }