All files / src/services/api/products transactions.ts

55.17% Statements 32/58
42.85% Branches 6/14
32.14% Functions 9/28
55.17% Lines 32/58

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                          68x                       68x               68x               68x               68x           68x 94x             43x           68x                         68x                                       68x         529x 529x 529x   529x     47x 43x             5x 3x 3x   2x                       68x           68x           68x                                                               68x 3x 1x           3x 1x       68x 20x             68x                
import { UseQueryOptions, useMutation, useQuery } from "react-query";
import { customInstance } from "..";
import { ProductParams, useGetTransactionsFactory } from "./queryFactory";
import { useGetCurrentMerchantId } from "@hooks/common";
import { RecurringItem } from "@customTypes/recurring.items.types";
import { buildMerchantEndpoints } from "../utils.api";
import { useAppDispatch } from "@redux/hooks";
import { updatePermissions } from "@redux/slices/app";
import { baseGetMerchantStats } from "../merchants";
import { DEFAULT_QUERY_CONFIG } from "@components/VirtualList/hooks/queries";
import { QKEY_GET_TRANSACTION_STATS } from "@constants/queryKeys";
import { StatsType } from "@services/api/api.constant";
 
const modifyRecurring = (
  merchId: string | number,
  orderRecurringItemID: string,
  params: any,
) => {
  return customInstance({
    url: `merchants/${merchId}/recurring-items/${orderRecurringItemID}/`,
    method: "PATCH",
    data: params,
  });
};
 
const refund = ({ reason, transactionIDs, transactionItemIDs }: any) => {
  return customInstance({
    url: "/refunds",
    method: "POST",
    data: { reason, transactionIDs, transactionItemIDs },
  });
};
 
const cancelTransfer = ({ transactionId, cancel, customerID }: any) => {
  return customInstance({
    url: buildMerchantEndpoints(`transfers/${transactionId}`, customerID),
    method: "PATCH",
    data: { cancel: cancel },
  });
};
 
export const useModiFyRecurring = (orderRecurringItemID: string) => {
  const { merchantId } = useGetCurrentMerchantId();
 
  return useMutation((data: any) => {
    return modifyRecurring(merchantId, orderRecurringItemID, data);
  });
};
 
export const useGetAllTransactions = (
  queryKey = "get-all-transactions",
  path = "transactions",
) => useGetTransactionsFactory(path, queryKey);
 
export const useGetTransactionsByProduct =
  (id: string, queryKey: string) =>
  (
    { queryString, page, sorting, searchQuery, maxRowsPerPage }: ProductParams,
    options: Omit<
      UseQueryOptions<any, any, any, any>,
      "queryKey" | "queryFn"
    > = {},
  ) =>
    useGetTransactionsFactory(`products/${id}/transaction-items`, queryKey)(
      { queryString, page, sorting, searchQuery, maxRowsPerPage },
      { ...options, refetchOnMount: true },
    );
 
export const useGetTransactionsByMerchant =
  (queryKey: string) =>
  (
    { queryString, page, sorting, searchQuery }: ProductParams,
    options: Omit<
      UseQueryOptions<any, any, any, any>,
      "queryKey" | "queryFn"
    > = {},
  ) =>
    useGetTransactionsFactory(`transactions`, queryKey)(
      { queryString, page, sorting, searchQuery },
      options,
    );
 
export const useGetSingleTransaction = (
  thxId: string,
  options: Omit<
    UseQueryOptions<any, any, any, any>,
    "queryKey" | "queryFn"
  > = {},
) => {
  return useQuery(
    ["get-single-transaction", thxId],
    async () => {
      const data = await customInstance({
        url: buildMerchantEndpoints(`transactions/${thxId}`),
        method: "GET",
      });
      return data;
    },
    options,
  );
};
 
export const useGetStats = (
  merchId?: any,
  type: StatsType = StatsType.EXTENDED,
  onError?: (e: any) => void,
) => {
  const { merchantId } = useGetCurrentMerchantId();
  const dispatch = useAppDispatch();
  const id = merchId ?? merchantId;
 
  return useQuery(
    [QKEY_GET_TRANSACTION_STATS, id, type],
    async () => {
      const data = await baseGetMerchantStats(id, type);
      return data;
    },
    {
      refetchOnWindowFocus: false,
      retry: false,
      ...DEFAULT_QUERY_CONFIG,
      onError(err: any) {
        if (onError) {
          onError(err);
          return;
        }
        Iif (err.not_authorized) {
          dispatch(
            updatePermissions({
              manage_money_stats: true,
            }),
          );
        }
      },
    },
  );
};
 
export const useRefund = () => {
  return useMutation((data: any) => {
    return refund(data);
  });
};
 
export const useCancelTransfer = () => {
  return useMutation((data: any) => {
    return cancelTransfer(data);
  });
};
 
export const useFindRecurringItem = (itemId: string, merchantId?: number) => {
  const dispatch = useAppDispatch();
 
  const getRecurringItem = (id: string) => {
    return customInstance({
      url: buildMerchantEndpoints(`recurring-items/${id}`, merchantId),
      method: "GET",
    });
  };
 
  return useQuery<RecurringItem>(
    ["get-recurring-item", itemId],
    async () => {
      const data = await getRecurringItem(itemId);
      return data;
    },
    {
      retry: 1,
      refetchOnWindowFocus: false,
      onError(err: any) {
        if (err.not_authorized) {
          dispatch(
            updatePermissions({
              modify_recurring_view: true,
            }),
          );
        }
      },
    },
  );
};
 
export const useUpdateRecurring = (itemId: string, merchant_id: number) => {
  const updateRecurrence = (id: string, data: any) => {
    return customInstance({
      url: `/merchants/${merchant_id}/recurring-items/${id}`,
      method: "PATCH",
      data,
    });
  };
  return useMutation((data: any) => {
    return updateRecurrence(itemId, data);
  });
};
 
export const getLastBlockedTransaction = () =>
  customInstance({
    url: buildMerchantEndpoints(
      `transactions?sort=-createdAt&page=1&max=1&filter=isBlocked:true%3BfirstCheckedAt:null%3BisFalsePositive:false`,
    ),
    method: "GET",
  });
 
export const setCheckedTransaction = async (
  merchantID: number,
  transactionID: string,
) =>
  customInstance({
    url: `merchants/${merchantID}/transactions/${transactionID}/read`,
    method: "PUT",
  });