All files / src/hooks/common useCheckSponsor.tsx

92.85% Statements 13/14
100% Branches 6/6
75% Functions 3/4
92.85% Lines 13/14

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        55x 5026x   5026x     158x                         5026x   5026x   5026x     55x 2x   2x                               2x   2x   2x        
import { useGetCurrentMerchantId } from "@hooks/common";
import { customInstance } from "@services/api";
import { useQuery } from "react-query";
 
const useCheckSponsor = () => {
  const { merchantId } = useGetCurrentMerchantId();
 
  const { data, isLoading } = useQuery(
    ["check-sponsor-member", merchantId],
    async () => {
      return await customInstance({
        url: `/accounts?filter=(id:${merchantId})`,
        method: "GET",
      });
    },
    {
      staleTime: 5 * 60 * 1000, // 5 mins
      refetchOnWindowFocus: false,
      refetchOnMount: false,
      enabled: Boolean(merchantId),
    },
  );
 
  const role = data?.data?.[0]?.userRole || "";
 
  const hasSponsorAccess = ["sponsor", "owner", "admin"].includes(role);
 
  return { isLoading, hasSponsorAccess };
};
 
export const useCheckSponsorRole = () => {
  const { merchantId } = useGetCurrentMerchantId();
 
  const { data, isLoading } = useQuery(
    ["check-sponsor-member", merchantId],
    async () => {
      return await customInstance({
        url: `/accounts?filter=(id:${merchantId})`,
        method: "GET",
      });
    },
    {
      staleTime: 5 * 60 * 1000, // 5 mins
      refetchOnMount: false,
      refetchOnWindowFocus: false,
      enabled: Boolean(merchantId),
    },
  );
 
  const role = data?.data?.[0]?.userRole || "";
 
  const isSponsor = !isLoading && role === "sponsor";
 
  return { isLoading, isSponsor };
};
 
export default useCheckSponsor;