All files / src/hooks/enterprise-api/account useGetMerchants.tsx

92.3% Statements 24/26
88.23% Branches 30/34
80% Functions 4/5
92% Lines 23/25

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 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126                          477x 97x           477x           12092x 12092x         12091x       12091x 12091x     12091x     12091x   12091x     33x 33x                                                     12091x   12091x       12091x 1921x 77x                                 12091x   12091x 12091x             12091x       12091x                    
import { UseQueryOptions, useQuery } from "react-query";
import { customInstance } from "@services/api";
import { useGetCurrentMerchantId } from "@hooks/common";
import { updatePermissions } from "@redux/slices/app";
import { useAppDispatch } from "@redux/hooks";
import { isDefined } from "@utils/helpers";
import { useMemo } from "react";
import { QKEY_GET_MERCHANT_BY_ID } from "@constants/queryKeys";
import { STATUS_ENUM } from "@components/ProfilePage/ProfileSetupHome/HomepageCard";
import { useGetAcquirerMsaVersion } from "@services/api/merchants";
import RESOURCE_BASE, { OPERATIONS } from "@constants/permissions";
import { useAccessControl } from "@features/Permissions/AccessControl";
 
export const getMerchantById = (id: number | string) => {
  return customInstance({
    url: `/merchants/${id}`,
    method: "GET",
  });
};
 
export const useGetMerchantById = (
  custom?: Omit<UseQueryOptions<any, any, any, any>, "queryKey" | "queryFn"> & {
    merchantId?: number | string;
  },
  isEnterprise?: boolean,
) => {
  const { merchantId, selectedUser } = useGetCurrentMerchantId();
  const isViewAcquirerAlowwed = useAccessControl({
    resource: RESOURCE_BASE.ACQUIRER,
    operation: OPERATIONS.READ,
  });
  const acquirerPermission =
    selectedUser?.merchType === "acquirer"
      ? isViewAcquirerAlowwed
      : Boolean(selectedUser?.merchType);
 
  const dispatch = useAppDispatch();
  const merchantIdCustom = custom?.merchantId || merchantId;
 
  const enabled =
    custom?.enabled !== undefined ? custom?.enabled === true : true;
 
  const isCustomEnabledSet =
    custom?.enabled !== undefined ? custom.enabled : true; //if we need different logic for enabling we need to rely on it as well
 
  const { data, ...rest } = useQuery(
    [QKEY_GET_MERCHANT_BY_ID, merchantIdCustom],
    async () => {
      const merchantPromise = getMerchantById(merchantIdCustom);
      return merchantPromise;
    },
    {
      refetchOnMount: false,
      refetchOnWindowFocus: false,
      staleTime: 10000,
      retry: 1,
      onError(err: any) {
        if (err.not_authorized) {
          dispatch(
            updatePermissions({
              get_merchant_by_id: true,
            }),
          );
        }
      },
      ...custom,
      enabled:
        isDefined(merchantIdCustom) &&
        merchantIdCustom !== 0 &&
        isCustomEnabledSet &&
        acquirerPermission,
    },
  );
 
  // the msa-version api call is for any merchant id,
  // and on BE part, they get the merchant/provider parent / grand parent acquirer id
  const enableAcquirerCall = enabled && isDefined(merchantIdCustom);
 
  const { data: acquirerData } = useGetAcquirerMsaVersion(merchantIdCustom, {
    enabled: enableAcquirerCall,
  });
 
  const memoizedData: any & { acquirerTcVersion: string } = useMemo(() => {
    if (!data) return data;
    return {
      ...data,
      msaVersion:
        isEnterprise || data?.type === "enterprise"
          ? acquirerData?.enterpriseMSAVersion
          : acquirerData?.submerchantMSAVersion,
      acquirerTcDate: acquirerData?.updatedAt,
      globalVersion: data?.msaVersion,
      aquirerTcVersion:
        isEnterprise || data?.type === "enterprise"
          ? acquirerData?.enterpriseMSAVersion
          : acquirerData?.submerchantMSAVersion,
      enterpriseMSAVersion: acquirerData?.enterpriseMSAVersion,
      submerchantMSAVersion: acquirerData?.submerchantMSAVersion,
      isPayfac: data?.isPayfac ?? false,
    };
  }, [data, acquirerData]);
  const isApprovedPAH = memoizedData?.owner?.statusName === "approved";
  const pahUnderVerification =
    memoizedData?.owner?.statusName === "ready_for_verification";
  const pahStatus = isApprovedPAH
    ? STATUS_ENUM.APPROVED
    : pahUnderVerification
    ? STATUS_ENUM.UNDER_VERIFICATION
    : STATUS_ENUM.INCOMPLETE;
 
  const isAllowedAddBankAccounts =
    memoizedData?.type === "enterprise"
      ? true
      : memoizedData?.allowAddingBankAccounts;
 
  return {
    data: memoizedData,
    acquirerData,
    pahStatus,
    isApprovedPAH,
    isAllowedAddBankAccounts,
    merchantId,
    ...rest,
  };
};