- docs/backend/pretix.md: Pretix REST API reference (auth, orders/items endpoints, pagination, rate limiting, worked example) - migration 20260909000000_pretix_sync.sql: events.pretix_organizer + pretix_event_slug; _map_ticket_type, _ensure_auth_user (QR token = password), sync_pretix_attendees (service-role-only, idempotent) - sidecar: pretixFetch/fetchPretixOrders/fetchPretixItems/transformPretix, runPretixSync + every-minute runPretixLoop, POST /run-pretix, /status extended with pretix state - admin: Pretix panel + recent runs in Interfaces, per-event Pretix config + Sync button in Events, api/types for Pretix - docker-compose: PRETIX_API_KEY / PRETIX_BASE / PRETIX_SYNC_INTERVAL_MS Verified on VM: 25 paid orders -> 25 attendees, auto-sync every minute, login via QR token works, idempotent (no duplicate registrations). Co-authored-by: Stephan Charles <[email protected]> Co-authored-by: Amelia Hernandez <[email protected]> Co-authored-by: Markus Franz <[email protected]> Co-authored-by: Sabrina Hota <[email protected]> Co-authored-by: Nilüfer Doğan <[email protected]>
472 lines
16 KiB
TypeScript
472 lines
16 KiB
TypeScript
import { useEffect, useState } from 'react';
|
||
import {
|
||
activateEvent,
|
||
deactivateEvent,
|
||
getAllEvents,
|
||
runPretixSync,
|
||
runSessionizeSync,
|
||
upsertEvent,
|
||
} from '../lib/api';
|
||
import type { Event, PretixSyncResult, SyncResult } from '../lib/types';
|
||
import { useToast } from '../context/ToastContext';
|
||
import { Empty, Modal, Spinner, Toggle } from '../components/ui';
|
||
|
||
export function Events() {
|
||
const { push } = useToast();
|
||
const [events, setEvents] = useState<Event[]>([]);
|
||
const [loaded, setLoaded] = useState(false);
|
||
const [error, setError] = useState<string | null>(null);
|
||
const [modal, setModal] = useState<{ event: Event | null } | null>(null);
|
||
const [syncingId, setSyncingId] = useState<string | null>(null);
|
||
const [syncResult, setSyncResult] = useState<SyncResult | null>(null);
|
||
const [pretixSyncingId, setPretixSyncingId] = useState<string | null>(null);
|
||
const [pretixResult, setPretixResult] = useState<PretixSyncResult | null>(null);
|
||
|
||
async function load() {
|
||
try {
|
||
setEvents(await getAllEvents());
|
||
setLoaded(true);
|
||
} catch (e) {
|
||
setError(e instanceof Error ? e.message : 'Failed to load');
|
||
}
|
||
}
|
||
|
||
useEffect(() => {
|
||
load();
|
||
}, []);
|
||
|
||
async function toggleActive(ev: Event, active: boolean) {
|
||
if (active && !confirm(`Set "${ev.name}" as the active event? All other events will be deactivated.`)) return;
|
||
try {
|
||
if (active) await activateEvent(ev.id);
|
||
else await deactivateEvent(ev.id);
|
||
await load();
|
||
push('success', active ? `${ev.name} is now active` : `${ev.name} deactivated`);
|
||
} catch (e) {
|
||
push('error', e instanceof Error ? e.message : 'Failed to update event');
|
||
}
|
||
}
|
||
|
||
async function runSync(ev: Event) {
|
||
if (!ev.sessionize_endpoint) {
|
||
push('error', `Set a Sessionize endpoint for ${ev.name} first (Edit)`);
|
||
return;
|
||
}
|
||
setSyncingId(ev.id);
|
||
try {
|
||
const result = await runSessionizeSync(ev.id);
|
||
setSyncResult(result);
|
||
await load();
|
||
push('success', `Synced ${ev.name}`);
|
||
} catch (e) {
|
||
push('error', e instanceof Error ? e.message : 'Sync failed');
|
||
} finally {
|
||
setSyncingId(null);
|
||
}
|
||
}
|
||
|
||
async function runPretix(ev: Event) {
|
||
if (!ev.pretix_organizer || !ev.pretix_event_slug) {
|
||
push('error', `Set the Pretix organizer + event slug for ${ev.name} first (Edit)`);
|
||
return;
|
||
}
|
||
setPretixSyncingId(ev.id);
|
||
try {
|
||
const result = await runPretixSync(ev.id);
|
||
setPretixResult(result);
|
||
await load();
|
||
push(
|
||
'success',
|
||
result.status === 'success'
|
||
? `Pretix synced ${ev.name} (${result.attendees ?? 0} attendees)`
|
||
: `Pretix ${result.status} for ${ev.name}`,
|
||
);
|
||
} catch (e) {
|
||
push('error', e instanceof Error ? e.message : 'Pretix sync failed');
|
||
} finally {
|
||
setPretixSyncingId(null);
|
||
}
|
||
}
|
||
|
||
if (error) return <Empty icon="⚠️" text={error} />;
|
||
if (!loaded)
|
||
return (
|
||
<div className="loading-screen">
|
||
<Spinner />
|
||
</div>
|
||
);
|
||
|
||
return (
|
||
<div>
|
||
<div className="page-head">
|
||
<div>
|
||
<h1>Events</h1>
|
||
<div className="sub">
|
||
{events.length} event{events.length === 1 ? '' : 's'} · only one can be active at a time.
|
||
</div>
|
||
</div>
|
||
<button className="btn primary" onClick={() => setModal({ event: null })}>
|
||
+ New event
|
||
</button>
|
||
</div>
|
||
|
||
<div className="card">
|
||
{events.length === 0 ? (
|
||
<Empty icon="📅" text="No events yet. Create one to get started." />
|
||
) : (
|
||
<div className="table-wrap">
|
||
<table className="data">
|
||
<thead>
|
||
<tr>
|
||
<th>Event</th>
|
||
<th>Dates</th>
|
||
<th>Location</th>
|
||
<th>Stats</th>
|
||
<th>Status</th>
|
||
<th></th>
|
||
</tr>
|
||
</thead>
|
||
<tbody>
|
||
{events.map((ev) => (
|
||
<tr key={ev.id}>
|
||
<td>
|
||
<div style={{ fontWeight: 700 }}>{ev.name}</div>
|
||
{ev.is_active && <span className="pill green">active</span>}
|
||
</td>
|
||
<td className="muted">
|
||
{fmtDate(ev.start_date)} – {fmtDate(ev.end_date)}
|
||
</td>
|
||
<td className="muted">{ev.location || '—'}</td>
|
||
<td className="muted small">
|
||
{ev.num_registrations} reg · {ev.num_sessions} sessions · {ev.num_speakers} speakers
|
||
{ev.avg_feedback != null && ` · ⭐ ${ev.avg_feedback.toFixed(1)}`}
|
||
</td>
|
||
<td>
|
||
<Toggle
|
||
checked={ev.is_active}
|
||
onChange={(v) => toggleActive(ev, v)}
|
||
label={ev.is_active ? 'Active' : 'Inactive'}
|
||
/>
|
||
</td>
|
||
<td>
|
||
<div style={{ display: 'flex', gap: 8, justifyContent: 'flex-end' }}>
|
||
<button
|
||
className="btn small"
|
||
disabled={syncingId === ev.id}
|
||
onClick={() => runSync(ev)}
|
||
title={
|
||
ev.sessionize_endpoint
|
||
? 'Sync with Sessionize'
|
||
: 'Set a Sessionize endpoint first (Edit)'
|
||
}
|
||
>
|
||
{syncingId === ev.id ? 'Syncing…' : 'Sync'}
|
||
</button>
|
||
<button
|
||
className="btn small"
|
||
disabled={pretixSyncingId === ev.id || !ev.pretix_organizer || !ev.pretix_event_slug}
|
||
onClick={() => runPretix(ev)}
|
||
title={
|
||
ev.pretix_organizer && ev.pretix_event_slug
|
||
? 'Sync with Pretix'
|
||
: 'Set the Pretix organizer + event slug first (Edit)'
|
||
}
|
||
>
|
||
{pretixSyncingId === ev.id ? 'Syncing…' : 'Sync Pretix'}
|
||
</button>
|
||
<button className="btn small" onClick={() => setModal({ event: ev })}>
|
||
Edit
|
||
</button>
|
||
</div>
|
||
</td>
|
||
</tr>
|
||
))}
|
||
</tbody>
|
||
</table>
|
||
</div>
|
||
)}
|
||
</div>
|
||
|
||
{modal && (
|
||
<EventModal
|
||
event={modal.event}
|
||
onClose={() => setModal(null)}
|
||
onSaved={async (msg) => {
|
||
setModal(null);
|
||
await load();
|
||
push('success', msg);
|
||
}}
|
||
/>
|
||
)}
|
||
|
||
{syncResult && <SyncResultModal result={syncResult} onClose={() => setSyncResult(null)} />}
|
||
{pretixResult && <PretixResultModal result={pretixResult} onClose={() => setPretixResult(null)} />}
|
||
</div>
|
||
);
|
||
}
|
||
|
||
function PretixResultModal({ result, onClose }: { result: PretixSyncResult; onClose: () => void }) {
|
||
const s = result.summary;
|
||
return (
|
||
<Modal
|
||
open
|
||
title={`Pretix sync — ${result.event_name ?? result.event_id}`}
|
||
onClose={onClose}
|
||
footer={
|
||
<button className="btn primary" onClick={onClose}>
|
||
Close
|
||
</button>
|
||
}
|
||
>
|
||
{result.status === 'error' || result.status === 'skipped' ? (
|
||
<div className="pill red" style={{ display: 'inline-block', padding: '8px 12px' }}>
|
||
{result.error ?? result.reason ?? 'Pretix sync failed'}
|
||
</div>
|
||
) : (
|
||
<>
|
||
<div className="stat-grid">
|
||
<div className="stat">
|
||
<div className="val">{result.orders ?? 0}</div>
|
||
<div className="lbl">Orders</div>
|
||
</div>
|
||
<div className="stat">
|
||
<div className="val">{result.attendees ?? 0}</div>
|
||
<div className="lbl">Attendees</div>
|
||
</div>
|
||
<div className="stat">
|
||
<div className="val">{s?.created ?? 0}</div>
|
||
<div className="lbl">Created</div>
|
||
</div>
|
||
<div className="stat">
|
||
<div className="val">{s?.updated ?? 0}</div>
|
||
<div className="lbl">Updated</div>
|
||
</div>
|
||
<div className="stat">
|
||
<div className="val">{result.skipped ?? 0}</div>
|
||
<div className="lbl">Skipped</div>
|
||
</div>
|
||
</div>
|
||
{s && s.errors.length > 0 && (
|
||
<div style={{ marginTop: 12 }}>
|
||
<div className="pill red" style={{ display: 'inline-block', marginBottom: 8 }}>
|
||
{s.errors.length} error(s)
|
||
</div>
|
||
<ul style={{ margin: 0, paddingLeft: 18 }}>
|
||
{s.errors.map((e, i) => (
|
||
<li key={i} className="small">
|
||
{e}
|
||
</li>
|
||
))}
|
||
</ul>
|
||
</div>
|
||
)}
|
||
</>
|
||
)}
|
||
</Modal>
|
||
);
|
||
}
|
||
|
||
function EventModal({
|
||
event,
|
||
onClose,
|
||
onSaved,
|
||
}: {
|
||
event: Event | null;
|
||
onClose: () => void;
|
||
onSaved: (msg: string) => void;
|
||
}) {
|
||
const [name, setName] = useState(event?.name ?? '');
|
||
const [startDate, setStartDate] = useState(event ? event.start_date.slice(0, 10) : '');
|
||
const [endDate, setEndDate] = useState(event ? event.end_date.slice(0, 10) : '');
|
||
const [location, setLocation] = useState(event?.location ?? '');
|
||
const [endpoint, setEndpoint] = useState(event?.sessionize_endpoint ?? '');
|
||
const [pretixOrganizer, setPretixOrganizer] = useState(event?.pretix_organizer ?? '');
|
||
const [pretixEventSlug, setPretixEventSlug] = useState(event?.pretix_event_slug ?? '');
|
||
const [realtimeClock, setRealtimeClock] = useState(event?.use_realtime_clock ?? false);
|
||
const [busy, setBusy] = useState(false);
|
||
|
||
async function save() {
|
||
if (!name.trim() || !startDate || !endDate) return;
|
||
setBusy(true);
|
||
try {
|
||
await upsertEvent(event?.id ?? null, {
|
||
name: name.trim(),
|
||
start_date: startDate,
|
||
end_date: endDate,
|
||
location: location.trim() || null,
|
||
sessionize_endpoint: endpoint.trim() || null,
|
||
pretix_organizer: pretixOrganizer.trim() || null,
|
||
pretix_event_slug: pretixEventSlug.trim() || null,
|
||
use_realtime_clock: realtimeClock,
|
||
});
|
||
onSaved(event ? 'Event updated' : 'Event created');
|
||
} catch (e) {
|
||
onSaved(e instanceof Error ? e.message : 'Save failed');
|
||
} finally {
|
||
setBusy(false);
|
||
}
|
||
}
|
||
|
||
return (
|
||
<Modal
|
||
open
|
||
title={event ? 'Edit event' : 'New event'}
|
||
onClose={onClose}
|
||
footer={
|
||
<>
|
||
<button className="btn" onClick={onClose}>
|
||
Cancel
|
||
</button>
|
||
<button className="btn primary" disabled={busy || !name.trim() || !startDate || !endDate} onClick={save}>
|
||
{busy ? 'Saving…' : 'Save'}
|
||
</button>
|
||
</>
|
||
}
|
||
>
|
||
<div className="field">
|
||
<label>Name</label>
|
||
<input className="input" value={name} onChange={(e) => setName(e.target.value)} placeholder="e.g. APPE 2027" />
|
||
</div>
|
||
<div className="grid-2">
|
||
<div className="field">
|
||
<label>Start date</label>
|
||
<input className="input" type="date" value={startDate} onChange={(e) => setStartDate(e.target.value)} />
|
||
</div>
|
||
<div className="field">
|
||
<label>End date</label>
|
||
<input className="input" type="date" value={endDate} onChange={(e) => setEndDate(e.target.value)} />
|
||
</div>
|
||
</div>
|
||
<div className="field">
|
||
<label>Location</label>
|
||
<input className="input" value={location} onChange={(e) => setLocation(e.target.value)} placeholder="—" />
|
||
</div>
|
||
<div className="field">
|
||
<label>Sessionize endpoint</label>
|
||
<input
|
||
className="input"
|
||
value={endpoint}
|
||
onChange={(e) => setEndpoint(e.target.value)}
|
||
placeholder="https://sessionize.com/api/v2/<id>/view/All"
|
||
/>
|
||
<div className="muted small" style={{ marginTop: 4 }}>
|
||
The Sessionize API URL (e.g. <code>…/api/v2/<id>/view/All</code>). Used by the
|
||
“Run sync” button. You can also import the .xlsx export in the Interfaces menu.
|
||
</div>
|
||
</div>
|
||
<div className="grid-2">
|
||
<div className="field">
|
||
<label>Pretix organizer</label>
|
||
<input
|
||
className="input"
|
||
value={pretixOrganizer}
|
||
onChange={(e) => setPretixOrganizer(e.target.value)}
|
||
placeholder="e.g. appecommday"
|
||
/>
|
||
</div>
|
||
<div className="field">
|
||
<label>Pretix event slug</label>
|
||
<input
|
||
className="input"
|
||
value={pretixEventSlug}
|
||
onChange={(e) => setPretixEventSlug(e.target.value)}
|
||
placeholder="e.g. tickets"
|
||
/>
|
||
</div>
|
||
</div>
|
||
<div className="muted small" style={{ marginBottom: 12, maxWidth: 640 }}>
|
||
The sidecar syncs paid Pretix tickets for this event every minute. Set the organizer +
|
||
event slug (e.g. <code>appecommday</code> / <code>tickets</code>). The API key is
|
||
configured on the sidecar.
|
||
</div>
|
||
<div className="field">
|
||
<div className="row between" style={{ alignItems: 'center' }}>
|
||
<div>
|
||
<label>Real-time clock</label>
|
||
<div className="muted small" style={{ marginTop: 2 }}>
|
||
{realtimeClock
|
||
? 'The app uses the real system clock for Live/Next/Done.'
|
||
: 'The app uses a simulated mid-event clock (demo always looks live).'}
|
||
</div>
|
||
</div>
|
||
<Toggle checked={realtimeClock} onChange={setRealtimeClock} label={realtimeClock ? 'On' : 'Off'} />
|
||
</div>
|
||
</div>
|
||
</Modal>
|
||
);
|
||
}
|
||
|
||
function SyncResultModal({ result, onClose }: { result: SyncResult; onClose: () => void }) {
|
||
const s = result.summary;
|
||
return (
|
||
<Modal
|
||
open
|
||
title={`Sessionize sync — ${result.event_name ?? result.event_id}`}
|
||
onClose={onClose}
|
||
footer={
|
||
<button className="btn primary" onClick={onClose}>
|
||
Close
|
||
</button>
|
||
}
|
||
>
|
||
{result.status === 'error' || !s ? (
|
||
<div className="pill red" style={{ display: 'inline-block', padding: '8px 12px' }}>
|
||
{result.error ?? 'Sync failed'}
|
||
</div>
|
||
) : (
|
||
<>
|
||
<div className="stat-grid">
|
||
<div className="stat">
|
||
<div className="val">{s.speakers_created}</div>
|
||
<div className="lbl">Speakers created</div>
|
||
</div>
|
||
<div className="stat">
|
||
<div className="val">{s.speakers_updated}</div>
|
||
<div className="lbl">Speakers updated</div>
|
||
</div>
|
||
<div className="stat">
|
||
<div className="val">{s.sessions_created}</div>
|
||
<div className="lbl">Sessions created</div>
|
||
</div>
|
||
<div className="stat">
|
||
<div className="val">{s.sessions_updated}</div>
|
||
<div className="lbl">Sessions updated</div>
|
||
</div>
|
||
<div className="stat">
|
||
<div className="val">{s.slots_created}</div>
|
||
<div className="lbl">Slots created</div>
|
||
</div>
|
||
<div className="stat">
|
||
<div className="val">{s.speakers_linked}</div>
|
||
<div className="lbl">Speaker links</div>
|
||
</div>
|
||
</div>
|
||
{result.skipped_speakers && result.skipped_speakers.length > 0 && (
|
||
<div className="muted small">
|
||
Skipped {result.skipped_speakers.length} speaker(s) without an email: {result.skipped_speakers.join(', ')}
|
||
</div>
|
||
)}
|
||
{s.errors.length > 0 && (
|
||
<div style={{ marginTop: 12 }}>
|
||
<div className="pill red" style={{ display: 'inline-block', marginBottom: 8 }}>
|
||
{s.errors.length} error(s)
|
||
</div>
|
||
<ul style={{ margin: 0, paddingLeft: 18 }}>
|
||
{s.errors.map((e, i) => (
|
||
<li key={i} className="small">
|
||
{e}
|
||
</li>
|
||
))}
|
||
</ul>
|
||
</div>
|
||
)}
|
||
</>
|
||
)}
|
||
</Modal>
|
||
);
|
||
}
|
||
|
||
function fmtDate(d: string): string {
|
||
return new Date(d + 'T00:00:00').toLocaleDateString([], { month: 'short', day: 'numeric', year: 'numeric' });
|
||
}
|