All files / src/components/Merchants/MerchantPreview/modals/hooks useManagePepData.ts

65.38% Statements 17/26
78.57% Branches 11/14
63.63% Functions 7/11
65.21% Lines 15/23

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                                  300x     300x   300x       300x   300x     37x           24x   24x                   23x 23x                     300x   1x                                 1x                 300x   300x                    
import { useState } from "react";
import { useMutation, useQuery, useQueryClient } from "react-query";
import { showMessage } from "@common/Toast";
import {
  checkPEPMutation,
  getCheckHistory,
} from "@services/api/politicallyExposed/politicallyExposedActions";
import { MERCHANT_SIDE_PANEL_PREVIEW_API_KEYS } from "@features/Merchants/MerchantSidePanel/constants";
import { GET_HISTORY_LIST } from "@features/Merchants/MerchantSidePanel/UnderwritingTasks/constants";
import { PEPResourcesIDs, THistoryCheck } from "@components/Merchants/MerchantPreview/PEP/types";
import { getListDummyData, parser } from "@components/Merchants/MerchantPreview/modals/helpers";
 
export default function useManagePepData({
  merchantId,
  legalEntityID,
  selectedOwner,
}: PEPResourcesIDs) {
  const [selectedCheck, setSelectedCheck] = useState<THistoryCheck | undefined>(
    undefined,
  );
  const queryClient = useQueryClient();
 
  const handleClickCheck = (check: THistoryCheck) => {
    setSelectedCheck(check);
  };
 
  const removeSelectedPEP = () => setSelectedCheck(undefined);
 
  const { data, isLoading, refetch, isFetching } = useQuery(
    ["pep-checks-history", selectedOwner?.id, merchantId, legalEntityID],
    async () => {
      const list = await getCheckHistory({
        selectedOwner,
        merchantId,
        legalEntityID,
      });
 
      const parsedData = list?.data.map(parser);
 
      return {
        data: parsedData || [],
        total: list.total || 0,
      };
    },
    {
      refetchOnWindowFocus: false,
      enabled: !!selectedOwner?.id && !!legalEntityID,
      staleTime: 5 * 60 * 1000, // 5 minutes
      onSuccess: async (data) => {
        setSelectedCheck((prev) => {
          Eif (!prev) return undefined;
          const obj = data?.data?.find(
            (item: any) => item?.checkID === prev.checkID,
          );
          return obj;
        });
      },
      retry: 1,
    },
  );
 
  const { mutate, isLoading: isRunningCheck } = useMutation(
    () =>
      checkPEPMutation({
        merchantId,
        selectedOwner,
        legalEntityID,
      }),
    {
      onSuccess() {
        showMessage("Success", "PEP Check is completed sucessfully", false);
        queryClient.invalidateQueries({
          queryKey: [GET_HISTORY_LIST, merchantId],
        });
        queryClient.invalidateQueries([
          MERCHANT_SIDE_PANEL_PREVIEW_API_KEYS.GET_LEGAL_ENTITY,
        ]);
        refetch();
      },
      onError: () => {
        showMessage(
          "Warning",
          "PEP Check is not successfully completed, the request is in queue",
          false,
        );
      },
    },
  );
 
  const checkPEP = () => selectedOwner?.id && mutate();
 
  return {
    data: data?.data ? data?.data : getListDummyData(selectedOwner?.createdAt),
    handleClickCheck,
    selectedCheck,
    removeSelectedPEP,
    isLoading: isFetching || isLoading,
    checkPEP,
    isRunningCheck,
  };
}