All files / src/services/api manage-team.ts

66.21% Statements 49/74
47.36% Branches 27/57
69.23% Functions 9/13
67.14% Lines 47/70

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 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234                      154x                 7x 7x 7x 7x 7x 7x 7x   7x   7x           1x   1x 1x                               154x               1x 1x     1x     1x     1x 1x   1x     1x     1x     1x     1x     1x   1x       1x           154x                                                                                           154x                       1584x 1584x 1584x 1584x                     1x               1x                                             1584x   154x 102x   102x 21x           102x 102x     21x 21x           102x   101x    
import { customInstance } from "@services/api";
import { UseQueryOptions, useQuery } from "react-query";
import { useGetCurrentMerchantId } from "@hooks/common";
import { ProductParams } from "./products/queryFactory";
import { ROWS_PER_PAGE } from "@hooks/common/usePagination";
import { useAppDispatch } from "@redux/hooks";
import { updatePermissions } from "@redux/slices/app";
import { checkPortals } from "@utils/routing";
import { AssigneesType } from "@components/Merchants/MerchantPreview/hooks/useGetTeamMembers";
import { QKEY_LIST_UNDERWRITERS } from "@constants/queryKeys";
 
export const getAllTeamMembers = ({
  id,
  queryString,
  page,
  sorting,
  searchQuery,
  memberStatus,
  loggedInUserAccountId,
}: ProductParams & { id: number }) => {
  let baseURL = `/accounts/${id}/members?sort=${sorting || "-user.accID"}`;
  Iif (searchQuery) baseURL += `&q="${searchQuery}"`;
  Eif (page) baseURL += `&page=${page}&max=${ROWS_PER_PAGE}`;
  const filters = [];
  Eif (memberStatus && memberStatus === "joined")
    filters.push(`memberStatus:"joined"`);
  Iif (loggedInUserAccountId)
    filters.push(`user.accID:!${loggedInUserAccountId}`);
  Eif (filters.length > 0) baseURL += `&filter=${filters.join("%3B")}`;
 
  return customInstance({
    url: baseURL,
    method: "GET",
  });
};
function correctURL(url: string) {
  const parts = url.split("?");
 
  Eif (parts.length <= 2) {
    return url;
  }
 
  return parts[0] + "?" + parts.slice(1).join("&");
}
 
type GetUnderwritingProps = {
  id?: number;
  searchQuery?: string;
  isEnterprisePortal?: boolean;
  MID?: string | number;
  type?: AssigneesType;
  query?: string;
};
 
//TODO: this should be refactored to include return statements, this way we are overwriting previous if's
const getUnderwriters = ({
  id,
  searchQuery,
  isEnterprisePortal,
  MID,
  type,
  query,
}: GetUnderwritingProps) => {
  let baseURL = `/accounts/${id}/underwriters`;
  Iif (isEnterprisePortal || (isEnterprisePortal && type === "sales")) {
    baseURL = `/accounts/${MID}/members?filter=(memberStatus:"joined")&page=1&max=20`;
  }
  Iif (type === "underwriter") {
    baseURL = `/accounts/${MID}/members?filter=(roleName:"uw_associate",roleName:"uw_executive",roleName:"sponsor",roleName:"owner",roleName:"admin")%3B(memberStatus:"joined")`;
  }
  Iif (type === "risk") {
    baseURL = `/accounts/${MID}/members?filter=(roleName:"risk_manager",roleName:"admin",roleName:"owner")%3B(memberStatus:"joined")`;
  }
  Eif (type === "sales" && !isEnterprisePortal) {
    baseURL = `/accounts/${MID}/members?filter=(roleName:"sales_associate",roleName:"sales_executive",roleName:"admin",roleName:"owner")%3B(memberStatus:"joined")`;
  }
  Iif (type === "members") {
    baseURL = `/accounts/${MID}/members?filter=(memberStatus:"joined")`;
  }
  Iif (type === "underwriting_admin") {
    baseURL = `/accounts/${MID}/members?filter=(roleName:"uw_associate",roleName:"uw_executive",roleName:"admin",roleName:"owner")%3B(memberStatus:"joined")`;
  }
  Iif (query && type !== "sales") {
    baseURL = `/accounts/${MID}/${query}`;
  }
  Iif (type === "sponsor") {
    baseURL = `/accounts/${MID}/members?filter=(roleName:"uw_executive",roleName:"sponsor",roleName:"owner",roleName:"admin")%3B(memberStatus:"joined")`;
  }
  Iif (type === "kyb") {
    baseURL = `/accounts/${MID}/members?filter=(roleName:"sales_associate",roleName:"sales_executive",roleName:"uw_associate",roleName:"uw_executive",roleName:"sponsor",roleName:"owner",roleName:"admin")%3B(memberStatus:"joined")`;
  }
  Iif (searchQuery) baseURL += `?q="${searchQuery}"`;
 
  baseURL +=
    (baseURL.includes("?") ? "&" : "?") +
    "sort=user.firstName,user.lastName,user.email";
 
  return customInstance({
    url: correctURL(baseURL),
    method: "GET",
  });
};
 
export const useGetAllTeamMembers = (
  params: ProductParams,
  id: number,
  options: Omit<
    UseQueryOptions<any, any, any, any>,
    "queryKey" | "queryFn"
  > = {},
) => {
  const dispatch = useAppDispatch();
 
  const { data, error, isLoading, isFetched, isFetching } = useQuery(
    [
      "get-all-team-members",
      id,
      params.sorting,
      params.searchQuery,
      params.page,
    ],
    async () => {
      const data = await getAllTeamMembers({ id, ...params });
      return data;
    },
    {
      refetchOnWindowFocus: false,
      refetchOnMount: false,
      staleTime: 65000,
      retry: 2,
      ...options,
      onError(err) {
        if (err.not_authorized) {
          dispatch(
            updatePermissions({
              "get-all-team-members": true,
            }),
          );
          if (options?.onError) {
            options.onError(err);
          }
        }
      },
    },
  );
 
  return { data, error, isLoading, isFetched, isFetching };
};
 
export const useGetAccUnderwriters = (
  params: {
    searchQuery?: string;
  },
  merchantId?: number,
  options: Omit<
    UseQueryOptions<any, any, any, any>,
    "queryKey" | "queryFn"
  > = {},
  type?: AssigneesType,
  query?: string,
) => {
  const dispatch = useAppDispatch();
  const { isEnterprisePortal } = checkPortals();
  const { merchantId: MID } = useGetCurrentMerchantId();
  const { data, error, isLoading, isFetched, isFetching } = useQuery(
    [
      QKEY_LIST_UNDERWRITERS,
      merchantId,
      params.searchQuery,
      isEnterprisePortal,
      MID,
      type,
      query,
    ],
    async () => {
      const data = await getUnderwriters({
        id: merchantId,
        searchQuery: params?.searchQuery,
        isEnterprisePortal,
        MID,
        type,
        query,
      });
      return data;
    },
    {
      refetchOnWindowFocus: false,
      refetchOnMount: false,
      staleTime: 65000,
      retry: 2,
      ...options,
      onError(err) {
        if (err.not_authorized) {
          dispatch(
            updatePermissions({
              QKEY_LIST_UNDERWRITERS: true,
            }),
          );
          if (options?.onError) {
            options.onError(err);
          }
        }
      },
    },
  );
 
  return { data, error, isLoading, isFetched, isFetching };
};
export const useGetTeamMemberById = (id?: string) => {
  const { merchantId } = useGetCurrentMerchantId();
 
  const getMember = (accID: number, id: string) => {
    return customInstance({
      url: `/accounts/${accID}/members/${id}`,
      method: "GET",
    });
  };
 
  const getTeamMemberById = (id?: string) => {
    return useQuery(
      "get-event-by-id",
      async () => {
        const memberData = await getMember(merchantId, id || "");
        return { memberData };
      },
      { refetchOnWindowFocus: false, enabled: !!id },
    );
  };
 
  const { data: teamMemberData, error, isLoading } = getTeamMemberById(id);
 
  return { teamMemberData, error, isLoading };
};