All files / src/features/Events/EventDetail/tickets useTicketTransaction.ts

0% Statements 0/5
0% Branches 0/5
0% Functions 0/1
0% Lines 0/5

Press n or j to go to the next uncovered block, b, p or k for the previous block.

1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58                                                                                                                   
import { QKEY_EVENT_TICKET_TRANSACTION } from "@constants/queryKeys";
import { useGetTransactionsByProduct } from "@services/api/products/transactions";
import { useParseTransactionRow } from "@pages/FundraisersDonationList/utils";
 
/**
 * The purchase behind one ticket, in the shape `TransactionPanelContent` expects
 * (frame 7420-215195 — the second panel the ticket panel's "Transaction detail"
 * link opens).
 *
 * Read from the same `products/{id}/transaction-items` list the Transactions tab
 * uses, narrowed to this transaction and parsed with the same row parser — so the
 * second panel gets exactly what it would have got had the merchant opened the row
 * from that tab. The ticket carries the transaction's OBJECT id (`GS_TXN_…`), not
 * the numeric one — the item view's `transactionID` filter field is an int64, so
 * the narrowing must go through its string counterpart, `TransactionObjID`
 * (TitleCase on the wire, matching the view's own json tag).
 *
 * A transaction can cover several items (one order, several tiers); the first is
 * the row that tab would have shown, and the panel's own Purchase Order section
 * lists them all regardless.
 */
export const useTicketTransaction = ({
  eventId,
  transactionId,
  enabled,
}: {
  eventId?: string;
  transactionId?: string;
  enabled?: boolean;
}) => {
  const { rowParser } = useParseTransactionRow();
 
  const { data, isLoading, error } = useGetTransactionsByProduct(
    eventId as string,
    `${QKEY_EVENT_TICKET_TRANSACTION}-${transactionId}`,
  )(
    {
      page: 1,
      maxRowsPerPage: 1,
      // The factory appends `&filter=<queryString>` verbatim. The value must be
      // quoted — the filter grammar reads bare tokens as numbers/booleans.
      queryString: encodeURIComponent(`(TransactionObjID:"${transactionId}")`),
    },
    {
      enabled: Boolean(enabled && eventId && transactionId),
      refetchOnWindowFocus: false,
    },
  );
 
  const item = data?.data?.[0];
 
  return {
    transaction: item ? rowParser(item) : undefined,
    isLoading,
    isError: Boolean(error),
  };
};