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

44.59% Statements 33/74
30.95% Branches 26/84
55.55% Functions 5/9
45.07% Lines 32/71

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                                            41x                     3266x 3266x 3266x 3266x 3266x 3266x 3266x 3266x   3266x       3266x       3266x       3266x 3266x   3266x                                                                       3266x     1672x     3266x                                   3266x   36x         36x                                                     3266x 3266x           3266x 3266x 840x 840x     840x   840x   840x     840x                     3266x                          
import { useAppDispatch, useAppSelector } from "@redux/hooks";
import {
  resetMarkedMerchants,
  selectMarkedMerchants,
  setAllMerchantsMarked,
  setMerchantExcluded,
  setMerchantIncluded,
} from "@redux/slices/enterprise/merchants";
import { useCallback, useMemo } from "react";
import useCheckSponsor from "@hooks/common/useCheckSponsor";
import { useGetCurrentMerchantId } from "@hooks/common";
import useFindMerchantStatsById from "@hooks/acquirer-api/merchants/stats/useFindMerchantStatsById";
import { useTableFilters } from "@redux/slices/tableFilters";
import { MerchantData } from "@hooks/enterprise-api/account/types";
import { useGetMerchantCounters } from "@hooks/acquirer-api/merchants/stats/useGetMerchantCounters";
import { useGetFeatureFlagValues } from "FeatureFlags/useGetFeatureFlagValues";
import {
  MarkedMerchant,
  PartialFoundInType,
} from "@redux/slices/enterprise/types";
import { APPROVAL_TYPE, FOUND_IN } from "@redux/slices/enterprise/consts";
 
export const useBulkMarkMerchants = () => {
  const {
    include,
    exclude,
    auto,
    pre,
  }: {
    auto: boolean;
    pre: boolean;
    exclude: MarkedMerchant[];
    include: MarkedMerchant[];
  } = useAppSelector(selectMarkedMerchants) || [];
  const dispatch = useAppDispatch();
  const { hasSponsorAccess } = useCheckSponsor();
  const { merchantId } = useGetCurrentMerchantId();
  const { isMerchantStatusRefactorEnabled } = useGetFeatureFlagValues();
  const { data: statsData } = useFindMerchantStatsById(merchantId);
  const { data: counter } = useGetMerchantCounters();
  const { quickFilter } = useTableFilters();
  const readyForReviewCount =
    (isMerchantStatusRefactorEnabled
      ? counter?.sponsorReadyForReviewCount
      : statsData?.merchantSponsorReadyForReviewCount) || 0;
  const autoApprovalCount =
    (isMerchantStatusRefactorEnabled
      ? counter?.autoApprovalCount
      : statsData?.merchantSponsorAutoApprovalCount) || 0;
  const preApprovalCount =
    (isMerchantStatusRefactorEnabled
      ? counter?.preApprovalCount
      : statsData?.merchantSponsorPreApprovalCount) || 0;
 
  const isAutoApproval = quickFilter === "auto_approval";
  const isPreApproval = quickFilter === "pre_approval";
 
  const toggleMark = useCallback(
    ({ id, mark, row }: { id: number; mark: boolean; row: MerchantData }) => {
      if (isMerchantStatusRefactorEnabled) {
        const foundIn: PartialFoundInType =
          row.approvalStateName === "auto_approval"
            ? FOUND_IN.AUTO
            : FOUND_IN.PRE;
 
        if (
          (row.approvalStateName === "pre_approval" && pre) ||
          (row.approvalStateName === "auto_approval" && auto)
        ) {
          dispatch(setMerchantExcluded({ id, mark, foundIn }));
          return;
        }
 
        dispatch(setMerchantIncluded({ id, mark, foundIn }));
      } else {
        const foundIn: PartialFoundInType =
          row.lastInternalApprovalType === APPROVAL_TYPE.AUTO
            ? FOUND_IN.AUTO
            : FOUND_IN.PRE;
 
        if (
          (row.lastInternalApprovalType === APPROVAL_TYPE.PRE && pre) ||
          (row.lastInternalApprovalType === APPROVAL_TYPE.AUTO && auto)
        ) {
          dispatch(setMerchantExcluded({ id, mark, foundIn }));
          return;
        }
 
        dispatch(setMerchantIncluded({ id, mark, foundIn }));
      }
    },
    [auto, pre, include, exclude],
  );
  const checkIfMerchantDisabled = (status: string) => {
    // disabled if no sponsor permisison
 
    Eif (!hasSponsorAccess) return true;
    return !["ready_for_review"].includes(status);
  };
  const toggleBulkMark = useCallback(
    (markAll: boolean) => {
      if (isAutoApproval) {
        dispatch(resetMarkedMerchants({ type: FOUND_IN.AUTO }));
        dispatch(setAllMerchantsMarked({ table: FOUND_IN.AUTO, markAll }));
        return;
      } else if (isPreApproval) {
        dispatch(resetMarkedMerchants({ type: FOUND_IN.PRE }));
        dispatch(setAllMerchantsMarked({ table: FOUND_IN.PRE, markAll }));
        return;
      }
 
      dispatch(resetMarkedMerchants({}));
      dispatch(setAllMerchantsMarked({ table: FOUND_IN.ALL, markAll }));
    },
    [isAutoApproval, isPreApproval],
  );
 
  const checkIfMerchantMarked = (row: any) => {
    // always unchecked if disabled
    Eif (
      checkIfMerchantDisabled(
        isMerchantStatusRefactorEnabled ? row.stateName : row.sponsorStatus,
      )
    )
      return false;
 
    if (exclude.some((merchant) => merchant.id === row.accID)) {
      return false;
    }
    if (include.some((merchant) => merchant.id === row.accID)) {
      return true;
    }
    if (isMerchantStatusRefactorEnabled) {
      if (row.approvalStateName === "pre_approval" && pre) {
        return true;
      }
      if (row.approvalStateName === "auto_approval" && auto) {
        return true;
      }
    } else {
      if (row.lastInternalApprovalType === APPROVAL_TYPE.PRE && pre) {
        return true;
      }
      if (row.lastInternalApprovalType === APPROVAL_TYPE.AUTO && auto) {
        return true;
      }
    }
 
    return false;
  };
 
  const getIsMarkAllChecked = useCallback(() => {
    return (
      (isAutoApproval && auto) ||
      (isPreApproval && pre) ||
      (!isAutoApproval && !isPreApproval && auto && pre)
    );
  }, [isAutoApproval, auto, isPreApproval, pre]);
  const isMarkAllChecked = getIsMarkAllChecked();
  const selectedMerchantsCount = useMemo(() => {
    let totalCount = include?.length;
    const commonCount = isMerchantStatusRefactorEnabled
      ? readyForReviewCount
      : autoApprovalCount + preApprovalCount;
    Iif (auto && pre) {
      totalCount = commonCount - exclude?.length;
    } else Iif (auto) {
      totalCount = autoApprovalCount - exclude?.length;
    } else Iif (pre) {
      totalCount = preApprovalCount - exclude?.length;
    }
    return totalCount || 0;
  }, [
    autoApprovalCount,
    preApprovalCount,
    readyForReviewCount,
    include,
    exclude,
    auto,
    pre,
  ]);
 
  return {
    toggleMark,
    toggleBulkMark,
    checkIfMerchantMarked,
    isMarkAllChecked,
    checkIfMerchantDisabled,
    selectedMerchantsCount,
    merchantId,
    preApprovalCount,
    autoApprovalCount,
    readyForReviewCount,
  };
};