All files / src/hooks/common useGetCurrentMerchantId.tsx

94.44% Statements 17/18
58.06% Branches 18/31
100% Functions 1/1
94.44% Lines 17/18

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                                          521x 22363x             22359x 22359x   22359x       22359x 22359x 22359x 22359x   22359x 22359x   22359x 22359x 22359x   22359x     22359x 22359x                                            
import { useAppSelector } from "@redux/hooks";
import { selectSelectedAccount } from "@redux/slices/auth/accounts";
import { selectMasqueradeMode } from "@redux/slices/app";
import { addSizeToImage } from "@components/UploadAvatar/UploadAvatar";
 
type ReturnedType = {
  merchantId: number;
  name?: string;
  img?: string;
  createdAt?: Date | number;
  isControlMode: boolean;
  selectedUser: ReturnType<typeof selectSelectedAccount>;
  masqFirstName?: string;
  isAcquirerLoggedIn?: boolean;
  isMerchantRole?: boolean;
  isProviderLoggedIn?: boolean;
  isSponsor: boolean;
  isAdminOrRiskManager: boolean;
  isAdminOrOwner: boolean;
};
 
const useGetCurrentMerchantId = (): ReturnedType => {
  const selectedUser = useAppSelector(selectSelectedAccount);
  const {
    id: masqID,
    name: masqName,
    imgSrc: masqImg,
    firstName: masqFirstName,
    masqueradeUserRole,
  } = useAppSelector(selectMasqueradeMode);
  let merchantId = selectedUser?.id || 0;
 
  Iif (masqName && typeof masqID === "number") {
    merchantId = masqID;
  }
 
  const name = masqName ? masqName : selectedUser?.name;
  const { img } = selectedUser || {};
  const blobImg = img && img?.includes("blob") ? img : "";
  const imgAvatar = masqImg || blobImg;
 
  const isAcquirerLoggedIn = selectedUser?.merchType === "acquirer";
  const isProviderLoggedIn = selectedUser?.merchType === "provider";
  const isMerchantRole =
    masqueradeUserRole === "merchant" && !!masqName && !!masqID;
  const userRole = selectedUser?.userRole || "";
  const isSponsor = userRole === "sponsor"; //we use it for multiple part of the app, it's best to have it exported from this hook already
 
  const isAdminOrRiskManager = ["admin", "risk_manager", "owner"].includes(
    userRole,
  );
  const isAdminOrOwner = ["admin", "owner"].includes(userRole);
  return {
    merchantId,
    name,
    img:
      imgAvatar && imgAvatar?.includes("blob")
        ? imgAvatar
        : imgAvatar && imgAvatar?.includes("https")
        ? addSizeToImage(imgAvatar, "thumb")
        : undefined,
    selectedUser,
    isControlMode: Boolean(masqName && masqID),
    masqFirstName,
    isAcquirerLoggedIn,
    isMerchantRole,
    isProviderLoggedIn,
    isSponsor,
    isAdminOrRiskManager,
    isAdminOrOwner,
  };
};
 
export default useGetCurrentMerchantId;