All files / src/features/Merchants/MerchantSidePanel/hooks usePahReassignment.ts

100% Statements 15/15
100% Branches 0/0
100% Functions 5/5
100% Lines 15/15

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 59 60 61 62                                                2x 149x   149x 9x             149x 9x   5x           5x       5x 5x 5x 5x     4x 4x       149x    
import { useMutation, useQueryClient } from "react-query";
import { customInstance } from "@services/api";
import { MERCHANT_SIDE_PANEL_PREVIEW_API_KEYS } from "@features/Merchants/MerchantSidePanel/constants";
import {
  QKEY_LIST_TEAM_MEMBERS,
  QKEY_CHECK_OWNER_MEMBER,
  QKEY_MERCHANT_OWNER_REASSIGNMENT,
} from "@constants/queryKeys";
 
export type PahReassignmentInput = { email: string };
 
type ChangePahOptions = {
  onSuccess?: () => void;
  onError?: (error: any) => void;
};
 
/**
 * PAH006 SS-2 — initiate a Primary Account Holder reassignment.
 *
 * POSTs the new PAH email to `/merchants/{merchantId}/owner-reassignments` (a thin
 * wrapper over the invite-create-as-owner path with reassignment intent). AuthZ is
 * enforced server-side; the FE only gates the entry point. On success the merchant
 * preview query is invalidated so the pending-reassignment surface refreshes.
 */
export const usePahReassignment = (merchantId: number) => {
  const queryClient = useQueryClient();
 
  const mutation = useMutation((data: PahReassignmentInput) =>
    customInstance({
      url: `/merchants/${merchantId}/owner-reassignments`,
      method: "POST",
      data,
    }),
  );
 
  const changePah = (data: PahReassignmentInput, opts?: ChangePahOptions) =>
    mutation.mutateAsync(data).then(
      (res) => {
        queryClient.invalidateQueries([
          MERCHANT_SIDE_PANEL_PREVIEW_API_KEYS.GET,
          merchantId,
        ]);
        // Refresh the merchant Team-tab surfaces too (status banner, invited-PAH
        // row, owner row); they read separate queries from the acquirer preview.
        queryClient.invalidateQueries([
          QKEY_MERCHANT_OWNER_REASSIGNMENT,
          merchantId,
        ]);
        queryClient.invalidateQueries(QKEY_LIST_TEAM_MEMBERS);
        queryClient.invalidateQueries([QKEY_CHECK_OWNER_MEMBER, merchantId]);
        opts?.onSuccess?.();
        return res;
      },
      (error) => {
        opts?.onError?.(error);
        throw error;
      },
    );
 
  return { changePah, isLoading: mutation.isLoading };
};