Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

feat(core): get account default wallet by username method updated to … #4750

Open
wants to merge 1 commit into
base: main
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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