All files / src/features/TransactionsRiskProfile/hooks useGetRiskProfile.tsx

94.11% Statements 16/17
90% Branches 9/10
100% Functions 4/4
94.11% Lines 16/17

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                                        51x   51x 15x 15x   15x           15x           15x   15x   3x             15x 15x     3x                 15x     3x                     15x         15x                    
import {
  getRiskProfile,
  getRiskProfileTransactions,
} from "@services/api/riskProfile/transactionsRiskProfile";
import { useQuery } from "react-query";
import useDataParser from "../helpers/parsers";
import { useAppSelector } from "@redux/hooks";
import {
  selectTimeFilterQuery,
  selectTypeFilter,
} from "@redux/slices/acquirer/transactionRiskProfile";
import { useModal } from "@ebay/nice-modal-react";
import {
  QKEY_LIST_RISK_PROFILE_TRANSACTIONS,
  QKEY_RISK_PROFILE,
} from "@constants/queryKeys";
import moment from "moment";
import { useAccessControl } from "features/Permissions/AccessControl";
import RESOURCE_BASE, { OPERATIONS } from "@constants/permissions";
 
const last24Hours = moment(new Date()).subtract(24, "hours").unix();
 
const useGetRiskProfile = (id: string) => {
  const modal = useModal();
  const typeFilter = useAppSelector(selectTypeFilter);
 
  const isListIPProfileAllowed = useAccessControl({
    resource: RESOURCE_BASE.IPPROFILE,
    operation: OPERATIONS.LIST,
    withPortal: true,
  });
 
  const isListTransactionAllowed = useAccessControl({
    resource: RESOURCE_BASE.IPPROFILE_TRANSACTION,
    operation: OPERATIONS.LIST,
    withPortal: true,
  });
 
  const isRequestEnabled = modal.visible && !!id;
 
  const { data, isLoading } = useQuery(
    [QKEY_RISK_PROFILE, id],
    async () => await getRiskProfile(id),
    {
      refetchOnWindowFocus: false,
      enabled: isRequestEnabled && isListIPProfileAllowed,
    },
  );
 
  const timeFilter = useAppSelector(selectTimeFilterQuery);
  const { data: transactions, isLoading: isLoadingTransactions } = useQuery(
    [QKEY_LIST_RISK_PROFILE_TRANSACTIONS, id, timeFilter],
    async () =>
      await getRiskProfileTransactions(id, {
        filter: timeFilter,
        groupBy: "day",
      }),
    {
      refetchOnWindowFocus: false,
      enabled: isRequestEnabled && isListTransactionAllowed,
    },
  );
  const { data: hasMore } = useQuery(
    ["has-more-transactions", id],
    async () => {
      const hasMore = await getRiskProfileTransactions(id, {
        filter: `createdAt:<d${last24Hours}&max=1`,
      });
      return hasMore?.total;
    },
    {
      refetchOnWindowFocus: false,
      enabled: isRequestEnabled && isListTransactionAllowed,
    },
  );
 
  const customData = useDataParser({
    data,
    transactions,
  });
 
  return {
    data: customData,
    isLoading: isLoading || isLoadingTransactions,
    hasMore: hasMore > 0,
    isListIPProfileAllowed,
    rawData: data,
  };
};
 
export default useGetRiskProfile;