Skip to content

Commit

Permalink
feat(core): get account default wallet by username method updated to …
Browse files Browse the repository at this point in the history
…support phone number
  • Loading branch information
esaugomez31 committed Feb 20, 2025
1 parent 52b9d5d commit 2eebfed
Show file tree
Hide file tree
Showing 5 changed files with 30 additions and 8 deletions.
22 changes: 22 additions & 0 deletions core/api/src/app/accounts/get-account-for-username-or-phone.ts
Original file line number Diff line number Diff line change
@@ -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<Account> => {
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
}
1 change: 1 addition & 0 deletions core/api/src/app/accounts/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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()

Expand Down
2 changes: 1 addition & 1 deletion core/api/src/domain/users/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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,
Expand Down
Original file line number Diff line number Diff line change
@@ -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),
Expand All @@ -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)
Expand Down
4 changes: 4 additions & 0 deletions core/api/src/graphql/shared/types/scalar/username.ts
Original file line number Diff line number Diff line change
@@ -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",
Expand All @@ -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" })
}

Expand Down

0 comments on commit 2eebfed

Please sign in to comment.