All files / src/services/api business-owners.ts

68.75% Statements 11/16
25% Branches 1/4
50% Functions 3/6
68.75% Lines 11/16

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              24x 19x                         24x 184x   184x     19x   19x 19x 19x                                           24x                           24x                            
import { UseQueryOptions, useQuery } from "react-query";
import { customInstance } from "./index";
import { buildMerchantEndpoints } from "./utils.api";
import { useAppDispatch } from "@redux/hooks";
import { updatePermissions } from "@redux/slices/app";
import { QKEY_LIST_BUSINESS_OWNERS } from "@constants/queryKeys";
 
export const getBusinessOwners = (id: number) => {
  return customInstance({
    url: buildMerchantEndpoints(`legal-entities/${id}/principals?sort=id`),
    method: "GET",
  });
};
 
type Options = Omit<
  UseQueryOptions<any, any, any, any>,
  "queryKey" | "queryFn"
> & {
  parser?: (response: any) => any;
};
 
export const useGetBusinessOwners = (id: number, options?: Options) => {
  const dispatch = useAppDispatch();
 
  return useQuery(
    [QKEY_LIST_BUSINESS_OWNERS, id],
    async () => {
      const data = await getBusinessOwners(id);
 
      Eif (options?.parser) {
        const parsedData = options.parser(data);
        return parsedData;
      }
 
      return data;
    },
    {
      ...options,
      enabled: !!id,
      refetchOnWindowFocus: false,
      onError(err: any) {
        if (err.not_authorized) {
          dispatch(
            updatePermissions({
              [QKEY_LIST_BUSINESS_OWNERS]: true,
            }),
          );
        }
      },
    },
  );
};
 
export const createBusinessOwner = (
  legalEntityID: number,
  data: any,
  merchantId?: number,
) =>
  customInstance({
    url: buildMerchantEndpoints(
      `legal-entities/${legalEntityID}/principals`,
      merchantId,
    ),
    method: "POST",
    data,
  });
 
export const patchBusinessOwner = (
  legalEntityID: number,
  ownerID: number | string,
  data: any,
  merchantId?: number,
) =>
  customInstance({
    url: buildMerchantEndpoints(
      `legal-entities/${legalEntityID}/principals/${ownerID}`,
      merchantId,
    ),
    method: "PATCH",
    data,
  });