import { apiDelete, apiGet, apiPost, apiPut } from "@/app/lib/clientApi"; // add-event-ticket-orders: an event's ticket settings, ticket types and orders (BO_EVENT_TICKETS). export interface TicketSettings { ticketsEnabled: boolean; paymentInstructions: string | null; holdDays: number | null; } export interface TicketTypeInput { name: string; priceSen: number; quantity: number; salesEndDate: string | null; sort: number; enabled: boolean; } export interface TicketType extends TicketTypeInput { id: number; held: number; left: number; } export type OrderStatus = "AWAITING" | "PAID" | "CANCELLED" | "EXPIRED"; export const ORDER_STATUSES: OrderStatus[] = ["AWAITING", "PAID", "CANCELLED", "EXPIRED"]; export interface OrderRow { id: number; reference: string; buyerName: string; buyerEmail: string; tickets: number; totalSen: number; status: OrderStatus; createdAt: string; } export interface OrderPage { content: OrderRow[]; totalElements: number; totalPages: number; number: number; size: number; } export interface OrderDetail { order: { id: number; reference: string; status: OrderStatus; buyerName: string; buyerEmail: string; buyerMobileE164: string | null; buyerCompany: string | null; totalSen: number; holdUntil: string | null; paidAt: string | null; paidBy: string | null; paymentRef: string | null; cancelledAt: string | null; cancelledBy: string | null; expiredAt: string | null; createdAt: string; }; lines: { id: number; name: string; unitPriceSen: number; quantity: number }[]; tickets: { id: number; typeName: string | null; status: "ACTIVE" | "CANCELLED"; cancelledAt: string | null; cancelledBy: string | null; attendeeName?: string | null; checkedInAt?: string | null }[]; } const base = (uuid: string) => `/api/events/${encodeURIComponent(uuid)}`; export const getTicketSettings = (uuid: string) => apiGet(`${base(uuid)}/tickets/settings`); export const saveTicketSettings = (uuid: string, s: TicketSettings) => apiPut(`${base(uuid)}/tickets/settings`, s); export const listTicketTypes = (uuid: string) => apiGet(`${base(uuid)}/ticket-types`); export const saveTicketType = (uuid: string, t: TicketTypeInput, id?: number) => id == null ? apiPost(`${base(uuid)}/ticket-types`, t) : apiPut(`${base(uuid)}/ticket-types/${id}`, t); export const deleteTicketType = (uuid: string, id: number) => apiDelete(`${base(uuid)}/ticket-types/${id}`); export const listOrders = (uuid: string, f: { status?: string; q?: string; page?: number }) => { const p = new URLSearchParams(); if (f.status) p.set("status", f.status); if (f.q) p.set("q", f.q); p.set("page", String(f.page ?? 0)); return apiGet(`${base(uuid)}/orders?${p}`); }; export const getOrder = (uuid: string, id: string) => apiGet(`${base(uuid)}/orders/${encodeURIComponent(id)}`); export const markOrderPaid = (uuid: string, id: string, paymentRef: string) => apiPost(`${base(uuid)}/orders/${encodeURIComponent(id)}/paid`, { paymentRef }); export const cancelOrder = (uuid: string, id: string) => apiPost(`${base(uuid)}/orders/${encodeURIComponent(id)}/cancel`); export const cancelTicket = (uuid: string, id: string, ticketId: number) => apiPost(`${base(uuid)}/orders/${encodeURIComponent(id)}/tickets/${ticketId}/cancel`); export const resendTicket = (uuid: string, id: string, ticketId: number) => apiPost(`${base(uuid)}/orders/${encodeURIComponent(id)}/tickets/${ticketId}/resend`); export const resendOrderEmail = (uuid: string, id: string) => apiPost(`${base(uuid)}/orders/${encodeURIComponent(id)}/resend`); /** RM 1,000.00 from whole sen; RM 0 reads "Free" where a caller wants it. */ export const money = (sen: number) => `RM ${(sen / 100).toLocaleString("en-MY", { minimumFractionDigits: 2, maximumFractionDigits: 2 })}`; /** The label for a refusal code from the ticket endpoints, else null (the caller shows its generic text). */ export const TICKET_ERRORS: Record = { PAYMENT_INSTRUCTIONS_REQUIRED: "TktErrInstructionsRequired", PAYMENT_INSTRUCTIONS_TOO_LONG: "TktErrInstructionsTooLong", HOLD_DAYS_INVALID: "TktErrHoldDays", NAME_INVALID: "TktErrName", NAME_TAKEN: "TktErrNameTaken", PRICE_INVALID: "TktErrPrice", QUANTITY_INVALID: "TktErrQuantity", QUANTITY_BELOW_HELD: "TktErrQuantityBelowHeld", TICKET_NOT_VALID: "TktErrTicketNotValid", DATE_INVALID: "TktErrDate", TYPE_HAS_ORDERS: "TktErrTypeHasOrders", NOT_PAYABLE: "TktErrNotPayable", SEATS_GONE: "TktErrSeatsGone", PAYMENT_REF_TOO_LONG: "TktErrPaymentRef", NOT_CANCELLABLE: "TktErrNotCancellable", LAST_TICKET: "TktErrLastTicket", };