Skip to content

Commit

Permalink
feat(3114): review feedback.
Browse files Browse the repository at this point in the history
  • Loading branch information
koekiebox committed Jan 22, 2025
1 parent 951a4c7 commit 4c60f7b
Show file tree
Hide file tree
Showing 11 changed files with 67 additions and 38 deletions.
3 changes: 2 additions & 1 deletion localenv/mock-account-servicing-entity/generated/graphql.ts

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

14 changes: 13 additions & 1 deletion packages/backend/src/graphql/generated/graphql.schema.json

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

3 changes: 2 additions & 1 deletion packages/backend/src/graphql/generated/graphql.ts

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

24 changes: 18 additions & 6 deletions packages/backend/src/graphql/resolvers/wallet_address.ts
Original file line number Diff line number Diff line change
Expand Up @@ -60,12 +60,25 @@ export const getWalletAddresses: QueryResolvers<TenantedApolloContext>['walletAd

export const getWalletAddress: QueryResolvers<TenantedApolloContext>['walletAddress'] =
async (parent, args, ctx): Promise<ResolversTypes['WalletAddress']> => {
const tenantId = tenantIdToProceed(
ctx.isOperator,
ctx.tenant.id,
args.tenantId
)
if (!tenantId) {
throw new GraphQLError(
errorToMessage[WalletAddressError.UnknownWalletAddress],
{
extensions: {
code: errorToCode[WalletAddressError.UnknownWalletAddress]
}
}
)
}

const walletAddressService = await ctx.container.use('walletAddressService')
const walletAddress = await walletAddressService.get(args.id)
if (
!walletAddress ||
!tenantIdToProceed(ctx.isOperator, ctx.tenant.id, walletAddress.tenantId)
) {
const walletAddress = await walletAddressService.get(args.id, tenantId)
if (!walletAddress) {
throw new GraphQLError(
errorToMessage[WalletAddressError.UnknownWalletAddress],
{
Expand Down Expand Up @@ -111,7 +124,6 @@ export const createWalletAddress: MutationResolvers<ForTenantIdContext>['createW

const options: CreateOptions = {
assetId: args.input.assetId,
// We always have a tenant for [ForTenantIdContext].
tenantId: ctx.forTenantId,
additionalProperties: addProps,
publicName: args.input.publicName,
Expand Down
4 changes: 3 additions & 1 deletion packages/backend/src/graphql/schema.graphql
Original file line number Diff line number Diff line change
Expand Up @@ -53,6 +53,8 @@ type Query {
walletAddress(
"Unique identifier of the wallet address."
id: String!
"Unique identifier of the tenant associated with the wallet address. Optional, if not provided, the tenantId will be obtained from the signature."
tenantId: String
): WalletAddress

"Get a wallet address by its url if it exists"
Expand Down Expand Up @@ -1232,7 +1234,7 @@ type CreateReceiverResponse {

input CreateWalletAddressInput {
"Unique identifier of the tenant associated with the wallet address. This cannot be changed. Optional, if not provided, the tenantId will be obtained from the signature."
tenantId: String
tenantId: ID
"Unique identifier of the asset associated with the wallet address. This cannot be changed."
assetId: String!
"Wallet address URL. This cannot be changed."
Expand Down
5 changes: 0 additions & 5 deletions packages/backend/src/middleware/tenant/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -19,11 +19,6 @@ export async function validateTenantMiddleware(
onFailValidation,
next
} = args
if (!tenantIdInput) {
;(context as ForTenantIdContext).forTenantId = context.tenant.id
return next()
}

const forTenantId = tenantIdToProceed(
context.isOperator,
context.tenant.id,
Expand Down
32 changes: 21 additions & 11 deletions packages/backend/src/open_payments/wallet_address/service.ts
Original file line number Diff line number Diff line change
Expand Up @@ -65,7 +65,7 @@ export interface WalletAddressService {
id: string,
includeVisibleOnlyAddProps: boolean
): Promise<WalletAddressAdditionalProperty[] | undefined>
get(id: string): Promise<WalletAddress | undefined>
get(id: string, tenantId?: string): Promise<WalletAddress | undefined>
getByUrl(url: string): Promise<WalletAddress | undefined>
getOrPollByUrl(url: string): Promise<WalletAddress | undefined>
getPage(
Expand Down Expand Up @@ -116,7 +116,7 @@ export async function createWalletAddressService({
walletAddressId,
includeVisibleOnlyAddProps
),
get: (id) => getWalletAddress(deps, id),
get: (id, tenantId) => getWalletAddress(deps, id, tenantId),
getByUrl: (url) => getWalletAddressByUrl(deps, url),
getOrPollByUrl: (url) => getOrPollByUrl(deps, url),
getPage: (pagination?, sortOrder?, tenantId?) =>
Expand Down Expand Up @@ -268,12 +268,20 @@ async function updateWalletAddress(

async function getWalletAddress(
deps: ServiceDependencies,
id: string
id: string,
tenantId?: string
): Promise<WalletAddress | undefined> {
const walletAdd = await deps.walletAddressCache.get(id)
if (walletAdd) return walletAdd
const inMem = await deps.walletAddressCache.get(id)
if (inMem) {
return tenantId && inMem.tenantId !== tenantId ? undefined : inMem
}

const query = WalletAddress.query(deps.knex)
if (tenantId) {
query.andWhere({ tenantId })
}

const walletAddress = await WalletAddress.query(deps.knex).findById(id)
const walletAddress = await query.findById(id)
if (walletAddress) {
const asset = await deps.assetService.get(walletAddress.assetId)
if (asset) walletAddress.asset = asset
Expand Down Expand Up @@ -349,11 +357,13 @@ async function getWalletAddressPage(
sortOrder?: SortOrder,
tenantId?: string
): Promise<WalletAddress[]> {
const addresses = await WalletAddress.query(deps.knex).getPage(
pagination,
sortOrder,
tenantId
)
const query = WalletAddress.query(deps.knex)

if (tenantId && tenantId.length > 0) {
query.where({ tenantId })
}

const addresses = await query.getPage(pagination, sortOrder)
for (const address of addresses) {
const asset = await deps.assetService.get(address.assetId)
if (asset) address.asset = asset
Expand Down
11 changes: 2 additions & 9 deletions packages/backend/src/shared/baseModel.ts
Original file line number Diff line number Diff line change
Expand Up @@ -50,13 +50,11 @@ class PaginationQueryBuilder<M extends Model, R = M[]> extends QueryBuilder<
* https://relay.dev/graphql/connections.htm
* @param pagination Pagination - cursors and limits.
* @param sortOrder SortOrder - Asc/Desc sort order.
* @param tenantId string - When filtering for a specific tenant.
* @returns Model[] An array of Models that form a page.
*/
getPage(
pagination?: Pagination,
sortOrder: SortOrder = SortOrder.Desc,
tenantId?: string
sortOrder: SortOrder = SortOrder.Desc
): this {
const tableName = this.modelClass().tableName
if (
Expand All @@ -69,18 +67,13 @@ class PaginationQueryBuilder<M extends Model, R = M[]> extends QueryBuilder<
if (first < 0 || first > 100) throw new Error('Pagination index error')
const last = pagination?.last || 20
if (last < 0 || last > 100) throw new Error('Pagination index error')

const tenantFilterClause = tenantId
? ` AND "${tableName}"."tenantId" = '${tenantId}'`
: ''

/**
* Forward pagination
*/
if (typeof pagination?.after === 'string') {
const comparisonOperator = sortOrder === SortOrder.Asc ? '>' : '<'
return this.whereRaw(
`("${tableName}"."createdAt", "${tableName}"."id") ${comparisonOperator} (select "${tableName}"."createdAt" :: TIMESTAMP, "${tableName}"."id" from ?? where "${tableName}"."id" = ?${tenantFilterClause})`,
`("${tableName}"."createdAt", "${tableName}"."id") ${comparisonOperator} (select "${tableName}"."createdAt" :: TIMESTAMP, "${tableName}"."id" from ?? where "${tableName}"."id" = ?)`,
[this.modelClass().tableName, pagination.after]
)
.orderBy([
Expand Down
3 changes: 2 additions & 1 deletion packages/frontend/app/generated/graphql.ts

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

3 changes: 2 additions & 1 deletion packages/mock-account-service-lib/src/generated/graphql.ts

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

3 changes: 2 additions & 1 deletion test/integration/lib/generated/graphql.ts

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

0 comments on commit 4c60f7b

Please sign in to comment.