All files / src/features/Enterprises/EnterprisesTable/hooks useAllEnterpriseTabColumns.tsx

95% Statements 38/40
95.45% Branches 21/22
87.5% Functions 14/16
95% Lines 38/40

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 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230                                                4x 135x   135x   135x 135x 135x   135x       135x   135x   135x 21x               224x                                                 135x 19x             72x                             135x 19x               72x                   135x 19x               224x 224x   224x                                           135x 21x               72x 72x   72x                               135x 23x                   72x             72x   72x                     135x                   4x       21x 2x   19x    
import { TableColumnType } from "@common/Table";
import { TAG_STATUSES_ENUM } from "@common/TextTag/types";
import { Box, Stack } from "@mui/material";
import { useAppSelector } from "@redux/hooks";
import {
  selectMerchantStatusName,
  selectMerchantTab,
} from "@redux/slices/enterprise/merchants";
import GiveText from "@shared/Text/GiveText";
import { useCustomThemeV2 } from "@theme/hooks/useCustomThemeV2";
import StatusTag from "componentsV2/Table/StatusTag";
import { useMemo } from "react";
import { parseAmount } from "@utils/index";
import { getSelectedTab } from "features/Merchants/MerchantsTable/utils";
import { MerchantData } from "@hooks/enterprise-api/account/types";
import { formatInUTCMoment } from "@utils/date.helpers";
import { useGetFeatureFlagValues } from "FeatureFlags/useGetFeatureFlagValues";
import { checkPortals } from "@utils/routing";
import MainInfo from "componentsV2/Table/MainInfo";
import useWatchlist from "features/Merchants/MerchantsTable/hooks/useWatchList";
import { QKEY_LIST_ENTERPRISE_STATS } from "@constants/queryKeys";
import { getMerchantStatus } from "@features/Merchants/MerchantsTable/utils";
import GiveTooltip from "@shared/Tooltip/GiveTooltip";
 
export const useAllEnterpriseTabColumns = () => {
  const { isWideView, isMobileView } = useCustomThemeV2();
  const { isMerchantStatusRefactorEnabled, isNewApprovalFlowEnabled } =
    useGetFeatureFlagValues();
 
  const tab = useAppSelector(selectMerchantTab);
  const selectedTab = getSelectedTab(tab);
  const isUnderwritingSelected = selectedTab === "underwriting";
 
  const { addMerchantToWatchlist, removeMerchantFromWatchlist } = useWatchlist(
    QKEY_LIST_ENTERPRISE_STATS,
  );
 
  const { isAcquirerPortal } = checkPortals();
 
  const statusName = useAppSelector(selectMerchantStatusName);
 
  const providerNameCol = useMemo(
    () => ({
      key: "name",
      title: "Provider",
      columnType: "merchant-first" as const,
      sortable: true,
      rightSort: true,
      columnWidth: getColumnWidth(isUnderwritingSelected, isWideView),
      renderColumn: (row: MerchantData) => {
        return (
          <MainInfo
            row={{
              ...row,
              parentName: row?.class?.displayName || "",
            }}
            disabled={false}
            isSponsorTable={false}
            removeFromWatchlistHandler={() =>
              removeMerchantFromWatchlist(row?.accID, row?.name)
            }
            addToWatchlistHandler={() =>
              addMerchantToWatchlist(row.accID, row.name)
            }
            isAcquirerPortal={isAcquirerPortal}
            isEnterpriseTable={true}
            isWarnedMerchant={false}
            thumbnailType="provider"
          />
        );
      },
    }),
    [isWideView, isAcquirerPortal, isUnderwritingSelected],
  );
 
  const createdCol = useMemo(
    () => ({
      key: "createdAt,-accID",
      sortable: true,
      title: "Created",
      columnWidth: 1,
      hideColumn: !isWideView,
      renderColumn: (row: MerchantData) => {
        return (
          <Box flexGrow={1} minWidth="90px">
            <GiveText
              data-testid={`provider-signup-date-${row.createdAt}`}
              variant="bodyS"
            >
              {formatInUTCMoment(row.createdAt, "MMM DD, YYYY")}
            </GiveText>
          </Box>
        );
      },
    }),
    [isWideView],
  );
 
  const transactionsCountCol = useMemo(
    () => ({
      key: "transactionsCount",
      title: "Transactions",
      columnWidth: 1,
      sortable: true,
      hideColumn: !isWideView || isUnderwritingSelected,
 
      renderColumn: (row: MerchantData) => {
        return (
          <Box minWidth="90px" textAlign="center">
            <GiveText variant="bodyS">{row.transactionsCount}</GiveText>
          </Box>
        );
      },
    }),
    [isWideView],
  );
 
  const approvedMerchantsCol = useMemo(
    () => ({
      key: "approvedSubmerchants",
      title: "Approved Merchants",
      columnWidth: isWideView ? 1.2 : 0.5,
      hideColumn: isUnderwritingSelected,
      headerPosition: "center",
      sortable: true,
      renderColumn: (row: MerchantData) => {
        const approvedMerchants = row.approvedSubmerchants;
        const totalMerchants = row.totalSubmerchants;
 
        return (
          <Stack
            flexDirection="row"
            alignItems="center"
            width={isWideView ? "100%" : "auto"}
            sx={{
              justifyContent: isMobileView ? "end" : "center",
            }}
          >
            <GiveText variant="bodyS" color="success">
              {approvedMerchants}
            </GiveText>
            <GiveText variant="bodyS" data-testid="total-merchants">
              &nbsp;/&nbsp;{totalMerchants}
            </GiveText>
          </Stack>
        );
      },
    }),
    [isWideView],
  );
 
  const totalProcessedCol = useMemo(
    () => ({
      key: "totalProcessed",
      title: "Processed (USD)",
      headerPosition: "right",
      hideColumn: !isWideView || isUnderwritingSelected,
      columnWidth: 1,
      sortable: true,
      renderColumn: (row: MerchantData) => {
        const totalProcessed = row?.totalProcessed / 100;
        const revenue = parseAmount(totalProcessed);
 
        return (
          <Box display="flex" width="100%" justifyContent="flex-end">
            <GiveTooltip
              title={Math.abs(totalProcessed)?.toLocaleString()}
              placement="bottom"
              disableHoverListener={Math.abs(totalProcessed || 0) < 1_000_000}
            >
              <GiveText variant="bodyS">{revenue}</GiveText>
            </GiveTooltip>
          </Box>
        );
      },
    }),
    [isWideView, isUnderwritingSelected],
  );
 
  const statusCol = useMemo(() => {
    return {
      key: "stateSortOrder,-accID",
      title: "Status",
      columnWidth: isUnderwritingSelected ? "120px" : 1.1,
      headerPosition: "left" as TableColumnType["headerPosition"],
      sortable: true,
      rightSort: true,
      hideColumn: !isWideView,
      columnType: "status-round" as const,
      renderColumn: (row: MerchantData) => {
        const status = getMerchantStatus(
          row,
          false,
          isMerchantStatusRefactorEnabled,
          isNewApprovalFlowEnabled,
        ) as TAG_STATUSES_ENUM;
 
        const approverName = `${row.approvedByFirstName} ${row.approvedByLastName}`;
 
        return (
          <StatusTag
            statusDisplayName={status}
            updatedBy={approverName}
            updatedAt={+row.approvedAt}
          />
        );
      },
    };
  }, [statusName, isWideView, isUnderwritingSelected]);
 
  return {
    providerNameCol,
    createdCol,
    transactionsCountCol,
    approvedMerchantsCol,
    totalProcessedCol,
    statusCol,
  };
};
 
const getColumnWidth = (
  isUnderwritingSelected: boolean,
  isWideView: boolean,
) => {
  if (isUnderwritingSelected) {
    return isWideView ? 4 : 3;
  }
  return isWideView ? 2.5 : 2;
};