-
Notifications
You must be signed in to change notification settings - Fork 143
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
feat(core): add new 'AccountDefaultWallet' with UsernameWithFlags type
- Loading branch information
Showing
10 changed files
with
188 additions
and
33 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,12 @@ | ||
query accountDefaultWalletByUsername( | ||
$username: UsernameWithFlags! | ||
$walletCurrency: WalletCurrency | ||
) { | ||
accountDefaultWalletByUsername( | ||
username: $username | ||
walletCurrency: $walletCurrency | ||
) { | ||
id | ||
currency | ||
} | ||
} |
This file was deleted.
Oops, something went wrong.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
72 changes: 72 additions & 0 deletions
72
core/api/src/graphql/public/root/query/account-default-wallet-by-username.ts
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,72 @@ | ||
import { Wallets } from "@/app" | ||
|
||
import { UsernameParser } from "@/domain/accounts" | ||
import { WalletCurrency as DomainWalletCurrency } from "@/domain/shared" | ||
import { CouldNotFindWalletFromUsernameAndCurrencyError } from "@/domain/errors" | ||
|
||
import { AccountsRepository } from "@/services/mongoose" | ||
|
||
import { mapError } from "@/graphql/error-map" | ||
import { GT } from "@/graphql/index" | ||
import UsernameWithFlags from "@/graphql/shared/types/scalar/username-with-flags" | ||
import WalletCurrency from "@/graphql/shared/types/scalar/wallet-currency" | ||
import PublicWallet from "@/graphql/public/types/object/public-wallet" | ||
|
||
const AccountDefaultWalletByUsernameQuery = GT.Field({ | ||
type: GT.NonNull(PublicWallet), | ||
args: { | ||
username: { | ||
type: GT.NonNull(UsernameWithFlags), | ||
}, | ||
walletCurrency: { type: WalletCurrency }, | ||
}, | ||
resolve: async (_, args) => { | ||
const { username: usernameWithFlags, walletCurrency } = args | ||
|
||
if (usernameWithFlags instanceof Error) { | ||
throw usernameWithFlags | ||
} | ||
|
||
const parser = UsernameParser(usernameWithFlags) | ||
const username = parser.parsedUsername() | ||
if (username instanceof Error) { | ||
throw mapError(username) | ||
} | ||
|
||
const account = await AccountsRepository().findByUsername(username) | ||
if (account instanceof Error) { | ||
throw mapError(account) | ||
} | ||
|
||
const wallets = await Wallets.listWalletsByAccountId(account.id) | ||
if (wallets instanceof Error) { | ||
throw mapError(wallets) | ||
} | ||
|
||
if (parser.isUsd()) { | ||
const wallet = wallets.find( | ||
(wallet) => wallet.currency === DomainWalletCurrency.Usd, | ||
) | ||
if (!wallet) { | ||
throw mapError(new CouldNotFindWalletFromUsernameAndCurrencyError(username)) | ||
} | ||
|
||
return wallet | ||
} | ||
|
||
if (!walletCurrency) { | ||
return wallets.find((wallet) => wallet.id === account.defaultWalletId) | ||
} | ||
|
||
const wallet = wallets.find((wallet) => wallet.currency === walletCurrency) | ||
if (!wallet) { | ||
throw mapError( | ||
new CouldNotFindWalletFromUsernameAndCurrencyError(usernameWithFlags), | ||
) | ||
} | ||
|
||
return wallet | ||
}, | ||
}) | ||
|
||
export default AccountDefaultWalletByUsernameQuery |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
35 changes: 35 additions & 0 deletions
35
core/api/src/graphql/shared/types/scalar/username-with-flags.ts
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,35 @@ | ||
import { UsernameWithFlagsRegex } from "@/domain/accounts" | ||
import { InputValidationError } from "@/graphql/error" | ||
import { GT } from "@/graphql/index" | ||
|
||
const UsernameWithFlags = GT.Scalar({ | ||
name: "UsernameWithFlags", | ||
description: "Unique identifier of a user, with optional flags", | ||
parseValue(value) { | ||
if (typeof value !== "string") { | ||
return new InputValidationError({ | ||
message: "Invalid type for Username with optional flags", | ||
}) | ||
} | ||
return validUsernameWithFlagsValue(value) | ||
}, | ||
parseLiteral(ast) { | ||
if (ast.kind === GT.Kind.STRING) { | ||
return validUsernameWithFlagsValue(ast.value) | ||
} | ||
return new InputValidationError({ | ||
message: "Invalid type for Username with optional flags", | ||
}) | ||
}, | ||
}) | ||
|
||
function validUsernameWithFlagsValue(value: string) { | ||
if (value.match(UsernameWithFlagsRegex)) { | ||
return value.toLowerCase() | ||
} | ||
return new InputValidationError({ | ||
message: "Invalid value for Username with optional flags", | ||
}) | ||
} | ||
|
||
export default UsernameWithFlags |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters