From 2eebfed8793ea680edaa014b39ce798d0f42daa4 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Esa=C3=BA=20G=C3=B3mez?= Date: Thu, 20 Feb 2025 14:35:54 -0600 Subject: [PATCH] feat(core): get account default wallet by username method updated to support phone number --- .../get-account-for-username-or-phone.ts | 22 +++++++++++++++++++ core/api/src/app/accounts/index.ts | 1 + core/api/src/domain/users/index.ts | 2 +- .../root/query/account-default-wallet.ts | 9 ++------ .../graphql/shared/types/scalar/username.ts | 4 ++++ 5 files changed, 30 insertions(+), 8 deletions(-) create mode 100644 core/api/src/app/accounts/get-account-for-username-or-phone.ts diff --git a/core/api/src/app/accounts/get-account-for-username-or-phone.ts b/core/api/src/app/accounts/get-account-for-username-or-phone.ts new file mode 100644 index 0000000000..6c9eb029c5 --- /dev/null +++ b/core/api/src/app/accounts/get-account-for-username-or-phone.ts @@ -0,0 +1,22 @@ +import { UsernameRegex } from "@/domain/accounts" +import { mapError } from "@/graphql/error-map" +import { AccountsRepository, UsersRepository } from "@/services/mongoose" + +export const getAccountByUsernameOrPhone = async ( + value: Username | PhoneNumber, +): Promise => { + let account: Account | RepositoryError + + if (value.match(UsernameRegex)) { + account = await AccountsRepository().findByUsername(value as Username) + } else { + const user = await UsersRepository().findByPhone(value as PhoneNumber) + if (user instanceof Error) throw mapError(user) + + account = await AccountsRepository().findByUserId(user.id) + } + + if (account instanceof Error) throw mapError(account) + + return account +} diff --git a/core/api/src/app/accounts/index.ts b/core/api/src/app/accounts/index.ts index 77174b4e50..c1ccc5cf60 100644 --- a/core/api/src/app/accounts/index.ts +++ b/core/api/src/app/accounts/index.ts @@ -24,6 +24,7 @@ export * from "./disable-notification-channel" export * from "./get-pending-incoming-on-chain-transactions-for-account" export * from "./get-invoices-for-account" export * from "./get-notification-settings-for-account" +export * from "./get-account-for-username-or-phone" const accounts = AccountsRepository() diff --git a/core/api/src/domain/users/index.ts b/core/api/src/domain/users/index.ts index c90ea6c0db..ed09a21036 100644 --- a/core/api/src/domain/users/index.ts +++ b/core/api/src/domain/users/index.ts @@ -18,7 +18,7 @@ export * from "./phone-metadata-authorizer" // TODO: we could be using https://gitlab.com/catamphetamine/libphonenumber-js#readme // for a more precise "regex" -const PhoneNumberRegex = /^\+\d{7,14}$/i // FIXME {7,14} to be refined +export const PhoneNumberRegex = /^\+\d{7,14}$/i // FIXME {7,14} to be refined export const checkedToPhoneNumber = ( phoneNumber: string, diff --git a/core/api/src/graphql/public/root/query/account-default-wallet.ts b/core/api/src/graphql/public/root/query/account-default-wallet.ts index fbf582c3d1..8734a3b446 100644 --- a/core/api/src/graphql/public/root/query/account-default-wallet.ts +++ b/core/api/src/graphql/public/root/query/account-default-wallet.ts @@ -1,11 +1,10 @@ -import { Wallets } from "@/app" +import { Wallets, Accounts } from "@/app" import { CouldNotFindWalletFromUsernameAndCurrencyError } from "@/domain/errors" import { mapError } from "@/graphql/error-map" import { GT } from "@/graphql/index" import Username from "@/graphql/shared/types/scalar/username" import WalletCurrency from "@/graphql/shared/types/scalar/wallet-currency" import PublicWallet from "@/graphql/public/types/object/public-wallet" -import { AccountsRepository } from "@/services/mongoose" const AccountDefaultWalletQuery = GT.Field({ type: GT.NonNull(PublicWallet), @@ -22,11 +21,7 @@ const AccountDefaultWalletQuery = GT.Field({ throw username } - const account = await AccountsRepository().findByUsername(username) - if (account instanceof Error) { - throw mapError(account) - } - + const account = await Accounts.getAccountByUsernameOrPhone(username) const wallets = await Wallets.listWalletsByAccountId(account.id) if (wallets instanceof Error) { throw mapError(wallets) diff --git a/core/api/src/graphql/shared/types/scalar/username.ts b/core/api/src/graphql/shared/types/scalar/username.ts index c0878dc521..1d90e24b34 100644 --- a/core/api/src/graphql/shared/types/scalar/username.ts +++ b/core/api/src/graphql/shared/types/scalar/username.ts @@ -1,6 +1,7 @@ import { UsernameRegex } from "@/domain/accounts" import { InputValidationError } from "@/graphql/error" import { GT } from "@/graphql/index" +import { PhoneNumberRegex } from "@/domain/users" const Username = GT.Scalar({ name: "Username", @@ -23,6 +24,9 @@ function validUsernameValue(value: string) { if (value.match(UsernameRegex)) { return value.toLowerCase() } + if (value.match(PhoneNumberRegex)) { + return value + } return new InputValidationError({ message: "Invalid value for Username" }) }