All files / src/features/Merchants/MerchantSidePanel/hooks useUpdateManualRiskLevel.ts

100% Statements 18/18
100% Branches 4/4
100% Functions 5/5
100% Lines 17/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                                            22x 190x 190x   190x               190x 4x             190x       5x 4x   1x               3x         3x       3x   3x 3x       3x         190x                
import { showMessage } from "@common/Toast";
import { customInstance } from "@services/api";
import { useMutation, useQueryClient } from "react-query";
import {
  TRiskLevel,
  TMerchantRiskProfile,
} from "@components/Merchants/MerchantPreview/RiskProfile/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";
 
type ManualRiskLevelPayload = {
  riskLevel: TRiskLevel | null;
  reason: string;
};
 
const useUpdateManualRiskLevel = (profileId: number, merchantId: number) => {
  const queryClient = useQueryClient();
  const tab = useAppSelector(selectMerchantTab);
 
  const canOverride = useAccessControl({
    resource: composePermission(
      RESOURCE_BASE.MERCHANT,
      RESOURCE_BASE.RISK_PROFILE_RISK_LEVEL,
    ),
    operation: OPERATIONS.UPDATE,
  });
 
  const mutation = useMutation((data: ManualRiskLevelPayload) =>
    customInstance({
      url: `/merchants/${merchantId}/risk/merchant-profiles/${profileId}/risk-level`,
      method: "PATCH",
      data,
    }),
  );
 
  const updateManualRiskLevel = (
    payload: ManualRiskLevelPayload,
    opts?: { onSuccess?: (data?: TMerchantRiskProfile) => void },
  ) => {
    if (!canOverride) return;
    mutation.mutate(payload, {
      onError: (err: any) => {
        showMessage(
          "Error",
          err?.response?.data?.message ||
            "Something went wrong while updating the risk level. Please try again.",
        );
      },
      onSuccess: (data) => {
        // Refresh the risk headline (effective level + manual flags) and the badges.
        queryClient.invalidateQueries([
          MERCHANT_SIDE_PANEL_PREVIEW_API_KEYS.RISK_PROFILE,
          profileId,
          merchantId,
        ]);
        queryClient.invalidateQueries([
          MERCHANT_SIDE_PANEL_PREVIEW_API_KEYS.GET,
          merchantId,
        ]);
        queryClient.invalidateQueries([QKEY_LIST_ACQUIRER_MERCHANTS, tab]);
        // Surface the BE-posted "Manual Risk Level" conversation message + changelog audit entry.
        queryClient.invalidateQueries(["get-conversation-topics", merchantId]);
        queryClient.invalidateQueries([
          MERCHANT_SIDE_PANEL_PREVIEW_API_KEYS.GET_CHANGELOG,
          merchantId,
        ]);
        opts?.onSuccess?.(data);
      },
    });
  };
 
  return {
    updateManualRiskLevel,
    isLoading: mutation.isLoading,
    canOverride,
  };
};
 
export default useUpdateManualRiskLevel;