All files / src/sections/PayBuilder/Checkout/hooks useSubmitPurchaseVerification.ts

100% Statements 9/9
100% Branches 2/2
100% Functions 2/2
100% Lines 8/8

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      13x                       13x           3x         2x 1x     13x 384x 384x    
import { useMutation } from "react-query";
import { customInstance } from "@services/api";
 
export const PURCHASE_VERIFICATION_URL = "/purchase-verifications";
 
type TSubmitPurchaseVerification = {
  orderID: number;
  code: string;
};
 
/**
 * Confirms the order rather than the request that was held: the buyer may type
 * the code in a different tab from the one holding the checkout. 204 on
 * success, 422 once the code is wrong, expired, or its attempts are spent.
 */
export const submitPurchaseVerification = async (
  data: TSubmitPurchaseVerification,
) => {
  // customInstance resolves a canceled or aborted request as null rather than
  // rejecting. Left alone, an unsent submission reads as a confirmed purchase
  // and the checkout is re-posted against a code the API never saw.
  const result = await customInstance({
    url: PURCHASE_VERIFICATION_URL,
    method: "POST",
    data,
  });
  if (result === null) throw new Error("purchase verification not submitted");
  return result;
};
 
export const useSubmitPurchaseVerification = () => {
  const { mutateAsync } = useMutation(submitPurchaseVerification);
  return { submitCode: mutateAsync };
};