import { AuthedFetchType } from "../../api/fake-fetch"; import { BankAccounts } from "../models"; import { errorMessage } from "../../utils"; import { MISSING_ACCOUNT_ID_ERROR_MESSAGE, MISSING_AMOUNTS_ERROR_MESSAGE, MISSING_BANK_ACCOUNT_ERROR_MESSAGE, MISSING_BANK_ACCOUNT_HOLDER_NAME_ERROR_MESSAGE, MISSING_BANK_ACCOUNT_HOLDER_TYPE_ERROR_MESSAGE, MISSING_BANK_ACCOUNT_ID_ERROR_MESSAGE, MISSING_BANK_ACCOUNT_NUMBER_ERROR_MESSAGE, MISSING_BANK_ACCOUNT_ROUTING_NUMBER_ERROR_MESSAGE, MISSING_BANK_ACCOUNT_ROUTING_NUMBER_LENGTH_ERROR_MESSAGE, MISSING_BANK_ACCOUNT_TYPE_ERROR_MESSAGE, MISSING_BANK_PAYLOAD } from "../../constants"; export default function bankAccounts(authedFetch: AuthedFetchType): BankAccounts { return { link: ({ accountID, bankAccount, plaidToken, mxAuthorizationCode }) => { let payload = {}; if (!accountID) { return errorMessage(MISSING_ACCOUNT_ID_ERROR_MESSAGE); } if (!bankAccount && !plaidToken && !mxAuthorizationCode) { return errorMessage(MISSING_BANK_PAYLOAD); } if (bankAccount) { if (!bankAccount.accountNumber) { return errorMessage(MISSING_BANK_ACCOUNT_NUMBER_ERROR_MESSAGE); } if (!bankAccount.routingNumber) { return errorMessage(MISSING_BANK_ACCOUNT_ROUTING_NUMBER_ERROR_MESSAGE); } if (bankAccount.routingNumber.length !== 9) { return errorMessage(MISSING_BANK_ACCOUNT_ROUTING_NUMBER_LENGTH_ERROR_MESSAGE); } if (!bankAccount.holderName) { return errorMessage(MISSING_BANK_ACCOUNT_HOLDER_NAME_ERROR_MESSAGE); } if (!bankAccount.holderType) { return errorMessage(MISSING_BANK_ACCOUNT_HOLDER_TYPE_ERROR_MESSAGE); } if ( !bankAccount.bankAccountType || !["checking", "savings"].includes(bankAccount.bankAccountType) ) { return errorMessage(MISSING_BANK_ACCOUNT_TYPE_ERROR_MESSAGE); } payload = { account: bankAccount }; } else if (plaidToken) { payload = { plaid: { token: plaidToken } }; } else if (mxAuthorizationCode) { payload = { mx: { authorizationCode: mxAuthorizationCode } }; } return authedFetch(`/accounts/${accountID}/bank-accounts`, { method: "POST", body: payload }); }, list: ({ accountID }) => { if (!accountID) { return errorMessage(MISSING_ACCOUNT_ID_ERROR_MESSAGE); } return authedFetch(`/accounts/${accountID}/bank-accounts`, { method: "GET" }); }, get: ({ accountID, bankAccountID }) => { if (!accountID) { return errorMessage(MISSING_ACCOUNT_ID_ERROR_MESSAGE); } if (!bankAccountID) { return errorMessage(MISSING_BANK_ACCOUNT_ID_ERROR_MESSAGE); } return authedFetch(`/accounts/${accountID}/bank-accounts/${bankAccountID}`, { method: "GET" }); }, delete: ({ accountID, bankAccountID }) => { if (!accountID) { return errorMessage(MISSING_ACCOUNT_ID_ERROR_MESSAGE); } if (!bankAccountID) { return errorMessage(MISSING_BANK_ACCOUNT_ID_ERROR_MESSAGE); } return authedFetch(`/accounts/${accountID}/bank-accounts/${bankAccountID}`, { method: "DELETE" }); }, startMicroDepositVerification: ({ accountID, bankAccountID }) => { if (!accountID) { return errorMessage(MISSING_ACCOUNT_ID_ERROR_MESSAGE); } if (!bankAccountID) { return errorMessage(MISSING_BANK_ACCOUNT_ID_ERROR_MESSAGE); } return authedFetch(`/accounts/${accountID}/bank-accounts/${bankAccountID}/micro-deposits`, { method: "POST" }); }, completeMicroDepositVerification: ({ accountID, bankAccountID, amounts }) => { if (!accountID) { return errorMessage(MISSING_ACCOUNT_ID_ERROR_MESSAGE); } if (!bankAccountID) { return errorMessage(MISSING_BANK_ACCOUNT_ID_ERROR_MESSAGE); } if (!amounts) { return errorMessage(MISSING_AMOUNTS_ERROR_MESSAGE); } return authedFetch(`/accounts/${accountID}/bank-accounts/${bankAccountID}/micro-deposits`, { method: "PUT", body: { amounts } }); } }; }