All files / src/components/Merchants/MerchantPreview/RiskProfile/hooks useRiskTriggersV2.ts

100% Statements 9/9
100% Branches 2/2
100% Functions 4/4
100% Lines 9/9

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            21x 1x                               21x 5x 5x     1x           1x         5x       5x          
import { customInstance } from "@services/api";
import { useState } from "react";
import { useQuery } from "react-query";
import { riskTriggersParser } from "../helpers/parsers";
import { TRiskTFactor } from "@components/Merchants/MerchantPreview/RiskProfile/types";
 
const getMerchantRiskProfile = (profileId: number, merchantId: number) => {
  return customInstance({
    url: `/merchants/${merchantId}/risk/merchant-profiles/${profileId}/factors`,
    method: "GET",
  });
};
 
export type RiskTriggerDataFormatted = {
  [category: string]: TRiskTFactor[];
};
 
export type RiskTriggerResponseData = { category: string } & TRiskTFactor;
 
type RiskTriggersResponse = {
  data: RiskTriggerResponseData[];
};
 
export const useRiskTriggersV2 = (profileId: number, merchantId: number) => {
  const [data, setData] = useState<RiskTriggersResponse>();
  const { data: defaultData, isLoading } = useQuery(
    ["risk-triggers", profileId, merchantId],
    async () => {
      return await getMerchantRiskProfile(profileId, merchantId);
    },
    {
      refetchOnWindowFocus: false,
      refetchOnMount: false,
      onSuccess(data) {
        setData(data);
      },
    },
  );
 
  const parsedTriggers = riskTriggersParser(
    data ? data?.data : defaultData?.data,
  );
 
  return {
    data: parsedTriggers,
    isLoading,
  };
};