Skip to content

Commit

Permalink
chore(core): remove redundant 'addWalletIfNonexistent' function
Browse files Browse the repository at this point in the history
  • Loading branch information
vindard committed Jan 12, 2024
1 parent da5ef4d commit 7b0c35d
Show file tree
Hide file tree
Showing 5 changed files with 31 additions and 81 deletions.
25 changes: 0 additions & 25 deletions core/api/src/app/accounts/add-wallet.ts

This file was deleted.

1 change: 0 additions & 1 deletion core/api/src/app/accounts/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,6 @@ import { AccountsRepository, WalletsRepository } from "@/services/mongoose"

export * from "./account-limit"
export * from "./add-new-contact"
export * from "./add-wallet"
export * from "./create-account"
export * from "./get-account-transactions-for-contact"
export * from "./get-contact-by-username"
Expand Down
29 changes: 26 additions & 3 deletions core/api/src/debug/create-usd-wallets.ts
Original file line number Diff line number Diff line change
Expand Up @@ -3,13 +3,36 @@
* yarn ts-node --files -r tsconfig-paths/register src/debug/create-usd-wallets.ts
*/

import { Accounts as AccountsWithSpans } from "@/app"
import { WalletCurrency } from "@/domain/shared"
import { WalletType } from "@/domain/wallets"
import { isUp } from "@/services/lnd/health"
import { lndsConnect } from "@/services/lnd/auth"
import { setupMongoConnection } from "@/services/mongodb"
import { AccountsRepository } from "@/services/mongoose"
import { AccountsRepository, WalletsRepository } from "@/services/mongoose"

const addWalletIfNonexistent = async ({
accountId,
type,
currency,
}: {
accountId: AccountId
type: WalletType
currency: WalletCurrency
}): Promise<Wallet | ApplicationError> => {
const wallets = await WalletsRepository().listByAccountId(accountId)
if (wallets instanceof Error) return wallets

const walletOfTypeAndCurrency = wallets.find(
(wallet) => wallet.currency === currency && wallet.type === type,
)
if (walletOfTypeAndCurrency) return walletOfTypeAndCurrency

return WalletsRepository().persistNew({
accountId,
type,
currency,
})
}

const createUsdWallets = async () => {
await setupMongoConnection()
Expand All @@ -18,7 +41,7 @@ const createUsdWallets = async () => {
if (accounts instanceof Error) return accounts
let progress = 0
for await (const account of accounts) {
await AccountsWithSpans.addWalletIfNonexistent({
await addWalletIfNonexistent({
accountId: account.id,
type: WalletType.Checking,
currency: WalletCurrency.Usd,
Expand Down
29 changes: 5 additions & 24 deletions core/api/test/helpers/user.ts
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,6 @@ import { lndOutside1, safePay } from "./lightning"
import { randomPhone, randomUserId } from "."

import { createAccountWithPhoneIdentifier } from "@/app/accounts"
import { addWalletIfNonexistent } from "@/app/accounts/add-wallet"
import { getAdminAccounts, getDefaultAccountsConfig } from "@/config"

import { CouldNotFindAccountFromKratosIdError, CouldNotFindError } from "@/domain/errors"
Expand Down Expand Up @@ -155,12 +154,6 @@ export const createUserAndWalletFromPhone = async (
const accountIP = await AccountsIpsRepository().update(accountIp)
if (!(accountIP instanceof CouldNotFindError) && accountIP instanceof Error)
throw accountIP

await addWalletIfNonexistent({
currency: WalletCurrency.Usd,
accountId: account.id,
type: WalletType.Checking,
})
}

if (account instanceof Error) throw account
Expand Down Expand Up @@ -190,20 +183,14 @@ export const createRandomUserAndWallets = async (): Promise<{
const phone = randomPhone()
const btcWalletDescriptor = await createUserAndWallet(phone)

const usdWallet = await addWalletIfNonexistent({
currency: WalletCurrency.Usd,
accountId: btcWalletDescriptor.accountId,
type: WalletType.Checking,
})
if (usdWallet instanceof Error) throw usdWallet
const accountWallets = await WalletsRepository().findAccountWalletsByAccountId(
btcWalletDescriptor.accountId,
)
if (accountWallets instanceof Error) throw accountWallets

return {
btcWalletDescriptor,
usdWalletDescriptor: {
id: usdWallet.id,
currency: WalletCurrency.Usd,
accountId: usdWallet.accountId,
},
usdWalletDescriptor: accountWallets.USD,
}
}

Expand Down Expand Up @@ -256,12 +243,6 @@ export const createUserAndWallet = async (
const accountIP = await AccountsIpsRepository().update(accountIp)
if (!(accountIP instanceof CouldNotFindError) && accountIP instanceof Error)
throw accountIP

await addWalletIfNonexistent({
currency: WalletCurrency.Usd,
accountId: account.id,
type: WalletType.Checking,
})
}

if (account instanceof Error) throw account
Expand Down

This file was deleted.

0 comments on commit 7b0c35d

Please sign in to comment.