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

50% Statements 8/16
0% Branches 0/2
20% Functions 1/5
53.33% Lines 8/15

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                              21x 5x 5x 5x   5x               5x               5x                                                     5x        
import { showMessage } from "@common/Toast";
import { customInstance } from "@services/api";
import { useMutation, useQueryClient } from "react-query";
import { TRiskStatus } from "../types";
import { QKEY_LIST_ACQUIRER_MERCHANTS } from "@constants/queryKeys";
import {
  composePermission,
  useAccessControl,
} from "features/Permissions/AccessControl";
import RESOURCE_BASE, { OPERATIONS } from "@constants/permissions";
import { MERCHANT_SIDE_PANEL_PREVIEW_API_KEYS } from "@features/Merchants/MerchantSidePanel/constants";
import { useAppSelector } from "@redux/hooks";
import { selectMerchantTab } from "@redux/slices/enterprise/merchants";
import { checkPortals } from "@utils/routing";
 
const useUpdateRiskProfile = (profileId: number, merchantId: number) => {
  const queryClient = useQueryClient();
  const tab = useAppSelector(selectMerchantTab);
  const { isEnterprisePortal } = checkPortals();
 
  const isAllowedUpdate = useAccessControl({
    resource: composePermission(
      RESOURCE_BASE.MERCHANT,
      RESOURCE_BASE.RISK_PROFILE,
    ),
    operation: OPERATIONS.UPDATE,
  });
 
  const updateEscalation = useMutation((data: any) => {
    return customInstance({
      url: `/merchants/${merchantId}/risk/merchant-profiles/${profileId}`,
      method: "PATCH",
      data,
    });
  });
 
  const handleChange = (status: TRiskStatus) => {
    if (!isAllowedUpdate) return;
    updateEscalation.mutate(
      { riskStatus: status },
      {
        onError: (err: any) => {
          showMessage("Error", err?.response?.data?.message);
        },
        onSuccess: (data) => {
          queryClient.setQueryData(
            [
              MERCHANT_SIDE_PANEL_PREVIEW_API_KEYS.RISK_PROFILE,
              profileId,
              merchantId,
            ],
            data,
          );
          queryClient.invalidateQueries([QKEY_LIST_ACQUIRER_MERCHANTS, tab]);
          queryClient.invalidateQueries([
            MERCHANT_SIDE_PANEL_PREVIEW_API_KEYS.GET,
            merchantId,
          ]);
        },
      },
    );
  };
 
  return { handleChange, isAllowedUpdate };
};
 
export default useUpdateRiskProfile;