All files / src/services/api accounts.ts

77.27% Statements 17/22
60% Branches 3/5
70% Functions 7/10
76.19% Lines 16/21

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            8x             8x                                             8x 45x           8x 267x   267x     45x   45x       45x           267x 58x   40x       267x     8x 270x                                
import { UseQueryOptions, useQuery } from "react-query";
import { customInstance } from ".";
import { SelectOptionProps } from "@common/Select/types";
import { MemberRoleName } from "@customTypes/team.member";
import { useGetCurrentMerchantId } from "@hooks/common";
 
const getAccountOwner = ({ id }: any) => {
  return customInstance({
    url: `/accounts/${id}/owner`,
    method: "GET",
  });
};
 
export const useGetAccountOwner = (
  id: number,
  options: Omit<
    UseQueryOptions<any, any, any, any>,
    "queryKey" | "queryFn"
  > = {},
) => {
  const { data, error, isLoading } = useQuery(
    ["get-account-owner", id],
    async () => {
      const data = await getAccountOwner({ id });
      return data;
    },
    {
      refetchOnWindowFocus: false,
      retry: 1,
      ...options,
    },
  );
 
  return { data, error, isLoading };
};
 
const getRoles = () => {
  return customInstance({
    url: `/member-roles`,
    method: "GET",
  });
};
 
export const useRoles = () => {
  const { merchantId } = useGetCurrentMerchantId();
 
  const response = useQuery(
    ["get-users_role", merchantId],
    async () => {
      const roles = await getRoles();
 
      const data: RolesData = {
        ...roles,
        data: ParseData(roles?.data),
      };
      return data;
    },
    {
      refetchOnWindowFocus: false,
    },
  );
  const getDefaultRole = (role: string) => {
    return (
      response?.data?.data?.find(
        (_element) => _element?.value === role?.toLowerCase(),
      )?.value || "member"
    );
  };
  return { ...response, getDefaultRole };
};
 
const ParseData = (data: any[]): ParsedRole[] =>
  data?.map((item) => ({
    ID: item?.ID,
    label: item?.DisplayName,
    value: item?.Name,
  })) || [];
 
export interface ParsedRole extends SelectOptionProps {
  label: string;
  value: MemberRoleName;
  ID: number | string;
}
 
interface RolesData {
  total: number;
  data: ParsedRole[];
}