All files / src/components/Settings/ManageTeam/hooks useInviteTeamMember.tsx

74.07% Statements 20/27
41.66% Branches 5/12
80% Functions 4/5
73.07% Lines 19/26

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                    1x 25x 25x   25x 1x     25x                           25x   25x       1x 1x 1x           1x   1x       1x 1x 1x 1x                               1x         25x    
import { CreateMemberTypes, createTeamMember } from "@services/api/customer";
import { MemberRoleName } from "@customTypes/team.member";
import { useMutation, useQueryClient } from "react-query";
import { showMessage, clearToasts } from "@common/Toast/ShowToast";
import { useCustomTheme } from "@theme/hooks/useCustomTheme";
import { useGetCurrentMerchantId } from "@hooks/common";
import { QKEY_CHECK_NEW_NOTIFICATIONS } from "@constants/queryKeys";
import { customInstance } from "@services/api";
import { ProcessorValue } from "@features/Merchants/MerchantSidePanel/types";
 
export const useInviteTeamMember = () => {
  const queryClient = useQueryClient();
  const { merchantId } = useGetCurrentMerchantId();
 
  const { mutateAsync: inviteMember, isLoading: isInviting } = useMutation(
    (data: CreateMemberTypes) => createTeamMember(data),
  );
 
  const { mutateAsync: assignProcessor, isLoading: isAssigning } = useMutation(
    ({
      memberId,
      processorNames,
    }: {
      memberId: number;
      processorNames: ProcessorValue[];
    }) =>
      customInstance({
        url: `/accounts/${merchantId}/members/${memberId}/permissions`,
        method: "PATCH",
        data: { processorNames },
      }),
  );
  const { isMobileView } = useCustomTheme();
 
  const handleInvite = (
    value: MemberRoleName,
    processorId?: ProcessorValue,
  ) => {
    return async (data: any, cb?: any) => {
      try {
        const response = await inviteMember({
          email: data?.emailList[0],
          memberRole: value as any,
          merchantId,
        });
 
        const memberId = response?.user?.accID;
 
        Iif (processorId && memberId) {
          await assignProcessor({ memberId, processorNames: [processorId] });
        }
 
        showMessage("Invitation", "", !isMobileView, "Invitation Sent");
        queryClient.invalidateQueries("get-all-team-members");
        queryClient.invalidateQueries([QKEY_CHECK_NEW_NOTIFICATIONS]);
        Eif (cb?.onSuccess) cb.onSuccess();
      } catch (err: any) {
        const inputError = err?.response?.data?.input;
        const message =
          inputError?.[0]?.message || err?.response?.data?.message;
        if (cb?.onError) {
          cb.onError(message);
        } else {
          showMessage(
            "Error",
            "",
            !isMobileView,
            "Failed. Try to invite again.",
          );
        }
      } finally {
        clearToasts();
      }
    };
  };
 
  return { handleInvite, isLoading: isInviting || isAssigning };
};