All files / src/components/Merchants/MerchantPreview/hooks useUpdateBusinessProfileStatus.ts

93.33% Statements 14/15
25% Branches 1/4
100% Functions 4/4
93.33% Lines 14/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                            22x         87x 87x 87x   3x               3x         3x     3x       3x         87x         3x         3x 3x           87x    
import { showMessage } from "@common/Toast";
import { IParsedData } from "../data.types";
import { customInstance } from "@services/api";
import { useMutation, useQueryClient } from "react-query";
import {
  MERCHANT_SIDE_PANEL_PREVIEW_API_KEYS,
  UNDERWRITING_CHALLENGE_API_KEYS,
} from "@features/Merchants/MerchantSidePanel/constants";
type Props = {
  merchantId: number;
  data: IParsedData;
  onSuccess?: (data: any) => void;
};
 
export const useUpdateBusinessProfileStatus = ({
  merchantId,
  data,
  onSuccess,
}: Props) => {
  const queryClient = useQueryClient();
  const businessProfileId = data.businessProfile.id;
  const updateBusinessProfileStatusMutation = useMutation(
    (data: any) => {
      return customInstance({
        url: `/merchants/${merchantId}/legal-entities/${businessProfileId}/status`,
        method: "PATCH",
        data,
      });
    },
    {
      onSuccess: (data) => {
        queryClient.invalidateQueries([
          MERCHANT_SIDE_PANEL_PREVIEW_API_KEYS.GET_LEGAL_ENTITY,
          merchantId,
          businessProfileId,
        ]);
        queryClient.invalidateQueries(
          UNDERWRITING_CHALLENGE_API_KEYS.GET_TASKS,
        );
        queryClient.invalidateQueries([
          ...UNDERWRITING_CHALLENGE_API_KEYS.GET_STATS,
          merchantId,
        ]);
        onSuccess?.(data);
      },
    },
  );
 
  const handleUpdateStatus = async (
    status: string,
    reason?: string,
    shouldSendReason?: boolean,
  ) => {
    const payload = {
      status: status === "move_back_to_pending" ? "pending" : status,
      reason: reason,
      sendReason: shouldSendReason,
    };
    try {
      await updateBusinessProfileStatusMutation.mutateAsync({ ...payload });
    } catch (error: any) {
      showMessage("Error", error?.response?.data?.message || error?.message);
    }
  };
 
  return { handleUpdateStatus };
};