feat: finalize ticket merging and speaker-aware imports
This commit is contained in:
1 parent
648daa0794
commit
4e71e9b797
35 files changed
+1772
-348
No files matched your search
@@ -91,14 +91,16 @@ export function Toggle({
|
||||
checked,
|
||||
onChange,
|
||||
label,
|
||||
disabled = false,
|
||||
}: {
|
||||
checked: boolean;
|
||||
onChange: (v: boolean) => void;
|
||||
label?: string;
|
||||
disabled?: boolean;
|
||||
}) {
|
||||
return (
|
||||
<label className="toggle" title={label}>
|
||||
<input type="checkbox" checked={checked} onChange={(e) => onChange(e.target.checked)} />
|
||||
<input type="checkbox" checked={checked} disabled={disabled} onChange={(e) => onChange(e.target.checked)} />
|
||||
<span className="track">
|
||||
<span className="knob" />
|
||||
</span>
|
||||
|
||||
@@ -153,10 +153,14 @@ export async function setRegistrationType(contactEmail: string, type: Registrati
|
||||
}
|
||||
|
||||
export async function setCheckedIn(registrationId: string, checkedIn: boolean): Promise<void> {
|
||||
const { error } = await supabase.from('registrations').update({ checked_in: checkedIn }).eq('id', registrationId);
|
||||
const { data, error } = await supabase.from('registrations').update({ checked_in: checkedIn }).eq('id', registrationId).select('id').single();
|
||||
if (error) throw new ApiError(error.message);
|
||||
if (!data) throw new ApiError('Registration no longer exists');
|
||||
}
|
||||
|
||||
export const mergeTickets = (sourceId: string, targetId: string) =>
|
||||
rpc<import('./types').TicketMergeResult>('merge_tickets', { p_source_id: sourceId, p_target_id: targetId });
|
||||
|
||||
// ---- Sponsors ----------------------------------------------------------------
|
||||
|
||||
export async function upsertSponsor(
|
||||
|
||||
@@ -172,6 +172,8 @@ export interface SidecarStatus {
|
||||
}
|
||||
|
||||
export interface Registration {
|
||||
import_source?: 'sessionize' | null;
|
||||
external_id?: string | null;
|
||||
id: string;
|
||||
event_id: string;
|
||||
contact_email: string;
|
||||
@@ -182,6 +184,14 @@ export interface Registration {
|
||||
created_at: string;
|
||||
}
|
||||
|
||||
export interface TicketMergeResult {
|
||||
source_id: string;
|
||||
target_id: string;
|
||||
target_email: string;
|
||||
type: RegistrationType;
|
||||
sessions_transferred: number;
|
||||
}
|
||||
|
||||
export interface Sponsor {
|
||||
id: string;
|
||||
event_id: string;
|
||||
|
||||
@@ -1,4 +1,4 @@
|
||||
import { useEffect, useMemo, useState } from 'react';
|
||||
import { useEffect, useMemo, useRef, useState } from 'react';
|
||||
import {
|
||||
createRegistration,
|
||||
deleteContacts,
|
||||
@@ -8,6 +8,7 @@ import {
|
||||
getRegistrations,
|
||||
getSponsors,
|
||||
setCheckedIn,
|
||||
mergeTickets,
|
||||
setRegistrationType,
|
||||
upsertSponsor,
|
||||
} from '../lib/api';
|
||||
@@ -33,8 +34,18 @@ export function Registrations() {
|
||||
const [sponsorFor, setSponsorFor] = useState<Registration | null>(null);
|
||||
const [busy, setBusy] = useState(false);
|
||||
const [deletingAll, setDeletingAll] = useState(false);
|
||||
const [mergeFor, setMergeFor] = useState<Registration | null>(null);
|
||||
const selectedEventId = useRef(selectedId);
|
||||
selectedEventId.current = selectedId;
|
||||
|
||||
const event = events.find((e) => e.id === selectedId) ?? null;
|
||||
const eventRegistrations = registrations.filter((registration) => registration.event_id === selectedId);
|
||||
const ticketed = eventRegistrations.filter((registration) => registration.qr_token?.trim());
|
||||
|
||||
async function reloadRegistrations(eventId: string) {
|
||||
const rows = await getRegistrations(eventId);
|
||||
if (selectedEventId.current === eventId) setRegistrations(rows);
|
||||
}
|
||||
|
||||
async function load() {
|
||||
try {
|
||||
@@ -106,8 +117,9 @@ export function Registrations() {
|
||||
const filtered = useMemo(() => {
|
||||
if (!registrations) return [];
|
||||
const q = query.trim().toLowerCase();
|
||||
if (!q) return registrations;
|
||||
if (!q) return registrations.filter((r) => r.event_id === selectedId);
|
||||
return registrations.filter((r) => {
|
||||
if (r.event_id !== selectedId) return false;
|
||||
const c = byEmail.get(r.contact_email);
|
||||
return (
|
||||
(c?.full_name ?? '').toLowerCase().includes(q) ||
|
||||
@@ -115,13 +127,13 @@ export function Registrations() {
|
||||
r.type.toLowerCase().includes(q)
|
||||
);
|
||||
});
|
||||
}, [registrations, query, byEmail]);
|
||||
}, [registrations, query, byEmail, selectedId]);
|
||||
|
||||
async function changeType(r: Registration, type: RegistrationType) {
|
||||
setBusy(true);
|
||||
try {
|
||||
await setRegistrationType(r.contact_email, type);
|
||||
await load();
|
||||
await reloadRegistrations(r.event_id);
|
||||
push('success', `${byEmail.get(r.contact_email)?.full_name ?? r.contact_email} → ${type}`);
|
||||
} catch (e) {
|
||||
const msg = e instanceof Error ? e.message : 'Update failed';
|
||||
@@ -139,10 +151,11 @@ export function Registrations() {
|
||||
|
||||
async function toggleCheckedIn(r: Registration, v: boolean) {
|
||||
setBusy(true);
|
||||
setRegistrations((rows) => rows.map((row) => row.id === r.id ? { ...row, checked_in: v } : row));
|
||||
try {
|
||||
await setCheckedIn(r.id, v);
|
||||
await load();
|
||||
} catch (e) {
|
||||
setRegistrations((rows) => rows.map((row) => row.id === r.id ? { ...row, checked_in: r.checked_in } : row));
|
||||
push('error', e instanceof Error ? e.message : 'Update failed');
|
||||
} finally {
|
||||
setBusy(false);
|
||||
@@ -181,7 +194,7 @@ export function Registrations() {
|
||||
<div className="sub">
|
||||
<strong>{event.name}</strong>
|
||||
{event.is_active && <span className="pill green" style={{ marginLeft: 8 }}>active</span>} ·{' '}
|
||||
{registrations.length} registrations · {registrations.filter((r) => r.checked_in).length} checked in.
|
||||
{ticketed.length} tickets · {ticketed.filter((r) => r.checked_in).length} checked in · {eventRegistrations.length - ticketed.length} without tickets.
|
||||
</div>
|
||||
</div>
|
||||
<div className="row" style={{ gap: 10 }}>
|
||||
@@ -189,6 +202,7 @@ export function Registrations() {
|
||||
className="input"
|
||||
style={{ maxWidth: 280 }}
|
||||
value={selectedId}
|
||||
disabled={busy || mergeFor !== null}
|
||||
onChange={(e) => setSelectedId(e.target.value)}
|
||||
>
|
||||
{events.map((e) => (
|
||||
@@ -260,13 +274,18 @@ export function Registrations() {
|
||||
<td>
|
||||
<div className="row" style={{ gap: 8 }}>
|
||||
<span className="mono small">{r.qr_token ?? '—'}</span>
|
||||
<button className="btn small" onClick={() => setQrFor(r)}>
|
||||
{r.qr_token?.trim() && <button className="btn small" onClick={() => setQrFor(r)}>
|
||||
QR
|
||||
</button>
|
||||
</button>}
|
||||
{!r.qr_token?.trim() && r.import_source === 'sessionize' && (
|
||||
<button className="btn small" disabled={busy} onClick={() => setMergeFor(r)}>
|
||||
Merge tickets
|
||||
</button>
|
||||
)}
|
||||
</div>
|
||||
</td>
|
||||
<td>
|
||||
<Toggle checked={r.checked_in} onChange={(v) => toggleCheckedIn(r, v)} />
|
||||
<Toggle checked={r.checked_in} disabled={busy} onChange={(v) => toggleCheckedIn(r, v)} />
|
||||
</td>
|
||||
<td style={{ textAlign: 'right' }}>
|
||||
<span className="pts">{r.points}</span>
|
||||
@@ -286,12 +305,28 @@ export function Registrations() {
|
||||
onClose={() => setAdding(false)}
|
||||
onSaved={async (msg) => {
|
||||
setAdding(false);
|
||||
await load();
|
||||
await reloadRegistrations(event.id);
|
||||
push('success', msg);
|
||||
}}
|
||||
/>
|
||||
)}
|
||||
|
||||
{mergeFor && (
|
||||
<MergeTicketsModal
|
||||
source={mergeFor}
|
||||
contacts={byEmail}
|
||||
tickets={ticketed.filter((ticket) => ticket.id !== mergeFor.id && ticket.external_id?.trim())}
|
||||
onClose={() => setMergeFor(null)}
|
||||
onMerged={async (result) => {
|
||||
setRegistrations((rows) => rows.filter((row) => row.id !== result.source_id).map((row) => row.id === result.target_id ? { ...row, type: result.type } : row));
|
||||
try { await reloadRegistrations(mergeFor.event_id); }
|
||||
catch { push('error', 'Merge saved; reload registrations to see the updated ticket.'); }
|
||||
setMergeFor(null);
|
||||
push('success', `Merged into ${result.target_email}; ${result.sessions_transferred} session assignment(s) transferred.`);
|
||||
}}
|
||||
/>
|
||||
)}
|
||||
|
||||
{qrFor && (
|
||||
<Modal
|
||||
open
|
||||
@@ -330,6 +365,61 @@ export function Registrations() {
|
||||
);
|
||||
}
|
||||
|
||||
function MergeTicketsModal({ source, contacts, tickets, onClose, onMerged }: {
|
||||
source: Registration;
|
||||
contacts: Map<string, Contact>;
|
||||
tickets: Registration[];
|
||||
onClose: () => void;
|
||||
onMerged: (result: import('../lib/types').TicketMergeResult) => Promise<void>;
|
||||
}) {
|
||||
const [search, setSearch] = useState('');
|
||||
const [selected, setSelected] = useState('');
|
||||
const [saving, setSaving] = useState(false);
|
||||
const [failure, setFailure] = useState('');
|
||||
const sourceName = contacts.get(source.contact_email)?.full_name ?? source.contact_email;
|
||||
const words = (name: string) => name.toLocaleLowerCase().normalize('NFD').replace(/[\u0300-\u036f]/g, '').split(/[^a-z0-9]+/).filter(Boolean);
|
||||
const sourceWords = words(sourceName);
|
||||
const score = (ticket: Registration) => words(contacts.get(ticket.contact_email)?.full_name ?? '').filter((word) => sourceWords.includes(word)).length;
|
||||
const options = tickets.filter((ticket) => `${contacts.get(ticket.contact_email)?.full_name ?? ''} ${ticket.contact_email}`.toLowerCase().includes(search.trim().toLowerCase()))
|
||||
.sort((first, second) => score(second) - score(first) || first.contact_email.localeCompare(second.contact_email));
|
||||
const target = tickets.find((ticket) => ticket.id === selected);
|
||||
async function save() {
|
||||
if (!target || saving) return;
|
||||
setSaving(true);
|
||||
setFailure('');
|
||||
try { await onMerged(await mergeTickets(source.id, target.id)); }
|
||||
catch (error) {
|
||||
setFailure(error instanceof Error ? error.message : 'Merge failed');
|
||||
setSaving(false);
|
||||
}
|
||||
}
|
||||
return (
|
||||
<Modal open title="Merge tickets" onClose={() => { if (!saving) onClose(); }} footer={
|
||||
<>
|
||||
<button className="btn" disabled={saving} onClick={onClose}>Cancel</button>
|
||||
<button className="btn primary" disabled={!target || saving} onClick={save}>{saving ? 'Merging...' : 'Confirm merge'}</button>
|
||||
</>
|
||||
}>
|
||||
<p><strong>{sourceName}</strong><br /><span className="muted">{source.contact_email}</span></p>
|
||||
<label className="field">
|
||||
<span>Destination ticket</span>
|
||||
<input className="input" value={search} disabled={saving} placeholder="Search name or email" onChange={(change) => setSearch(change.target.value)} />
|
||||
</label>
|
||||
<div style={{ maxHeight: 280, overflowY: 'auto', marginTop: 12 }} role="radiogroup" aria-label="Destination ticket">
|
||||
{options.map((ticket) => (
|
||||
<label key={ticket.id} className="row" style={{ padding: '10px 0', gap: 10 }}>
|
||||
<input type="radio" name="merge-ticket" disabled={saving} checked={selected === ticket.id} onChange={() => setSelected(ticket.id)} />
|
||||
<span><strong>{contacts.get(ticket.contact_email)?.full_name ?? ticket.contact_email}</strong><br />{ticket.contact_email}{score(ticket) > 0 && <span className="pill green" style={{ marginLeft: 8 }}>Name match</span>}</span>
|
||||
</label>
|
||||
))}
|
||||
{options.length === 0 && <p className="muted">No matching purchased tickets.</p>}
|
||||
</div>
|
||||
{target && <p>The ticket for <strong>{target.contact_email}</strong> keeps its QR, check-in and points. Session assignments and the {source.type} role transfer before the imported registration is deleted. Other events are unchanged.</p>}
|
||||
{failure && <p role="alert" className="error">{failure}</p>}
|
||||
</Modal>
|
||||
);
|
||||
}
|
||||
|
||||
function AddRegistrationModal({
|
||||
contacts,
|
||||
existing,
|
||||
|
||||
Reference in new issue
Block a user