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

64.7% Statements 22/34
75% Branches 9/12
36.36% Functions 4/11
64.7% Lines 22/34

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 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132                                            2184x 2184x   2184x 2184x   2184x   2184x   2184x 6x 6x                   2184x   6x               6x         2184x       2184x 2184x   2184x       2184x   2184x                         2184x                       2184x                       2184x                       2184x                    
import { useMutation, useQueryClient } from "react-query";
import { useMerchantSidePanelContext } from "../Provider/MerchantSidePanelProvider";
import { customInstance } from "@services/api";
import { MERCHANT_SIDE_PANEL_PREVIEW_API_KEYS } from "../constants";
import { useResyncMSP } from "@components/Merchants/MerchantPreview/hooks/useResyncMSP";
import { showMessage } from "@common/Toast";
import { useUpdateUnderwriting } from "@components/Merchants/MerchantPreview/hooks/useUnderwritingUpdate";
import {
  QKEY_LIST_ACQUIRER_MERCHANTS,
  QKEY_LIST_ENTERPRISE_STATS,
  QKEY_LIST_MERCHANT_STATS,
} from "@constants/queryKeys";
import { MerchantPayload } from "./hook.types";
import { TMerchantBaseContextData } from "@components/Merchants/MerchantPreview/data.types";
import { useRiskUpdate } from "@features/Merchants/MerchantSidePanel/hooks/useRiskUpdate";
import { useGetFeatureFlagValues } from "FeatureFlags/useGetFeatureFlagValues";
import { useRefetchCounters } from "@hooks/acquirer-api/merchants/stats/useGetMerchantCounters";
 
export default function useUpdateMerchant(
  defaultContext?: TMerchantBaseContextData,
  merchantID?: number,
) {
  const context = useMerchantSidePanelContext();
  const { handleRefetchCounters } = useRefetchCounters();
  const { isMerchantStatusRefactorEnabled, isNewApprovalFlowEnabled } =
    useGetFeatureFlagValues();
  const { data, id, isProvider } = defaultContext ?? context;
 
  const merchantId = id || merchantID;
 
  const queryClient = useQueryClient();
 
  const handleSuccess = (data?: any) => {
    if (data)
      queryClient.setQueryData(
        [MERCHANT_SIDE_PANEL_PREVIEW_API_KEYS.GET, merchantId],
        data,
      );
    else
      EqueryClient.invalidateQueries([
        MERCHANT_SIDE_PANEL_PREVIEW_API_KEYS.GET,
        merchantId,
      ]);
  };
  const updateMerchantMutation = useMutation(
    (payload: Partial<MerchantPayload | any>) => {
      return customInstance({
        url: `/merchants/${merchantId}`,
        method: "PATCH",
        data: payload,
      });
    },
    {
      onSuccess(data) {
        handleSuccess(data);
      },
    },
  );
 
  const { isLoading: isReSyncMSPLoading, handleResyncMSP } = useResyncMSP(
    data?.status?.accID,
  );
 
  const { updateUnderwriting } = useUpdateUnderwriting(data?.status?.accID);
  const { updateRiskStatus } = useRiskUpdate(data?.status?.accID);
 
  const usedUpdate = isMerchantStatusRefactorEnabled
    ? updateRiskStatus
    : updateUnderwriting;
 
  const mutationFieldName = isNewApprovalFlowEnabled ? "state" : "status";
 
  const refreshMerchantData = () => {
    const listQueryKey = isProvider
      ? QKEY_LIST_ENTERPRISE_STATS
      : QKEY_LIST_ACQUIRER_MERCHANTS;
    queryClient.invalidateQueries([
      MERCHANT_SIDE_PANEL_PREVIEW_API_KEYS.GET,
      data?.status?.accID,
    ]);
    queryClient.invalidateQueries(QKEY_LIST_MERCHANT_STATS);
    handleRefetchCounters();
    queryClient.invalidateQueries(listQueryKey);
  };
 
  const handleSuspendMutation = () => {
    usedUpdate.mutate(
      { [mutationFieldName]: "suspended" },
      {
        onSuccess: refreshMerchantData,
        onError: (err: any) => {
          showMessage("Error", err?.response?.data?.message);
        },
      },
    );
  };
 
  const handleApproveMutation = () => {
    usedUpdate.mutate(
      { [mutationFieldName]: "approved" },
      {
        onSuccess: refreshMerchantData,
        onError: (err: any) => {
          showMessage("Error", err?.response?.data?.message);
        },
      },
    );
  };
 
  const handleResumeMutation = () => {
    updateUnderwriting.mutate(
      { status: "preapproved_no_tx" },
      {
        onSuccess: refreshMerchantData,
        onError: (err: any) => {
          showMessage("Error", err?.response?.data?.message);
        },
      },
    );
  };
 
  return {
    updateMerchantMutation,
    isReSyncMSPLoading,
    handleResyncMSP,
    handleSuspendMutation,
    handleApproveMutation,
    handleResumeMutation,
    isApproveSuspendLoading: updateUnderwriting.isLoading,
  };
}