diff --git a/core/api/src/app/accounts/add-wallet.ts b/core/api/src/app/accounts/add-wallet.ts deleted file mode 100644 index c5bed73718..0000000000 --- a/core/api/src/app/accounts/add-wallet.ts +++ /dev/null @@ -1,44 +0,0 @@ -import { WalletsRepository } from "@/services/mongoose" - -export const addWallet = async ({ - accountId, - type, - currency, -}: { - accountId: AccountId - type: WalletType - currency: WalletCurrency -}): Promise => { - const wallet = await WalletsRepository().persistNew({ - accountId, - type, - currency, - }) - if (wallet instanceof Error) return wallet - - return wallet -} - -export const addWalletIfNonexistent = async ({ - accountId, - type, - currency, -}: { - accountId: AccountId - type: WalletType - currency: WalletCurrency -}): Promise => { - 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, - }) -} diff --git a/core/api/src/app/accounts/index.ts b/core/api/src/app/accounts/index.ts index f22232b2f1..073971d171 100644 --- a/core/api/src/app/accounts/index.ts +++ b/core/api/src/app/accounts/index.ts @@ -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" diff --git a/core/api/src/debug/create-usd-wallets.ts b/core/api/src/debug/create-usd-wallets.ts deleted file mode 100644 index ec9f8e3263..0000000000 --- a/core/api/src/debug/create-usd-wallets.ts +++ /dev/null @@ -1,45 +0,0 @@ -/** - * how to run: - * 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" - -const createUsdWallets = async () => { - await setupMongoConnection() - - const accounts = AccountsRepository().listLockedAccounts() - if (accounts instanceof Error) return accounts - let progress = 0 - for await (const account of accounts) { - await AccountsWithSpans.addWalletIfNonexistent({ - accountId: account.id, - type: WalletType.Checking, - currency: WalletCurrency.Usd, - }) - progress++ - if (progress % 1000 === 0) { - console.log(`${progress} accounts iterated`) - } - } - - console.log("completed") -} - -const main = async () => { - return createUsdWallets() -} - -setupMongoConnection() - .then(async (mongoose) => { - await Promise.all(lndsConnect.map((lndParams) => isUp(lndParams))) - await main() - return mongoose.connection.close() - }) - .catch((err) => console.log(err)) diff --git a/core/api/test/helpers/user.ts b/core/api/test/helpers/user.ts index eb24c40b0c..7881cae4e7 100644 --- a/core/api/test/helpers/user.ts +++ b/core/api/test/helpers/user.ts @@ -2,8 +2,7 @@ import { lndOutside1, safePay } from "./lightning" import { randomPhone, randomUserId } from "." -import { addWallet, createAccountWithPhoneIdentifier } from "@/app/accounts" -import { addWalletIfNonexistent } from "@/app/accounts/add-wallet" +import { createAccountWithPhoneIdentifier } from "@/app/accounts" import { getAdminAccounts, getDefaultAccountsConfig } from "@/config" import { CouldNotFindAccountFromKratosIdError, CouldNotFindError } from "@/domain/errors" @@ -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 @@ -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, } } @@ -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 @@ -286,11 +267,10 @@ export const addNewWallet = async ({ accountId: AccountId currency: WalletCurrency }): Promise => { - // Create wallet for account (phone number) - const wallet = await addWallet({ - currency, + const wallet = await WalletsRepository().persistNew({ accountId, type: WalletType.Checking, + currency, }) if (wallet instanceof Error) throw wallet diff --git a/core/api/test/integration/services/wallets-repository.spec.ts b/core/api/test/integration/services/wallets-repository.spec.ts index e3ab087323..64e2a35f48 100644 --- a/core/api/test/integration/services/wallets-repository.spec.ts +++ b/core/api/test/integration/services/wallets-repository.spec.ts @@ -1,6 +1,9 @@ import { randomUUID } from "crypto" +import mongoose from "mongoose" + import { + CouldNotFindAccountFromIdError, CouldNotFindWalletFromAccountIdAndCurrencyError, MultipleWalletsFoundForAccountIdAndCurrency, RepositoryError, @@ -8,6 +11,7 @@ import { import { WalletCurrency } from "@/domain/shared" import { WalletsRepository } from "@/services/mongoose" import { Wallet } from "@/services/mongoose/schema" +import { WalletType } from "@/domain/wallets" const wallets = WalletsRepository() const accountId = randomUUID() as AccountId @@ -82,4 +86,17 @@ describe("WalletsRepository", () => { expect((accountWallets as RepositoryError).message).toBe(WalletCurrency.Usd) }) }) + + describe("persistNew", () => { + it("fail to create a wallet with non-existent account", async () => { + const id = new mongoose.Types.ObjectId() + + const newWallet = await wallets.persistNew({ + accountId: id as unknown as AccountId, + type: WalletType.Checking, + currency: WalletCurrency.Btc, + }) + expect(newWallet).toBeInstanceOf(CouldNotFindAccountFromIdError) + }) + }) }) diff --git a/core/api/test/legacy-integration/app/accounts/multiple-wallet-account.spec.ts b/core/api/test/legacy-integration/app/accounts/multiple-wallet-account.spec.ts deleted file mode 100644 index 2aeca519e0..0000000000 --- a/core/api/test/legacy-integration/app/accounts/multiple-wallet-account.spec.ts +++ /dev/null @@ -1,75 +0,0 @@ -import mongoose from "mongoose" - -import { Accounts } from "@/app" -import { getDefaultAccountsConfig } from "@/config" -import { WalletCurrency } from "@/domain/shared" -import { WalletType } from "@/domain/wallets" -import { WalletsRepository } from "@/services/mongoose" - -import { - createUserAndWalletFromPhone, - getAccountByPhone, - getUsdWalletIdByPhone, - randomPhone, - randomUserId, -} from "test/helpers" - -it("change default walletId of account", async () => { - const phone = randomPhone() - - const kratosUserId = randomUserId() - - const account = await Accounts.createAccountWithPhoneIdentifier({ - newAccountInfo: { phone, kratosUserId }, - config: getDefaultAccountsConfig(), - }) - if (account instanceof Error) throw account - - const accountId = account.id - - const newWallet = await WalletsRepository().persistNew({ - accountId, - type: WalletType.Checking, - currency: WalletCurrency.Btc, - }) - if (newWallet instanceof Error) throw newWallet - - const newAccount = await Accounts.updateDefaultWalletId({ - accountId, - walletId: newWallet.id, - }) - - if (newAccount instanceof Error) throw newAccount - - expect(newAccount.defaultWalletId).toBe(newWallet.id) -}) - -it("fail to create an invalid account", async () => { - const id = new mongoose.Types.ObjectId() - - const newWallet = await WalletsRepository().persistNew({ - accountId: id as unknown as AccountId, - type: WalletType.Checking, - currency: WalletCurrency.Btc, - }) - - expect(newWallet).toBeInstanceOf(Error) -}) - -it("does not add a usd wallet if one exists", async () => { - const phone = randomPhone() - - await createUserAndWalletFromPhone(phone) - const account = await getAccountByPhone(phone) - const usdWalletId = await getUsdWalletIdByPhone(phone) - expect(usdWalletId).toBeDefined() - - const newWallet = await Accounts.addWalletIfNonexistent({ - accountId: account.id, - type: WalletType.Checking, - currency: WalletCurrency.Usd, - }) - if (newWallet instanceof Error) throw newWallet - - expect(newWallet.id).toBe(usdWalletId) -})