Skip to content

Commit

Permalink
chore: address pr feedback
Browse files Browse the repository at this point in the history
- remove use of graphql types
  • Loading branch information
UncleSamtoshi committed Nov 8, 2023
1 parent cd828d4 commit 1e64dce
Show file tree
Hide file tree
Showing 18 changed files with 240 additions and 185 deletions.
13 changes: 9 additions & 4 deletions core/api/src/app/accounts/get-invoices-for-account.ts
Original file line number Diff line number Diff line change
Expand Up @@ -6,12 +6,17 @@ import { WalletsRepository } from "@/services/mongoose"
export const getInvoicesForAccountByWalletIds = async ({
account,
walletIds,
paginationArgs,
rawPaginationArgs,
}: {
account: Account
walletIds?: WalletId[]
paginationArgs?: PaginationArgs
}): Promise<PaginatedResult<WalletInvoice> | ApplicationError> => {
rawPaginationArgs: {
first?: number | null
last?: number | null
before?: string | null
after?: string | null
}
}): Promise<PaginatedQueryResult<WalletInvoice> | ApplicationError> => {
const walletsRepo = WalletsRepository()

const accountWallets = await walletsRepo.listByAccountId(account.id)
Expand All @@ -21,5 +26,5 @@ export const getInvoicesForAccountByWalletIds = async ({
walletIds ? walletIds.includes(wallet.id) : true,
)

return getInvoicesForWallets({ wallets, paginationArgs })
return getInvoicesForWallets({ wallets, rawPaginationArgs })
}
2 changes: 0 additions & 2 deletions core/api/src/app/errors.ts
Original file line number Diff line number Diff line change
Expand Up @@ -20,7 +20,6 @@ import * as CaptchaErrors from "@/domain/captcha/errors"
import * as AuthenticationErrors from "@/domain/authentication/errors"
import * as UserErrors from "@/domain/users/errors"
import * as WalletInvoiceErrors from "@/domain/wallet-invoices/errors"
import * as PaginationErrors from "@/domain/pagination/errors"

import * as LedgerFacadeErrors from "@/services/ledger/domain/errors"
import * as KratosErrors from "@/services/kratos/errors"
Expand Down Expand Up @@ -50,7 +49,6 @@ export const ApplicationErrors = {
...AuthenticationErrors,
...UserErrors,
...WalletInvoiceErrors,
...PaginationErrors,

...KratosErrors,
...LedgerFacadeErrors,
Expand Down
25 changes: 15 additions & 10 deletions core/api/src/app/wallets/get-invoices-for-wallets.ts
Original file line number Diff line number Diff line change
@@ -1,27 +1,32 @@
import { MAX_PAGINATION_PAGE_SIZE } from "@/config"
import { parsePaginationArgs } from "@/domain/pagination"
import { checkedToPaginatedQueryArgs } from "@/domain/primitives"
import { WalletInvoicesRepository } from "@/services/mongoose"

export const getInvoicesForWallets = async ({
wallets,
paginationArgs,
rawPaginationArgs,
}: {
wallets: Wallet[]
paginationArgs?: PaginationArgs
}): Promise<PaginatedResult<WalletInvoice> | ApplicationError> => {
rawPaginationArgs: {
first?: number | null
last?: number | null
before?: string | null
after?: string | null
}
}): Promise<PaginatedQueryResult<WalletInvoice> | ApplicationError> => {
const walletIds = wallets.map((wallet) => wallet.id)

const parsedPaginationArgs = parsePaginationArgs({
paginationArgs,
const paginationArgs = checkedToPaginatedQueryArgs({
paginationArgs: rawPaginationArgs,
maxPageSize: MAX_PAGINATION_PAGE_SIZE,
})

if (parsedPaginationArgs instanceof Error) {
return parsedPaginationArgs
if (paginationArgs instanceof Error) {
return paginationArgs
}

return WalletInvoicesRepository().getInvoicesForWallets({
return WalletInvoicesRepository().findInvoicesForWallets({
walletIds,
paginationArgs: parsedPaginationArgs,
paginationArgs,
})
}
2 changes: 2 additions & 0 deletions core/api/src/domain/ledger/index.types.d.ts
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,8 @@ type LedgerError = import("./errors").LedgerError
type FeeDifferenceError = import("./errors").FeeDifferenceError
type LedgerServiceError = import("./errors").LedgerServiceError

type PaginationArgs = import("graphql-relay").ConnectionArguments

declare const liabilitiesWalletId: unique symbol
type LiabilitiesWalletId = string & { [liabilitiesWalletId]: never }

Expand Down
2 changes: 0 additions & 2 deletions core/api/src/domain/pagination/errors.ts

This file was deleted.

60 changes: 0 additions & 60 deletions core/api/src/domain/pagination/index.ts

This file was deleted.

19 changes: 0 additions & 19 deletions core/api/src/domain/pagination/index.types.d.ts

This file was deleted.

76 changes: 74 additions & 2 deletions core/api/src/domain/primitives/index.ts
Original file line number Diff line number Diff line change
@@ -1,5 +1,4 @@
import { InvalidMinutesError } from "@/domain/errors"

import { InvalidMinutesError, InvalidPaginatedQueryArgsError } from "@/domain/errors"
export const toSeconds = (seconds: number): Seconds => {
return seconds as Seconds
}
Expand All @@ -13,3 +12,76 @@ export const checkedToMinutes = (minutes: number): Minutes | ValidationError =>
if (!isMinutes) return new InvalidMinutesError(`Invalid value for minutes: ${minutes}`)
return minutes as Minutes
}

export const checkedToPaginatedQueryCursor = (cursor: string): PaginatedQueryCursor => {
return cursor as PaginatedQueryCursor
}

export const checkedToPaginatedQueryArgs = ({
paginationArgs,
maxPageSize,
}: {
paginationArgs: {
first?: number | null
last?: number | null
before?: string | null
after?: string | null
}
maxPageSize: number
}): InvalidPaginatedQueryArgsError | PaginatedQueryArgs => {
const { first, last, before, after } = paginationArgs || {}
if (first && last) {
return new InvalidPaginatedQueryArgsError(`Cannot use both "first" and "last".`)
}

const afterCursor =
typeof after === "string" ? checkedToPaginatedQueryCursor(after) : undefined
const beforeCursor =
typeof before === "string" ? checkedToPaginatedQueryCursor(before) : undefined

if (typeof first === "number") {
if (first > maxPageSize) {
return new InvalidPaginatedQueryArgsError(
`Requested page size (${first}) is greater than max allowed page size ${maxPageSize}.`,
)
}

if (first <= 0) {
return new InvalidPaginatedQueryArgsError(
`Requested page size (${first}) must be greater than 0.`,
)
}

return {
first,
after: afterCursor,
before: beforeCursor,
}
}

if (typeof last === "number") {
if (last > maxPageSize) {
return new InvalidPaginatedQueryArgsError(
`Requested page size (${last}) is greater than max allowed page size ${maxPageSize}.`,
)
}

if (last <= 0) {
return new InvalidPaginatedQueryArgsError(
`Requested page size (${last}) must be greater than 0.`,
)
}

return {
last,
after: afterCursor,
before: beforeCursor,
}
}

return {
first: maxPageSize,
after: afterCursor,
before: beforeCursor,
}
}
27 changes: 27 additions & 0 deletions core/api/src/domain/primitives/index.types.d.ts
Original file line number Diff line number Diff line change
Expand Up @@ -28,3 +28,30 @@ type JSONValue =
| undefined
type JSONArray = Array<JSONValue>
type JSONObject = { [key: string]: JSONValue }

type PaginatedQueryCursor = string & { readonly brand: unique symbol }
type PaginatedQueryArgs =
| {
first: number
last?: undefined
after?: PaginatedQueryCursor
before?: PaginatedQueryCursor
}
| {
first?: undefined
last: number
before?: PaginatedQueryCursor
after?: PaginatedQueryCursor
}
type PaginatedQueryResult<T> = {
edges: {
node: T
cursor: PaginatedQueryCursor
}[]
pageInfo: {
startCursor?: PaginatedQueryCursor
endCursor?: PaginatedQueryCursor
hasNextPage: boolean
hasPreviousPage: boolean
}
}
6 changes: 3 additions & 3 deletions core/api/src/domain/wallet-invoices/index.types.d.ts
Original file line number Diff line number Diff line change
Expand Up @@ -179,13 +179,13 @@ interface IWalletInvoicesRepository {
paymentHash: PaymentHash,
) => Promise<WalletInvoiceWithOptionalLnInvoice | RepositoryError>

getInvoicesForWallets: ({
findInvoicesForWallets: ({
walletIds,
paginationArgs,
}: {
walletIds: WalletId[]
paginationArgs: ParsedPaginationArgs
}) => Promise<PaginatedResult<WalletInvoice> | RepositoryError>
paginationArgs: PaginatedQueryArgs
}) => Promise<PaginatedQueryResult<WalletInvoice> | RepositoryError>

findByPaymentHash: (
paymentHash: PaymentHash,
Expand Down
2 changes: 1 addition & 1 deletion core/api/src/graphql/error-map.ts
Original file line number Diff line number Diff line change
Expand Up @@ -446,7 +446,7 @@ export const mapError = (error: ApplicationError): CustomGraphQLError => {

case "UnauthorizedIPMetadataCountryError":
return new UnauthorizedIPMetadataCountryError({ logger: baseLogger })
case "InvalidPaginationArgsError":
case "InvalidPaginatedQueryArgsError":
message = error.message
return new ValidationInternalError({ message, logger: baseLogger })

Expand Down
1 change: 0 additions & 1 deletion core/api/src/graphql/public/types/abstract/account.ts
Original file line number Diff line number Diff line change
Expand Up @@ -65,7 +65,6 @@ const IAccount = GT.Interface({
pendingTransactions: {
type: GT.NonNullList(Transaction),
args: {
...connectionArgs,
walletIds: {
type: GT.List(WalletId),
},
Expand Down
2 changes: 1 addition & 1 deletion core/api/src/graphql/shared/types/object/btc-wallet.ts
Original file line number Diff line number Diff line change
Expand Up @@ -135,7 +135,7 @@ const BtcWallet = GT.Object<Wallet>({
resolve: async (source, args) => {
const result = await Wallets.getInvoicesForWallets({
wallets: [source],
paginationArgs: args,
rawPaginationArgs: args,
})

if (result instanceof Error) {
Expand Down
2 changes: 1 addition & 1 deletion core/api/src/graphql/shared/types/object/usd-wallet.ts
Original file line number Diff line number Diff line change
Expand Up @@ -133,7 +133,7 @@ const UsdWallet = GT.Object<Wallet>({
resolve: async (source, args) => {
const result = await Wallets.getInvoicesForWallets({
wallets: [source],
paginationArgs: args,
rawPaginationArgs: args,
})

if (result instanceof Error) {
Expand Down
Loading

0 comments on commit 1e64dce

Please sign in to comment.