Skip to content

Commit

Permalink
feat: adding a fetchByUserId admin endpoint (#3401)
Browse files Browse the repository at this point in the history
Co-authored-by: Nicolas Burtey <[email protected]>
  • Loading branch information
nicolasburtey and Nicolas Burtey authored Oct 20, 2023
1 parent 8e7c0ec commit 58eb8d1
Show file tree
Hide file tree
Showing 7 changed files with 63 additions and 1 deletion.
9 changes: 8 additions & 1 deletion core/api/src/app/admin/index.ts
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
export * from "./update-user-phone"
export * from "./send-admin-push-notification"

import { checkedToAccountId, checkedToUsername } from "@/domain/accounts"
import { checkedToAccountId, checkedToUserId, checkedToUsername } from "@/domain/accounts"
import { IdentityRepository } from "@/services/kratos"
import { AccountsRepository, UsersRepository } from "@/services/mongoose"

Expand Down Expand Up @@ -38,3 +38,10 @@ export const getAccountByAccountId = async (accountIdRaw: string) => {
const accounts = AccountsRepository()
return accounts.findById(accountId)
}

export const getAccountByUserId = async (userIdRaw: string) => {
const userId = checkedToUserId(userIdRaw)
if (userId instanceof Error) return userId
const accounts = AccountsRepository()
return accounts.findByUserId(userId)
}
2 changes: 2 additions & 0 deletions core/api/src/graphql/admin/queries.ts
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,7 @@ import AccountDetailsByUserEmailQuery from "./root/query/account-details-by-emai
import ListWalletIdsQuery from "./root/query/all-walletids"
import WalletQuery from "./root/query/wallet"
import AccountDetailsByAccountId from "./root/query/account-details-by-account-id"
import AccountDetailsByUserId from "./root/query/account-details-by-user-id"

import { GT } from "@/graphql/index"

Expand All @@ -20,6 +21,7 @@ export const queryFields = {
accountDetailsByUsername: AccountDetailsByUsernameQuery,
accountDetailsByEmail: AccountDetailsByUserEmailQuery,
accountDetailsByAccountId: AccountDetailsByAccountId,
accountDetailsByUserId: AccountDetailsByUserId,
transactionById: TransactionByIdQuery,
transactionsByHash: TransactionsByHashQuery,
lightningInvoice: LightningInvoiceQuery,
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,27 @@
import { GT } from "@/graphql/index"

import AuditedAccount from "@/graphql/admin/types/object/account"
import { mapError } from "@/graphql/error-map"

import { Admin } from "@/app"

const AccountDetailsByUserId = GT.Field({
args: {
userId: { type: GT.NonNullID },
},
type: GT.NonNull(AuditedAccount),
resolve: async (_, { userId }) => {
if (userId instanceof Error) {
throw userId
}

const account = await Admin.getAccountByUserId(userId)
if (account instanceof Error) {
throw mapError(account)
}

return account
},
})

export default AccountDetailsByUserId
1 change: 1 addition & 0 deletions core/api/src/graphql/admin/schema.graphql
Original file line number Diff line number Diff line change
Expand Up @@ -271,6 +271,7 @@ type PriceOfOneSettlementMinorUnitInDisplayMinorUnit implements PriceInterface {
type Query {
accountDetailsByAccountId(accountId: ID!): AuditedAccount!
accountDetailsByEmail(email: EmailAddress!): AuditedAccount!
accountDetailsByUserId(userId: ID!): AuditedAccount!
accountDetailsByUserPhone(phone: Phone!): AuditedAccount!
accountDetailsByUsername(username: Username!): AuditedAccount!
allLevels: [AccountLevel!]!
Expand Down
Original file line number Diff line number Diff line change
@@ -1,5 +1,8 @@
query accountDetailsByAccountId($accountId: ID!) {
accountDetailsByAccountId(accountId: $accountId) {
id
owner {
id
}
}
}
8 changes: 8 additions & 0 deletions core/api/test/bats/admin-gql/account-details-by-user-id.gql
Original file line number Diff line number Diff line change
@@ -0,0 +1,8 @@
query accountDetailsByUserId($userId: ID!) {
accountDetailsByUserId(userId: $userId) {
id
owner {
id
}
}
}
14 changes: 14 additions & 0 deletions core/api/test/bats/admin.bats
Original file line number Diff line number Diff line change
Expand Up @@ -160,6 +160,20 @@ gql_admin_file() {
returnedId="$(graphql_output '.data.accountDetailsByAccountId.id')"
[[ "$returnedId" == "$id" ]] || exit 1

userId="$(graphql_output '.data.accountDetailsByAccountId.owner.id')"
echo "userId: $userId"

variables=$(
jq -n \
--arg userId "$userId" \
'{userId: $userId}'
)

exec_admin_graphql "$admin_token" 'account-details-by-user-id' "$variables"
returnedId="$(graphql_output '.data.accountDetailsByUserId.owner.id')"
[[ "$returnedId" == "$userId" ]] || exit 1


# TODO: add check by email

# TODO: business update map info
Expand Down

0 comments on commit 58eb8d1

Please sign in to comment.