From c57fb82bb21301a11b74f64fc3127f1ce64b1699 Mon Sep 17 00:00:00 2001 From: Sam Peters Date: Mon, 6 Nov 2023 15:22:15 -0600 Subject: [PATCH] chore: address pr feedback --- ...ending-onchain-transactions-for-account.ts | 33 ++++++++++--------- .../public/types/object/business-account.ts | 26 ++++++++++++++- .../public/types/object/consumer-account.ts | 11 ++----- 3 files changed, 45 insertions(+), 25 deletions(-) diff --git a/core/api/src/app/accounts/get-pending-onchain-transactions-for-account.ts b/core/api/src/app/accounts/get-pending-onchain-transactions-for-account.ts index 0d846e8e1f..d2e92a9275 100644 --- a/core/api/src/app/accounts/get-pending-onchain-transactions-for-account.ts +++ b/core/api/src/app/accounts/get-pending-onchain-transactions-for-account.ts @@ -1,33 +1,36 @@ import { getPendingOnChainTransactionsForWallets } from "../wallets/get-pending-onchain-transactions-for-wallets" -import { AccountValidator } from "@/domain/accounts" import { RepositoryError } from "@/domain/errors" -import { WalletsRepository } from "@/services/mongoose" import { checkedToWalletId } from "@/domain/wallets" +import { WalletsRepository } from "@/services/mongoose" export const getPendingOnChainTransactionsForAccountByWalletIds = async ({ account, walletIds, }: { account: Account - walletIds: string[] + walletIds?: string[] }): Promise => { const walletsRepo = WalletsRepository() - const wallets: Wallet[] = [] - for (const uncheckedWalletId of walletIds) { - const walletId = checkedToWalletId(uncheckedWalletId) - if (walletId instanceof Error) return walletId - const wallet = await walletsRepo.findById(walletId) - if (wallet instanceof RepositoryError) return wallet + const accountWallets = await walletsRepo.listByAccountId(account.id) + if (accountWallets instanceof RepositoryError) return accountWallets - const accountValidator = AccountValidator(account) - if (accountValidator instanceof Error) return accountValidator - const validateWallet = accountValidator.validateWalletForAccount(wallet) - if (validateWallet instanceof Error) return validateWallet + if (!walletIds) { + return getPendingOnChainTransactionsForWallets({ wallets: accountWallets }) + } - wallets.push(wallet) + const checkedWalletIds: WalletId[] = [] + + for (const walletId of walletIds) { + const checkedWalletId = checkedToWalletId(walletId) + if (checkedWalletId instanceof Error) return checkedWalletId + checkedWalletIds.push(checkedWalletId) } - return getPendingOnChainTransactionsForWallets({ wallets }) + const selectedWallets = accountWallets.filter((wallet) => + checkedWalletIds.includes(wallet.id), + ) + + return getPendingOnChainTransactionsForWallets({ wallets: selectedWallets }) } diff --git a/core/api/src/graphql/public/types/object/business-account.ts b/core/api/src/graphql/public/types/object/business-account.ts index d323a29900..d76de35165 100644 --- a/core/api/src/graphql/public/types/object/business-account.ts +++ b/core/api/src/graphql/public/types/object/business-account.ts @@ -6,7 +6,9 @@ import DisplayCurrency from "../../../shared/types/scalar/display-currency" import AccountLevel from "../../../shared/types/scalar/account-level" -import { TransactionConnection } from "../../../shared/types/object/transaction" +import Transaction, { + TransactionConnection, +} from "../../../shared/types/object/transaction" import RealtimePrice from "./realtime-price" import { NotificationSettings } from "./notification-settings" @@ -158,6 +160,28 @@ const BusinessAccount = GT.Object({ ) }, }, + pendingTransactions: { + type: GT.NonNullList(Transaction), + args: { + walletIds: { + type: GT.List(WalletId), + }, + }, + resolve: async (source, args) => { + const { walletIds } = args + + const transactions = + await Accounts.getPendingOnChainTransactionsForAccountByWalletIds({ + account: source, + walletIds, + }) + + if (transactions instanceof Error) { + throw mapError(transactions) + } + return transactions + }, + }, notificationSettings: { type: GT.NonNull(NotificationSettings), resolve: (source) => source.notificationSettings, diff --git a/core/api/src/graphql/public/types/object/consumer-account.ts b/core/api/src/graphql/public/types/object/consumer-account.ts index 88b3d801b0..aecdc6870a 100644 --- a/core/api/src/graphql/public/types/object/consumer-account.ts +++ b/core/api/src/graphql/public/types/object/consumer-account.ts @@ -216,21 +216,14 @@ const ConsumerAccount = GT.Object({ }, }, resolve: async (source, args) => { - let { walletIds } = args - - if (!walletIds) { - const wallets = await WalletsRepository().listByAccountId(source.id) - if (wallets instanceof Error) { - throw mapError(wallets) - } - walletIds = wallets.map((wallet) => wallet.id) - } + const { walletIds } = args const transactions = await Accounts.getPendingOnChainTransactionsForAccountByWalletIds({ account: source, walletIds, }) + if (transactions instanceof Error) { throw mapError(transactions) }