All files / src/services/api/products manage-money.ts

63.33% Statements 19/30
41.17% Branches 7/17
46.66% Functions 7/15
65.51% Lines 19/29

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                                      28x                 11x                           28x                     211x   211x         211x     11x   11x         11x                   11x                                             28x 15x                         28x 15x                     28x 15x                           28x         15x   15x                                
import { customInstance } from "@services/api";
import {
  useQuery,
  useMutation,
  useQueryClient,
  UseQueryOptions,
} from "react-query";
 
import { ROWS_PER_PAGE } from "@hooks/common/usePagination";
import { buildMerchantEndpoints } from "../utils.api";
import { useAppDispatch } from "@redux/hooks";
import { updatePermissions } from "@redux/slices/app";
import { useGetMerchantById } from "@hooks/enterprise-api/account/useGetMerchants";
import { QKEY_LIST_ALL_BANK_ACCOUNTS } from "@constants/queryKeys";
import {
  BankAccount,
  NewBankAccount,
} from "@hooks/merchant-api/manage-money/useListBankAccounts";
 
export const getBankAccounts = ({
  sortQuery,
  page,
  merchantId,
}: {
  sortQuery?: string;
  page: number;
  merchantId: number;
}) => {
  return customInstance({
    url: `merchants/${merchantId}/bank-accounts?&sort=${
      sortQuery ? sortQuery : "-createdAt"
    }&page=${page}&max=${ROWS_PER_PAGE}`,
    method: "GET",
  });
};
 
export interface BankAccountsResponse {
  data: NewBankAccount[];
  total: number;
  isAllowedAddAccounts?: boolean;
}
 
export const useGetBankAccounts = (
  {
    page,
    sorting,
    merchantId,
  }: { page: number; sorting: string; merchantId: number },
  options: Omit<
    UseQueryOptions<any, any, any, any>,
    "queryKey" | "queryFn"
  > = {},
) => {
  const dispatch = useAppDispatch();
 
  const { data: merchantData, isAllowedAddBankAccounts } = useGetMerchantById({
    merchantId,
    enabled: !!merchantId,
  });
 
  return useQuery<BankAccountsResponse | undefined>(
    [QKEY_LIST_ALL_BANK_ACCOUNTS, sorting, page, merchantId],
    async () => {
      Iif (!merchantId) return;
 
      const data = await getBankAccounts({
        sortQuery: sorting,
        page,
        merchantId,
      });
      Iif (merchantData?.linkedBankAccount) {
        return {
          data: [
            { ...merchantData.linkedBankAccount, isLinked: true },
            ...(data.data ? data.data : []),
          ],
          total: data.total + 1,
          isAllowedAddAccounts: isAllowedAddBankAccounts,
        };
      }
      return {
        data: data.data,
        total: data.total,
        isAllowedAddAccounts: isAllowedAddBankAccounts,
      };
    },
    {
      ...options,
      enabled: Boolean(merchantData?.accID && merchantId) && options?.enabled,
      retry: 2,
      onError(err: any) {
        if (err.not_authorized) {
          dispatch(
            updatePermissions({
              bank_accounts_list: true,
            }),
          );
        }
      },
    },
  );
};
 
export const useRequestPlaidToken = (enabled: boolean) => {
  return useQuery(
    "request_plaid_link_token",
    async () => {
      return customInstance({
        url: buildMerchantEndpoints(`plaid/link-token`),
        method: "POST",
        data: {},
      });
    },
    { retry: false, enabled },
  );
};
 
export const useGenerateNewPlaidToken = () => {
  return useMutation({
    mutationFn: async (expiredAccessToken: string) => {
      return customInstance({
        url: buildMerchantEndpoints(`plaid/link-token`),
        method: "POST",
        data: { expiredAccessToken },
      });
    },
  });
};
 
export const useAccessPlaidLinkToken = () => {
  return useQuery(
    "access_plaid_link_token",
    async () => {
      return customInstance({
        url: buildMerchantEndpoints(`plaid/access-token`),
        method: "POST",
      });
    },
    {
      enabled: false,
    },
  );
};
 
export const useCreateBankAccountWithPlaid = ({
  onError = () => null,
}: {
  onError?: (e: any) => void;
} = {}) => {
  const queryClient = useQueryClient();
 
  return useMutation({
    mutationFn: (data: any) => {
      return customInstance({
        url: buildMerchantEndpoints(`bank-accounts`),
        method: "POST",
        data,
      });
    },
    onError(error) {
      onError(error);
    },
    onSuccess() {
      queryClient.invalidateQueries(QKEY_LIST_ALL_BANK_ACCOUNTS);
    },
  });
};