All files / src/features/Merchants/MerchantsTable/hooks useUnderwritingColumns.tsx

77.41% Statements 24/31
50% Branches 19/38
77.77% Functions 7/9
77.41% Lines 24/31

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                                            33x 4003x         4003x 4003x 4003x 4003x   4003x 4003x 4003x   4003x 896x             132x                 4003x 862x                                               4003x 1203x               180x               180x             180x         180x                         4003x 862x                                                                                           4003x             4003x        
import { useMemo } from "react";
import { Box, Stack } from "@mui/material";
import { useFormatDateInTimezone } from "@utils/date.helpers";
import GiveDateTooltip from "@shared/Tooltip/GiveDateTooltip";
import { TAG_STATUSES_ENUM } from "@common/TextTag/types";
import { useAppSelector } from "@redux/hooks";
import { selectMerchantTab } from "@redux/slices/enterprise/merchants";
import { MerchantData } from "@hooks/enterprise-api/account/types";
import GiveText from "@shared/Text/GiveText";
import MerchantAssignmentManager from "@shared/MerchantAssignmentManager/MerchantAssignmentManager";
import { getMerchantStatus } from "../utils";
import { getSelectedTab } from "../utils";
import { useGetFeatureFlagValues } from "FeatureFlags/useGetFeatureFlagValues";
import { useCustomThemeV2 } from "@theme/hooks/useCustomThemeV2";
import GiveProgressBar from "@shared/ProgressBar/GiveProgressBar";
import CircularScore from "@shared/UnderwritingScore/CircularScore";
import { getTaxIdFormat } from "@utils/index";
 
type Props = {
  isEnterprise?: boolean;
};
 
const useUnderwritingColumns = ({ isEnterprise }: Props) => {
  const { formatInTimezone } = useFormatDateInTimezone();
  const {
    isMerchantStatusRefactorEnabled,
    isMerchantOnboardingModalEnabled,
    isNewApprovalFlowEnabled,
  } = useGetFeatureFlagValues();
  const { isWideView } = useCustomThemeV2();
  const tab = useAppSelector(selectMerchantTab);
  const selectedTab = getSelectedTab(tab);
 
  const isInvitedTabSelected = selectedTab === "invited";
  const isPendingTabSelected = selectedTab === "pending";
  const isKYBTabSelected = selectedTab === "kyb";
 
  const taxIdCol = useMemo(() => {
    return {
      key: "taxIDNumber,-accID",
      sortable: false,
      title: "Tax ID",
      columnWidth: isPendingTabSelected || isKYBTabSelected ? 1 : 1.4,
      hideColumn: isEnterprise && !isWideView,
      renderColumn: (row: MerchantData) => {
        return (
          <GiveText variant="bodyS">
            {getTaxIdFormat(row?.taxIDNumber, row?.tinType, true) || "-"}
          </GiveText>
        );
      },
    };
  }, [isPendingTabSelected, isKYBTabSelected, isWideView, isEnterprise]);
 
  const KYBCol = useMemo(() => {
    return {
      key: "kybApprovalAt,-accID",
      sortable: true,
      title: "KYB",
      columnWidth: 1.2,
      hideColumn: isEnterprise && !isWideView,
      renderColumn: (row: MerchantData) => {
        const date = row?.kybApprovalAt;
 
        const parsedDate = date
          ? formatInTimezone(Number(date), "MMM dd, yyyy")
          : "";
 
        return (
          <GiveDateTooltip>
            <GiveText data-testid={`give-risk-${date}`} variant="bodyS">
              {parsedDate}
            </GiveText>
          </GiveDateTooltip>
        );
      },
    };
  }, [isWideView, isEnterprise]);
 
  const assignedCol = useMemo(() => {
    return {
      key: "assigned",
      sortable: false,
      title: "Assigned",
      columnWidth: 1,
      columnType: "assignment" as const,
      hideColumn: isEnterprise && !isWideView,
      renderColumn: (row: MerchantData) => {
        const status = getMerchantStatus(
          row,
          isInvitedTabSelected,
          isMerchantStatusRefactorEnabled,
          isNewApprovalFlowEnabled,
          isMerchantOnboardingModalEnabled,
        ) as TAG_STATUSES_ENUM;
 
        Iif (
          status === TAG_STATUSES_ENUM.PENDING &&
          isMerchantOnboardingModalEnabled
        ) {
          return null;
        }
 
        const isRiskType = [
          TAG_STATUSES_ENUM.APPROVED,
          TAG_STATUSES_ENUM.SUSPENDED,
        ].includes(status);
 
        return (
          <MerchantAssignmentManager
            data={row as MerchantData}
            disabled={false}
            isRiskType={isRiskType}
            {...(isRiskType ? { type: "risk" } : {})}
            isEnterprise={isEnterprise}
          />
        );
      },
    };
  }, [tab, isEnterprise, isWideView]);
 
  const underwritingProgressCol = useMemo(() => {
    return {
      key: "completedTasksCount,-accID",
      sortable: true,
      title: "Underwriting Progress",
      columnWidth: !isWideView ? 1 : 1.7,
      columnType: isWideView ? "text" : "empty",
      renderColumn: (row: MerchantData) => {
        const { completedTasksCount = 0, totalTasksCount = 1 } = row || {};
 
        const percentage = (completedTasksCount * 100) / totalTasksCount;
 
        return (
          <Stack
            direction="row"
            alignItems="center"
            justifyContent="space-between"
            gap="12px"
          >
            <GiveText
              variant={isWideView ? "bodyS" : "bodyXS"}
              color={!totalTasksCount ? "tertiary" : "primary"}
            >
              {completedTasksCount}/{totalTasksCount}
            </GiveText>
            <Box width={isWideView ? "115px" : "auto"}>
              {isWideView ? (
                <GiveProgressBar
                  value={percentage}
                  variant={percentage === 100 ? "completed" : "incomplete"}
                  type="underwritingV2"
                />
              ) : (
                <CircularScore
                  defaultThreshold={100}
                  score={percentage}
                  simple
                  type="underwritingV2"
                />
              )}
            </Box>
          </Stack>
        );
      },
    };
  }, [isWideView]);
 
  const columns = {
    taxIdCol,
    KYBCol,
    assignedCol,
    underwritingProgressCol,
  };
 
  return columns;
};
 
export default useUnderwritingColumns;