All files / src/services/api transactions.ts

70% Statements 14/20
0% Branches 0/2
66.66% Functions 4/6
70% Lines 14/20

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                              539x         3x           3x         3x                 539x                                   539x     2x         2x               539x       3x         3x               539x       3x                         539x                            
import { customInstance } from "./index";
import type {
  BackgroundTask,
  TransactionExportTask,
  ReconciliationExportTask,
} from "@customTypes/backgroundTasks";
import { addFiltersAndSortToUrl, buildMerchantEndpoints } from "./utils.api";
 
/**
 * Initiates an asynchronous transaction export
 * @param merchantId - The merchant ID
 * @param filterParams - Optional filter parameters
 * @param sorting - Optional sorting parameters
 * @returns Background task with pending status
 */
export const initiateTransactionExport = async (
  merchantId: number,
  filterParams?: string,
  sorting?: string,
): Promise<TransactionExportTask> => {
  const URL = addFiltersAndSortToUrl(
    buildMerchantEndpoints("transactions.csv"),
    filterParams,
    sorting,
  );
 
  const response = await customInstance({
    url: `${URL}&async=true`,
    method: "GET",
  });
 
  return response;
};
 
/**
 * Initiates an asynchronous reconciliation export.
 * The endpoint is always async — no async=true parameter needed.
 * Returns 202 immediately; poll getBackgroundTaskStatus until completed.
 * @param urlFilters - Optional date range query string ("" for all-time, "?start_date=X&end_date=Y" for range)
 */
export const initiateReconciliationExport = async (
  urlFilters: string,
): Promise<ReconciliationExportTask> => {
  const response = await customInstance({
    url: `/submerchants-financial-summary.csv${urlFilters}`,
    method: "GET",
  });
 
  return response;
};
 
/**
 * Initiates an asynchronous bank-reconciliation ("Standard Template") export.
 * The endpoint is always async — returns 202 immediately; poll
 * getBackgroundTaskStatus until completed, then download the presigned S3 URL.
 * @param urlFilters - Optional date range query string ("" for all-time,
 *   "?start_date=X&end_date=Y" for a range)
 */
export const initiateBankReconciliationExport = async (
  urlFilters: string,
): Promise<ReconciliationExportTask> => {
  const response = await customInstance({
    url: `/submerchants-bank-reconciliation.csv${urlFilters}`,
    method: "GET",
  });
 
  return response;
};
 
/**
 * Gets the status of a background task
 * @param taskId - The task ID
 * @returns Background task with current status
 */
export const getBackgroundTaskStatus = async <TResult = any>(
  accountId: number,
  taskId: number,
): Promise<BackgroundTask<TResult>> => {
  const response = await customInstance({
    url: `/accounts/${accountId}/background-tasks/${taskId}`,
    method: "GET",
  });
 
  return response;
};
 
/**
 * Marks one or more background tasks as read
 * @param accountId - The account ID
 * @param taskIds - Array of task IDs to mark as read
 */
export const markBackgroundTasksAsRead = async (
  accountId: number,
  taskIds: number[],
): Promise<void> => {
  await customInstance({
    url: `/accounts/${accountId}/background-tasks/read`,
    method: "PUT",
    data: { taskIds },
  });
};
 
/**
 * Gets all background tasks for an account with optional filtering
 * @param accountId - The account ID
 * @param filter - Optional filter string
 * @returns Array of background tasks
 */
export const getBackgroundTasks = async <TResult = any>(
  accountId: number,
  filter?: string,
): Promise<BackgroundTask<TResult>[]> => {
  const url = `/accounts/${accountId}/background-tasks`;
  const fullUrl = filter ? `${url}?filter=${filter}` : url;
 
  const response = await customInstance({
    url: fullUrl,
    method: "GET",
  });
 
  return response;
};