Skip to content

Commit

Permalink
chore: address pr feedback
Browse files Browse the repository at this point in the history
  • Loading branch information
UncleSamtoshi committed Nov 7, 2023
1 parent af4d06d commit c57fb82
Show file tree
Hide file tree
Showing 3 changed files with 45 additions and 25 deletions.
Original file line number Diff line number Diff line change
@@ -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<WalletOnChainSettledTransaction[] | ApplicationError> => {
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 })
}
26 changes: 25 additions & 1 deletion core/api/src/graphql/public/types/object/business-account.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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"
Expand Down Expand Up @@ -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,
Expand Down
11 changes: 2 additions & 9 deletions core/api/src/graphql/public/types/object/consumer-account.ts
Original file line number Diff line number Diff line change
Expand Up @@ -216,21 +216,14 @@ const ConsumerAccount = GT.Object<Account, GraphQLPublicContextAuth>({
},
},
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)
}
Expand Down

0 comments on commit c57fb82

Please sign in to comment.