Archived
WIP 1.51.14: people and messaging on v2
Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com> Claude-Session: https://claude.ai/code/session_01Ls1vMdg2GYqbnKZmPaX1ot
This commit is contained in:
@@ -106,6 +106,6 @@ export function trackFunnel(eventId: string, stage: "view" | "start") {
|
||||
if (!sessionId || !eventId) return;
|
||||
const body = JSON.stringify({ sessionId, eventId, stage, source: sourceFromUrl() });
|
||||
try {
|
||||
fetch(`${API_BASE}/api/analytics/track`, { method: "POST", headers: { "Content-Type": "application/json" }, body, keepalive: true }).catch(() => {});
|
||||
fetch(`${API_BASE}/api/v2/analytics/track`, { method: "POST", headers: { "Content-Type": "application/json" }, body, keepalive: true }).catch(() => {});
|
||||
} catch { /* ignore */ }
|
||||
}
|
||||
|
||||
File diff suppressed because it is too large
Load Diff
Vendored
+4272
File diff suppressed because it is too large
Load Diff
+12
-31
@@ -162,41 +162,15 @@ export async function apiFetch<T = unknown>(path: string, options: ApiOptions =
|
||||
// instead of caching an empty one.
|
||||
export async function apiFetchListForPrerender<T>(path: string, options: ApiOptions = {}): Promise<T[]> {
|
||||
try {
|
||||
return (await apiFetch<T[]>(path, options)) || [];
|
||||
// v2 answers { data: [...] }; v1 answered the bare list.
|
||||
const res = await apiFetch<T[] | { data?: T[] }>(path, options);
|
||||
return (Array.isArray(res) ? res : res?.data) || [];
|
||||
} catch (err) {
|
||||
if (process.env.NEXT_PHASE === "phase-production-build") return [];
|
||||
throw err;
|
||||
}
|
||||
}
|
||||
|
||||
// Several list endpoints (/api/users, /api/payments) are paginated and cap
|
||||
// `limit` at 200 server-side, so a single request can silently miss records
|
||||
// beyond the first page. This walks every page and returns the full list.
|
||||
async function fetchAllPages<T>(path: string, token: string, params: Record<string, string> = {}): Promise<T[]> {
|
||||
const query = new URLSearchParams({ ...params, limit: "200", page: "1" });
|
||||
const first = await apiFetch<Paginated<T>>(`${path}?${query.toString()}`, { authToken: token });
|
||||
const items: T[] = Array.isArray(first?.data) ? first.data : [];
|
||||
const pages = first?.pages || 1;
|
||||
|
||||
if (pages > 1) {
|
||||
// Page 1 told us the total page count — fetch the rest concurrently instead of
|
||||
// awaiting them one at a time. This function is called on every dashboard poll
|
||||
// (see useVisiblePolling usages), so a serial loop here directly slows those polls.
|
||||
const remaining = await Promise.all(
|
||||
Array.from({ length: pages - 1 }, (_, i) => {
|
||||
const page = i + 2;
|
||||
const pageQuery = new URLSearchParams({ ...params, limit: "200", page: String(page) });
|
||||
return apiFetch<Paginated<T>>(`${path}?${pageQuery.toString()}`, { authToken: token });
|
||||
})
|
||||
);
|
||||
for (const res of remaining) {
|
||||
if (Array.isArray(res?.data)) items.push(...res.data);
|
||||
}
|
||||
}
|
||||
|
||||
return items;
|
||||
}
|
||||
|
||||
/**
|
||||
* Every event staff can see (API v2 GET /events/manage): upcoming by default; `past`,
|
||||
* `inactive` widen it, `closed: false` leaves out cashed-up events. Full records — ticket options,
|
||||
@@ -244,8 +218,15 @@ export async function fetchEvent<T = Event>(id: string, options: { authToken?: s
|
||||
return data as unknown as T;
|
||||
}
|
||||
|
||||
export function fetchAllUsers(token: string, params: Record<string, string> = {}): Promise<User[]> {
|
||||
return fetchAllPages<User>("/api/users", token, params);
|
||||
/** Every account (API v2 GET /users, all pages at once). */
|
||||
export async function fetchAllUsers(token: string, params: Record<string, string> = {}): Promise<User[]> {
|
||||
const { apiV2Data } = await import("@/lib/apiV2");
|
||||
const page = (n: number) => apiV2Data("get", "/users", { query: { ...params, limit: "200", page: String(n) }, authToken: token }) as Promise<Paginated<User>>;
|
||||
const first = await page(1);
|
||||
const items: User[] = Array.isArray(first?.data) ? [...first.data] : [];
|
||||
const rest = await Promise.all(Array.from({ length: Math.max(0, (first?.pages || 1) - 1) }, (_, i) => page(i + 2)));
|
||||
for (const res of rest) if (Array.isArray(res?.data)) items.push(...res.data);
|
||||
return items;
|
||||
}
|
||||
|
||||
/** Every payment staff may see (API v2 GET /payments, all pages at once). */
|
||||
|
||||
+17
-12
@@ -53,8 +53,13 @@ export type ReportPdfPayload = {
|
||||
extraTables?: { title: string; columns: string[]; rows: (string | number)[][] }[];
|
||||
};
|
||||
|
||||
/** The message in a v2 error body (or the text as it came). */
|
||||
function v2ErrorText(text: string): string {
|
||||
try { const j = JSON.parse(text); return j?.error?.message || j?.message || text; } catch { return text; }
|
||||
}
|
||||
|
||||
export async function downloadReportPdf(apiBase: string, authToken: string, payload: ReportPdfPayload) {
|
||||
const url = `${apiBase}/api/reports/pdf`;
|
||||
const url = `${apiBase}/api/v2/reports/pdf`;
|
||||
const res = await fetch(url, {
|
||||
method: 'POST',
|
||||
headers: {
|
||||
@@ -65,7 +70,7 @@ export async function downloadReportPdf(apiBase: string, authToken: string, payl
|
||||
});
|
||||
if (!res.ok) {
|
||||
const text = await res.text().catch(() => '');
|
||||
throw new Error(text || `Failed to generate PDF (${res.status})`);
|
||||
throw new Error(v2ErrorText(text) || `Failed to generate PDF (${res.status})`);
|
||||
}
|
||||
const blob = await res.blob();
|
||||
const dl = document.createElement('a');
|
||||
@@ -80,7 +85,7 @@ export async function downloadReportPdf(apiBase: string, authToken: string, payl
|
||||
// Downloads a styled .xlsx mirroring the same branded look as the PDF (colored header,
|
||||
// stat rows, a data-bar chart, banded table with a highlighted total row).
|
||||
export async function downloadReportExcel(apiBase: string, authToken: string, payload: ReportPdfPayload) {
|
||||
const url = `${apiBase}/api/reports/excel`;
|
||||
const url = `${apiBase}/api/v2/reports/excel`;
|
||||
const res = await fetch(url, {
|
||||
method: 'POST',
|
||||
headers: {
|
||||
@@ -91,7 +96,7 @@ export async function downloadReportExcel(apiBase: string, authToken: string, pa
|
||||
});
|
||||
if (!res.ok) {
|
||||
const text = await res.text().catch(() => '');
|
||||
throw new Error(text || `Failed to generate Excel file (${res.status})`);
|
||||
throw new Error(v2ErrorText(text) || `Failed to generate Excel file (${res.status})`);
|
||||
}
|
||||
const blob = await res.blob();
|
||||
const dl = document.createElement('a');
|
||||
@@ -104,7 +109,7 @@ export async function downloadReportExcel(apiBase: string, authToken: string, pa
|
||||
}
|
||||
|
||||
export async function emailReportPdf(apiBase: string, authToken: string, payload: ReportPdfPayload & { subject?: string; body?: string }) {
|
||||
const url = `${apiBase}/api/reports/email`;
|
||||
const url = `${apiBase}/api/v2/reports/email`;
|
||||
const res = await fetch(url, {
|
||||
method: 'POST',
|
||||
headers: {
|
||||
@@ -115,15 +120,15 @@ export async function emailReportPdf(apiBase: string, authToken: string, payload
|
||||
});
|
||||
if (!res.ok) {
|
||||
const text = await res.text().catch(() => '');
|
||||
throw new Error(text || `Failed to email PDF (${res.status})`);
|
||||
throw new Error(v2ErrorText(text) || `Failed to email PDF (${res.status})`);
|
||||
}
|
||||
return res.json();
|
||||
return (await res.json()).data;
|
||||
}
|
||||
|
||||
// Sends the report PDF to the current user's own WhatsApp number (same self-service pattern as
|
||||
// emailReportPdf — no recipient picker needed).
|
||||
export async function whatsappReportPdf(apiBase: string, authToken: string, payload: ReportPdfPayload & { caption?: string }) {
|
||||
const url = `${apiBase}/api/reports/whatsapp`;
|
||||
const url = `${apiBase}/api/v2/reports/whatsapp`;
|
||||
const res = await fetch(url, {
|
||||
method: 'POST',
|
||||
headers: {
|
||||
@@ -134,15 +139,15 @@ export async function whatsappReportPdf(apiBase: string, authToken: string, payl
|
||||
});
|
||||
if (!res.ok) {
|
||||
const text = await res.text().catch(() => '');
|
||||
throw new Error(text || `Failed to send PDF via WhatsApp (${res.status})`);
|
||||
throw new Error(v2ErrorText(text) || `Failed to send PDF via WhatsApp (${res.status})`);
|
||||
}
|
||||
return res.json();
|
||||
return (await res.json()).data;
|
||||
}
|
||||
|
||||
// "Print": fetches the same PDF as downloadReportPdf but opens it in a new tab instead of
|
||||
// downloading, so the browser's built-in PDF viewer's print button handles printing.
|
||||
export async function viewReportPdf(apiBase: string, authToken: string, payload: ReportPdfPayload) {
|
||||
const url = `${apiBase}/api/reports/pdf`;
|
||||
const url = `${apiBase}/api/v2/reports/pdf`;
|
||||
const res = await fetch(url, {
|
||||
method: 'POST',
|
||||
headers: {
|
||||
@@ -153,7 +158,7 @@ export async function viewReportPdf(apiBase: string, authToken: string, payload:
|
||||
});
|
||||
if (!res.ok) {
|
||||
const text = await res.text().catch(() => '');
|
||||
throw new Error(text || `Failed to generate PDF (${res.status})`);
|
||||
throw new Error(v2ErrorText(text) || `Failed to generate PDF (${res.status})`);
|
||||
}
|
||||
const blob = await res.blob();
|
||||
const objUrl = URL.createObjectURL(blob);
|
||||
|
||||
Reference in New Issue
Block a user