Files
crosscode-platform/apps/events/frontend/src/lib/eventSetup.ts
T

98 lines
5.4 KiB
TypeScript

import { apiV2, type V2Body } from "@/lib/apiV2";
import type { EventAttachment, EventOption } from "@/types";
/**
* Event set-up calls on API v2 (1.51.8): ticket options and variants, scanning sections,
* attachments, planned costs and image uploads. Each returns what the screens already work with.
*/
type Token = string | null | undefined;
// ─── Ticket options ──────────────────────────────────────────────────────────
export async function createTicketOption(token: Token, eventId: string, body: V2Body<"/events/{id}/ticket-options", "post">): Promise<EventOption> {
const { data } = await apiV2("post", "/events/{id}/ticket-options", { params: { id: eventId }, authToken: token, body });
return data as unknown as EventOption;
}
/** Only what's sent changes; `earlyBirdTiers` and `variants`, when sent, are the full lists. */
export async function updateTicketOption(token: Token, optionId: string, body: V2Body<"/ticket-options/{id}", "patch">): Promise<EventOption> {
const { data } = await apiV2("patch", "/ticket-options/{id}", { params: { id: optionId }, authToken: token, body });
return data as unknown as EventOption;
}
export async function deleteTicketOption(token: Token, optionId: string): Promise<void> {
await apiV2("delete", "/ticket-options/{id}", { params: { id: optionId }, authToken: token });
}
// ─── Scanning sections ───────────────────────────────────────────────────────
export type SectionRecord = { id: string; eventId: string; name: string; allowedOptions?: { eventOptionId: string; eventOption?: { id: string; name: string } }[] };
export async function listSections<T = SectionRecord>(token: Token, eventId?: string): Promise<T[]> {
const { data } = await apiV2("get", "/sections", { authToken: token, query: eventId ? { eventId } : {} });
return data as unknown as T[];
}
export async function createSection(token: Token, body: V2Body<"/sections", "post">): Promise<SectionRecord> {
const { data } = await apiV2("post", "/sections", { authToken: token, body });
return data as unknown as SectionRecord;
}
export async function updateSection(token: Token, sectionId: string, body: V2Body<"/sections/{id}", "patch">): Promise<SectionRecord> {
const { data } = await apiV2("patch", "/sections/{id}", { params: { id: sectionId }, authToken: token, body });
return data as unknown as SectionRecord;
}
export async function deleteSection(token: Token, sectionId: string): Promise<void> {
await apiV2("delete", "/sections/{id}", { params: { id: sectionId }, authToken: token });
}
// ─── Attachments ─────────────────────────────────────────────────────────────
export async function listAttachments(eventId: string): Promise<EventAttachment[]> {
const { data } = await apiV2("get", "/events/{id}/attachments", { params: { id: eventId } });
return data as unknown as EventAttachment[];
}
/** Uploads a file (PDF, Office, text, CSV or ZIP, up to 20 MB) to an event. */
export async function uploadAttachment(token: Token, eventId: string, file: File): Promise<EventAttachment> {
const fd = new FormData();
fd.append("file", file);
const { apiFetch } = await import("@/lib/api");
const { data } = await apiFetch<{ data: EventAttachment }>(`/api/v2/events/${encodeURIComponent(eventId)}/attachments`, { method: "POST", body: fd, authToken: token });
return data;
}
export async function deleteAttachment(token: Token, eventId: string, attachmentId: string): Promise<void> {
await apiV2("delete", "/events/{id}/attachments/{attachmentId}", { params: { id: eventId, attachmentId }, authToken: token });
}
// ─── Planned costs ───────────────────────────────────────────────────────────
export async function createCost(token: Token, eventId: string, body: V2Body<"/events/{id}/costs", "post">): Promise<void> {
await apiV2("post", "/events/{id}/costs", { params: { id: eventId }, authToken: token, body });
}
export async function updateCost(token: Token, costId: string, body: V2Body<"/costs/{id}", "patch">): Promise<void> {
await apiV2("patch", "/costs/{id}", { params: { id: costId }, authToken: token, body });
}
export async function deleteCost(token: Token, costId: string): Promise<void> {
await apiV2("delete", "/costs/{id}", { params: { id: costId }, authToken: token });
}
// ─── Image uploads ───────────────────────────────────────────────────────────
/**
* Uploads an image (field "image") and returns the path to save (`/uploads/…`). `logo` and
* `favicon` work without a token during first-time setup.
*/
export async function uploadImage(token: Token, kind: "event-image" | "logo" | "favicon", file: File | Blob): Promise<string> {
const fd = new FormData();
fd.append("image", file);
const { apiFetch } = await import("@/lib/api");
const { data } = await apiFetch<{ data: { path: string; url: string } }>(`/api/v2/uploads/${kind}`, { method: "POST", body: fd, authToken: token });
return data.path;
}