All files / src/hooks/acquirer-api/merchants/stats useGetMerchantCounters.ts

100% Statements 27/27
86.95% Branches 20/23
100% Functions 6/6
100% Lines 26/26

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 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104                                                        498x         498x 199x 199x           498x     5590x               5590x 5590x 5590x   5590x       5590x 5590x     199x 90x                           498x 10366x 10366x   10366x   10366x 4x             10366x     498x   15956x 15956x 15740x 3808x    
import { UseQueryOptions, useQuery, useQueryClient } from "react-query";
import { QKEY_LIST_MERCHANT_COUNTERS } from "@constants/queryKeys";
import { DEFAULT_QUERY_CONFIG } from "@components/VirtualList/hooks/queries";
import { customInstance } from "@services/api";
import { checkPortals } from "@utils/routing";
import { useGetCurrentMerchantId } from "@hooks/common";
 
export type MerchantCounterType = {
  accID: number;
  allMerchantsCount: number;
  underwritingCount: number;
  onboardingCount?: number;
  preApprovalCount?: number;
  autoApprovalCount?: number;
  sponsorCount?: number;
  sponsorReadyForReviewCount?: number;
  complianceCount?: number;
  invitedCount?: number;
  approvedCount?: number;
  kybCount?: number;
  signupCount?: number;
};
 
type QueryReturnedType = MerchantCounterType;
export type PortalType =
  | "acquirer_merchants_list"
  | "acquirer_enterprises_list";
 
export const PortalType = {
  enterprise: "acquirer_enterprises_list",
  merchant: "acquirer_merchants_list",
};
 
export const getMerchantCounters = (id: string | number, type?: PortalType) => {
  const filterString = type ? `?portal_type=${type}` : "";
  return customInstance({
    url: `/merchants/${id}/merchant-counters${filterString}`,
    method: "GET",
  });
};
 
export const useGetMerchantCounters = (
  options?: Omit<UseQueryOptions<any, any, any, any>, "queryKey" | "queryFn">,
) => {
  const { isMerchantPortal } = checkPortals();
  //the root cause for this is that in some place, for some reason, we are refetching multiple endpoints/hooks on account change including some that reach to a point that this endpoint is called
  const {
    isMerchantRole,
    isAcquirerLoggedIn,
    isControlMode,
    isProviderLoggedIn,
    merchantId,
  } = useGetCurrentMerchantId();
  const counterType = getCounterType();
  const isMissingType = isAcquirerLoggedIn && !isControlMode && !counterType; //to avoid failing requests when leaving control mode of provider
  const isNotMerchant =
    !isMerchantPortal &&
    !isMerchantRole &&
    (isAcquirerLoggedIn || isProviderLoggedIn); //to make sure we do not make any unnecessary fetch on merchant side, the endpoint is not available there
  const enabledCheckByID =
    !!merchantId && merchantId !== 0 && isNotMerchant && !isMissingType;
  return useQuery<QueryReturnedType>(
    [QKEY_LIST_MERCHANT_COUNTERS, counterType, merchantId],
    async () => {
      const merchantStats = await getMerchantCounters(merchantId, counterType);
      return merchantStats;
    },
    {
      ...DEFAULT_QUERY_CONFIG,
      ...options,
      retry: 2,
      enabled:
        options?.enabled !== undefined
          ? options?.enabled && enabledCheckByID
          : enabledCheckByID,
    },
  );
};
 
export const useRefetchCounters = () => {
  const queryClient = useQueryClient();
  const { merchantId } = useGetCurrentMerchantId();
 
  const counterType = getCounterType();
 
  const handleRefetchCounters = () => {
    queryClient.invalidateQueries([
      QKEY_LIST_MERCHANT_COUNTERS,
      counterType,
      merchantId,
    ]);
  };
 
  return { handleRefetchCounters };
};
 
const getCounterType = () => {
  const { isAcquirerEnterprises, isMerchantsTable, isAcquirerPortal } =
    checkPortals();
  if (isAcquirerEnterprises) return PortalType.enterprise as PortalType;
  if (isMerchantsTable && isAcquirerPortal)
    return PortalType.merchant as PortalType;
};