All files / src/components/AcquirerMerchants/hooks useCreateMerchantActions.ts

52.94% Statements 9/17
40% Branches 4/10
50% Functions 1/2
56.25% Lines 9/16

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                          796x 796x   796x 796x   796x     796x   796x         796x                           796x                  
import NiceModal from "@ebay/nice-modal-react";
import {
  GIVE_CREATE_MERCHANT_PANEL_MODAL,
  ENTERPRISE_NOT_AVAILABLE_MODAL,
} from "modals/modal_names";
import { useLocation } from "react-router-dom";
import { useAccessControl } from "features/Permissions/AccessControl";
import RESOURCE_BASE, { OPERATIONS } from "@constants/permissions";
import { checkPortals } from "@utils/routing";
import { useGetMerchantById } from "@hooks/enterprise-api/account/useGetMerchants";
import { useGetEnterprisesSummary } from "@hooks/acquirer-api/merchants/stats/useMerchantStats";
 
export function useCreateMerchantActions() {
  const { data } = useGetMerchantById();
  const { isEnterprisePortal } = checkPortals();
 
  const { pathname } = useLocation();
  const isAcquirer = pathname.includes("acquirer");
 
  const { stats, isError, isLoading, isFetching } = useGetEnterprisesSummary();
 
  const isProviderDisabled =
    !(data?.statusName === "approved") && isEnterprisePortal;
 
  const isCreateMerchantAllowed = useAccessControl({
    resource: RESOURCE_BASE.MERCHANT,
    operation: OPERATIONS.CREATE,
  });
 
  const handleClickCreateMerchant = () => {
    if (!isAcquirer) {
      NiceModal.show(GIVE_CREATE_MERCHANT_PANEL_MODAL);
      return;
    }
    if (isError) return;
 
    if (stats.countTotal >= 1) {
      NiceModal.show(GIVE_CREATE_MERCHANT_PANEL_MODAL);
    } else {
      NiceModal.show(ENTERPRISE_NOT_AVAILABLE_MODAL);
    }
  };
 
  return {
    handleClickCreateMerchant,
    isLoading,
    isAcquirer,
    isFetching,
    isProviderDisabled,
    isCreateMerchantAllowed: isCreateMerchantAllowed && !isProviderDisabled,
  };
}