Phase 7+8: Pretix + Sessionize interfaces

- Migration 20260825000000_interfaces.sql: external_id on contacts/
  registrations/sessions (+ partial unique indexes), slots_id_seq, sync_runs
  audit table, and service-role-only sync_pretix_attendees /
  sync_sessionize_sessions RPCs (idempotent upserts).
- sync-trigger Edge Function: admin-JWT or CRON_SECRET auth, pulls from
  Pretix/Sessionize base URLs, normalizes, calls the service-role RPCs.
- interfaces/mock-api: dependency-free Node server (:54330) + fixtures so the
  full pipeline is testable without real tokens.
- APPE Admin 'Interfaces' view: per-source + 'Sync all' triggers, result
  counts, recent sync runs, and QR CSV export; /functions proxy added.
- Docs: new docs/backend/interfaces.md; updated README, roadmap, next-steps,
  schema, rls, rpcs, architecture, admin-app, and docs index.

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]>
This commit is contained in:
committed 2026-08-23 03:34:30 +02:00
1 parent 7605c9bbf4
commit 74c41c8c1d
26 files changed
+1529 -80

No files matched your search

+240
View File
@@ -0,0 +1,240 @@
import { useCallback, useEffect, useState } from 'react';
import {
exportQrCsv,
getActiveEvent,
getSyncRuns,
triggerSync,
type SyncResult,
} from '../lib/api';
import type { Event, SyncRun } from '../lib/types';
import { useToast } from '../context/ToastContext';
import { Empty, Spinner } from '../components/ui';
type Source = 'pretix' | 'sessionize' | 'both';
function fmtTime(iso: string | null): string {
if (!iso) return '—';
const d = new Date(iso);
return d.toLocaleString(undefined, {
month: 'short',
day: 'numeric',
hour: '2-digit',
minute: '2-digit',
second: '2-digit',
});
}
function SourcePill({ source }: { source: SyncRun['source'] }) {
return <span className={`pill ${source === 'pretix' ? 'gold' : 'green'}`}>{source}</span>;
}
export function Sync() {
const { push } = useToast();
const [event, setEvent] = useState<Event | null>(null);
const [loaded, setLoaded] = useState(false);
const [error, setError] = useState<string | null>(null);
const [runs, setRuns] = useState<SyncRun[]>([]);
const [busy, setBusy] = useState<Source | null>(null);
const [last, setLast] = useState<SyncResult | null>(null);
const load = useCallback(async () => {
try {
const [ev, r] = await Promise.all([getActiveEvent(), getSyncRuns(8)]);
setEvent(ev);
setRuns(r);
setLoaded(true);
} catch (e) {
setError(e instanceof Error ? e.message : 'Failed to load');
}
}, []);
useEffect(() => {
load();
}, [load]);
async function doSync(source: Source) {
setBusy(source);
setLast(null);
try {
const res = await triggerSync(source);
setLast(res);
const bits: string[] = [];
if (res.results.pretix)
bits.push(`Pretix: ${res.results.pretix.updated} updated`);
if (res.results.sessionize)
bits.push(
`Sessionize: ${res.results.sessionize.sessions_created} new, ${res.results.sessionize.sessions_updated} updated`,
);
push(res.ok ? 'success' : 'error', res.ok ? `Sync complete — ${bits.join(' · ')}` : `Sync finished with errors`);
await load();
} catch (e) {
push('error', e instanceof Error ? e.message : 'Sync failed');
} finally {
setBusy(null);
}
}
async function doExport() {
if (!event) return;
try {
const n = await exportQrCsv(event.id);
push('success', `Exported ${n} lanyard QR code${n === 1 ? '' : 's'} to CSV`);
} catch (e) {
push('error', e instanceof Error ? e.message : 'Export failed');
}
}
if (error) return <Empty icon="⚠️" text={error} />;
if (!loaded)
return (
<div className="loading-screen">
<Spinner />
</div>
);
return (
<div>
<div className="page-head">
<div>
<h1>Interfaces</h1>
<div className="sub">
Sync real data from <strong>Pretix</strong> (tickets → contacts + registrations) and{' '}
<strong>Sessionize</strong> (agenda → slots + sessions + speakers).
</div>
</div>
<div className="row" style={{ gap: 8 }}>
<button className="btn" disabled={busy !== null} onClick={() => doExport()}>
⬇ Export QR CSV
</button>
<button className="btn primary" disabled={busy !== null} onClick={() => doSync('both')}>
{busy === 'both' ? 'Syncing…' : '🔄 Sync all'}
</button>
</div>
</div>
{!event && (
<div className="card" style={{ marginBottom: 16 }}>
<Empty icon="📅" text="No active event. Syncs target the active event — set one in the Events view." />
</div>
)}
<div className="grid-2">
<div className="card">
<div className="row between" style={{ marginBottom: 14 }}>
<h2 className="h2">🎟️ Pretix</h2>
<button
className="btn small"
disabled={busy !== null || !event}
onClick={() => doSync('pretix')}
>
{busy === 'pretix' ? 'Syncing…' : 'Sync now'}
</button>
</div>
<p className="muted small" style={{ marginTop: 0 }}>
Pulls ticket holders and upserts <code>contacts</code> + <code>registrations</code> (QR tokens
generated for new ticket holders). Ticket type maps to registration type
(speaker / staff / sponsor / attendee).
</p>
{last?.results.pretix && (
<div className="sync-result">
<span className="pill green">{last.results.pretix.updated} updated</span>
<span className="pill">{last.results.pretix.skipped} skipped</span>
{last.results.pretix.errors.length > 0 && (
<span className="pill red">{last.results.pretix.errors.length} errors</span>
)}
</div>
)}
</div>
<div className="card">
<div className="row between" style={{ marginBottom: 14 }}>
<h2 className="h2">🗓️ Sessionize</h2>
<button
className="btn small"
disabled={busy !== null || !event}
onClick={() => doSync('sessionize')}
>
{busy === 'sessionize' ? 'Syncing…' : 'Sync now'}
</button>
</div>
<p className="muted small" style={{ marginTop: 0 }}>
Pulls the agenda and upserts <code>slots</code> + <code>sessions</code> + speakers. Slots are
matched by date + time; sessions by their Sessionize id.
</p>
{last?.results.sessionize && (
<div className="sync-result">
<span className="pill green">{last.results.sessionize.sessions_created} new</span>
<span className="pill">{last.results.sessionize.sessions_updated} updated</span>
<span className="pill">{last.results.sessionize.slots_created} slots</span>
<span className="pill">{last.results.sessionize.speakers_linked} speakers</span>
</div>
)}
</div>
</div>
{last && last.errors.length > 0 && (
<div className="card" style={{ marginTop: 16, borderColor: 'var(--red, #d50000)' }}>
<h2 className="h2" style={{ color: 'var(--red, #d50000)' }}>
⚠️ Sync errors
</h2>
<ul className="muted small" style={{ margin: '8px 0 0', paddingLeft: 18 }}>
{last.errors.map((e, i) => (
<li key={i}>{e}</li>
))}
</ul>
</div>
)}
<div className="card" style={{ marginTop: 16 }}>
<div className="row between" style={{ marginBottom: 14 }}>
<h2 className="h2">🕘 Recent sync runs</h2>
<button className="btn small ghost" onClick={load}>
Refresh
</button>
</div>
{runs.length === 0 ? (
<Empty icon="🔄" text="No sync runs yet. Trigger one above." />
) : (
<div className="table-wrap">
<table className="data">
<thead>
<tr>
<th>Source</th>
<th>Started</th>
<th>Created</th>
<th>Updated</th>
<th>Skipped</th>
<th>Triggered by</th>
<th>Status</th>
</tr>
</thead>
<tbody>
{runs.map((r) => (
<tr key={r.id}>
<td>
<SourcePill source={r.source} />
</td>
<td className="muted small">{fmtTime(r.started_at)}</td>
<td>{r.created}</td>
<td>{r.updated}</td>
<td>{r.skipped}</td>
<td className="muted small">{r.triggered_by}</td>
<td>
{r.error ? (
<span className="pill red" title={r.error}>
error
</span>
) : (
<span className="pill green">ok</span>
)}
</td>
</tr>
))}
</tbody>
</table>
</div>
)}
</div>
</div>
);
}