import { AuthedFetchType } from "../../api/fake-fetch"; import { MISSING_ACCOUNT_ID_ERROR_MESSAGE, MISSING_WALLET_ID_ERROR_MESSAGE, MISSING_WALLET_TRANSACTION_ID_ERROR_MESSAGE } from "../../constants"; import { errorMessage } from "../../utils"; import { defaultWalletTransactionsFilter, Wallet, Wallets, WalletTransaction } from "./models"; export default function wallets(authedFetch: AuthedFetchType): Wallets { return { get: ({ accountID, walletID }): Promise => { if (!accountID) { return errorMessage(MISSING_ACCOUNT_ID_ERROR_MESSAGE); } if (!walletID) { return errorMessage(MISSING_WALLET_ID_ERROR_MESSAGE); } return authedFetch(`/accounts/${accountID}/wallets/${walletID}`); }, list: ({ accountID }): Promise => { if (!accountID) { return errorMessage(MISSING_ACCOUNT_ID_ERROR_MESSAGE); } return authedFetch(`/accounts/${accountID}/wallets`); }, getTransaction: ({ accountID, walletID, transactionID }): Promise => { if (!accountID) { return errorMessage(MISSING_ACCOUNT_ID_ERROR_MESSAGE); } if (!walletID) { return errorMessage(MISSING_WALLET_ID_ERROR_MESSAGE); } if (!transactionID) { return errorMessage(MISSING_WALLET_TRANSACTION_ID_ERROR_MESSAGE); } return authedFetch(`/accounts/${accountID}/wallets/${walletID}/transactions/${transactionID}`); }, listTransactions: ({ accountID, walletID, filter }): Promise => { if (!accountID) { return errorMessage(MISSING_ACCOUNT_ID_ERROR_MESSAGE); } if (!walletID) { return errorMessage(MISSING_WALLET_ID_ERROR_MESSAGE); } filter = filter || defaultWalletTransactionsFilter; const params = new URLSearchParams(filter as any); return authedFetch( `/accounts/${accountID}/wallets/${walletID}/transactions?${params.toString()}` ); } }; }