All files / src/components/Merchants/MerchantPreview/OFAC/hooks useUpdateOFACCheck.tsx

83.33% Statements 15/18
25% Branches 2/8
83.33% Functions 5/6
83.33% Lines 15/18

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                                          2x             2x             20x   20x   20x   1x                   20x           1x       1x       1x                                   1x     1x       1x       1x                 20x        
import { customInstance } from "@services/api";
import { useMutation, useQueryClient } from "react-query";
import { showMessage } from "@common/Toast";
import { updatePermissions } from "@redux/slices/app";
import { useAppDispatch } from "@redux/hooks";
import { MERCHANT_SIDE_PANEL_PREVIEW_API_KEYS } from "@features/Merchants/MerchantSidePanel/constants";
import { GET_HISTORY_LIST } from "@features/Merchants/MerchantSidePanel/UnderwritingTasks/constants";
 
type Status =
  | "clear"
  | "manually_cleared"
  | "possible_match"
  | "confirmed_match";
 
export type TOFACEndpointBuilder = {
  data: { status: Status };
  merchantId: number;
  legalEntityId?: number;
  ofacId: number;
};
 
export const lastCheckMapper = {
  possible_match: "Possible match",
  confirmed_match: "Confirmed match",
  clear: "Clear",
  manually_cleared: "Manually cleared",
};
 
export const useUpdateOFACCheck = ({
  tab,
  onSuccessCallback,
}: {
  tab: string;
  onSuccessCallback?: (data: any) => void;
}) => {
  const dispatch = useAppDispatch();
 
  const queryClient = useQueryClient();
  // {{URL}}/merchants/:MERCH_ID/legal-entities/:LEGAL_ID/ofac-checks/:OFAC_ID/status
  const updateOFACCheck = useMutation(
    ({ data, merchantId, legalEntityId, ofacId }: TOFACEndpointBuilder) => {
      return customInstance({
        url: legalEntityId
          ? `/merchants/${merchantId}/legal-entities/${legalEntityId}/ofac-checks/${ofacId}/status`
          : `/merchants/${merchantId}/ofac-checks/${ofacId}/status`,
        method: "PATCH",
        data,
      });
    },
  );
 
  const handleSubmit = ({
    data,
    merchantId,
    legalEntityId,
    ofacId,
  }: TOFACEndpointBuilder) => {
    updateOFACCheck.mutate(
      { data, merchantId, legalEntityId, ofacId },
      {
        onSuccess: (data) => {
          queryClient.setQueryData(
            ["get-ofac-check-by-id", merchantId, ofacId],
            data,
          );
          onSuccessCallback && onSuccessCallback(data);
        },
        onError: (error: any) => {
          showMessage(
            "Error",
            error?.message || error?.response?.status,
            true,
            "Something went wrong...",
          );
          if (error.not_authorized) {
            dispatch(
              updatePermissions({
                ofac_check: true,
              }),
            );
          }
        },
        onSettled: () => {
          queryClient.invalidateQueries({
            queryKey: [GET_HISTORY_LIST, merchantId],
          });
          queryClient.invalidateQueries([
            MERCHANT_SIDE_PANEL_PREVIEW_API_KEYS.GET_OFAC,
            merchantId,
          ]);
          queryClient.invalidateQueries([
            MERCHANT_SIDE_PANEL_PREVIEW_API_KEYS.GET,
            merchantId,
          ]);
          queryClient.invalidateQueries([
            MERCHANT_SIDE_PANEL_PREVIEW_API_KEYS.GET_LEGAL_ENTITY,
            merchantId,
          ]);
        },
      },
    );
  };
 
  return {
    handleSubmit,
  };
};