All files / src/components/ProfilePage/BusinessProfileSetup/hooks useBusinessDetailsData.tsx

69.76% Statements 30/43
40% Branches 14/35
61.53% Functions 8/13
69.04% Lines 29/42

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 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148                                                  44x 44x   44x 2x           44x 2x   44x 2x       2x       2x 2x 1x     1x     2x   2x 2x   2x           44x                                   44x 2x     2x       44x                   44x 2x                 44x 1x             44x                     19x       19x                        
import { useMutation, useQueryClient } from "react-query";
import { customInstance } from "@services/api";
import { useGetCurrentMerchantId } from "@hooks/common";
import { showMessage } from "@common/Toast";
import { generatePayload } from "../helpers/generatePayload";
import { onPrincipalError } from "../utils/principal.utils";
import {
  QKEY_BUSINESS_PROFILE_BY_ID,
  QKEY_GET_MERCHANT_BY_ID,
} from "@constants/queryKeys";
import { getMerchantErrorMessage } from "@components/Merchants/CreateMerchantPanel/helpers";
import { TSetFormValues } from "@components/ProfilePage/BusinessProfileSetupNew/types";
 
type Props = {
  legalEntityId?: number;
};
 
type TOptions = {
  handleNext?: () => void;
  makeApiCall?: boolean;
  dirtyFields?: Record<string, boolean | undefined>;
  onSuccess?: () => void;
};
 
function useBusinessDetailsData({ legalEntityId }: Props) {
  const { merchantId } = useGetCurrentMerchantId();
  const queryClient = useQueryClient();
 
  const refetchLegalEntity = () =>
    queryClient.invalidateQueries([
      QKEY_BUSINESS_PROFILE_BY_ID,
      legalEntityId,
      merchantId,
    ]);
 
  const refetchMerchant = () =>
    queryClient.invalidateQueries([QKEY_GET_MERCHANT_BY_ID, merchantId]);
 
  const submitStep: TSetFormValues = async (name, values, options) => {
    const isBusinessSection = ["businessAddress", "businessDetails"].includes(
      name,
    );
 
    const mutation = isBusinessSection
      ? createBusinessProfileMutation
      : updateMerchantMutation;
 
    let payload = generatePayload(name, values, options?.dirtyFields);
    if (name === "businessAddress") {
      payload = {
        address: payload,
      };
      await updateMerchantMutation.mutateAsync(payload, { onError });
    }
 
    mutation.mutate(payload, {
      onSuccess: () => {
        Eif (isBusinessSection) refetchLegalEntity();
        refetchMerchant();
 
        options?.handleNext && options.handleNext();
      },
      onError,
    });
  };
 
  const submitPrincipal = (values: any, options?: TOptions) => {
    createBusinessOwnerMutation.mutate(values, {
      onSuccess: () => {
        const message = values?.id
          ? "Business owner updated successfully"
          : "Business owner created successfully";
 
        showMessage("Success", message);
 
        refetchLegalEntity();
 
        options?.onSuccess && options.onSuccess();
        options?.handleNext && options.handleNext();
      },
      onError: onPrincipalError,
    });
  };
 
  const setFormValues: TSetFormValues = (name, values, options) => {
    Iif (name === "businessOwners") {
      submitPrincipal(values, options);
    } else {
      submitStep(name, values, options);
    }
  };
 
  const createBusinessOwnerMutation = useMutation((data: any) => {
    return customInstance({
      url: data?.id
        ? `/merchants/${merchantId}/legal-entities/${legalEntityId}/principals/${data?.id}`
        : `/merchants/${merchantId}/legal-entities/${legalEntityId}/principals`,
      method: data?.id ? "PATCH" : "POST",
      data,
    });
  });
 
  const createBusinessProfileMutation = useMutation((data: any) => {
    return customInstance({
      url: legalEntityId
        ? `/merchants/${merchantId}/legal-entities/${legalEntityId}`
        : `/merchants/${merchantId}/legal-entities`,
      method: legalEntityId ? "PATCH" : "POST",
      data,
    });
  });
 
  const updateMerchantMutation = useMutation((data: any) => {
    return customInstance({
      url: `/merchants/${merchantId}`,
      method: "PATCH",
      data,
    });
  });
 
  return {
    setFormValues,
    isLoading:
      createBusinessProfileMutation.isLoading ||
      updateMerchantMutation.isLoading ||
      createBusinessOwnerMutation.isLoading,
  };
}
 
export default useBusinessDetailsData;
 
const onError = (error: any) => {
  showMessage("Error", getAPIError(error));
};
 
const getAPIError = (error: any) => {
  const input = error?.response?.data?.input?.[0] || null;
 
  if (input?.param === "taxIDNumber" && input?.code === "duplicate") {
    return `The provided "EIN" value is already taken.`;
  }
 
  return (
    getMerchantErrorMessage(input?.param, input?.code, input?.message) ||
    error?.response?.data?.message
  );
};