-
Notifications
You must be signed in to change notification settings - Fork 89
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
662/mk/use open payments types #1024
Changes from 23 commits
c3ccd88
3415f5f
4693a79
c633b30
909462a
8feaacb
5300073
01a0ba0
36d8b8e
d04f994
379aee8
11fa542
27cfdde
4efddb3
dc051ec
0fddf9e
ad57048
d48dc0a
9302bad
5e5d4e8
25972d3
4800c80
cf95410
9a967f1
e5f3c03
5733224
28a34df
5e95486
b6d25c6
09a0c10
c079942
c55e586
fc311e2
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
Original file line number | Diff line number | Diff line change | ||||
---|---|---|---|---|---|---|
|
@@ -2,7 +2,7 @@ import { Model, ModelOptions, Pojo, QueryContext } from 'objection' | |||||
import { v4 as uuid } from 'uuid' | ||||||
|
||||||
import { Amount, AmountJSON, serializeAmount } from '../../amount' | ||||||
import { Connection, ConnectionJSON } from '../../connection/model' | ||||||
import { Connection } from '../../connection/model' | ||||||
import { | ||||||
PaymentPointer, | ||||||
PaymentPointerSubresource | ||||||
|
@@ -11,7 +11,11 @@ import { Asset } from '../../../asset/model' | |||||
import { LiquidityAccount, OnCreditOptions } from '../../../accounting/service' | ||||||
import { ConnectorAccount } from '../../../connector/core/rafiki' | ||||||
import { WebhookEvent } from '../../../webhook/model' | ||||||
import { IncomingPayment as OpenPaymentsIncomingPayment } from 'open-payments' | ||||||
import { | ||||||
IncomingPayment as OpenPaymentsIncomingPayment, | ||||||
IncomingPaymentWithConnection as OpenPaymentsIncomingPaymentWithConnection, | ||||||
IncomingPaymentWithConnectionUrl as OpenPaymentsIncomingPaymentWithConnectionUrl | ||||||
} from 'open-payments' | ||||||
|
||||||
export enum IncomingPaymentEventType { | ||||||
IncomingPaymentExpired = 'incoming_payment.expired', | ||||||
|
@@ -80,7 +84,6 @@ export class IncomingPayment | |||||
} | ||||||
} | ||||||
|
||||||
public paymentPointer!: PaymentPointer | ||||||
public description?: string | ||||||
public expiresAt!: Date | ||||||
public state!: IncomingPaymentState | ||||||
|
@@ -127,8 +130,15 @@ export class IncomingPayment | |||||
this.receivedAmountValue = amount.value | ||||||
} | ||||||
|
||||||
public get url(): string { | ||||||
return `${this.paymentPointer.url}${IncomingPayment.urlPath}/${this.id}` | ||||||
public async getUrl(fetchedPaymentPointer?: PaymentPointer): Promise<string> { | ||||||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Adding There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. What if callers just assigned There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Taking a step back, if/when we change subresource urls to not include the payment pointer, we'll no longer need an async There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. I'm hesitant to do that, since Maybe we can
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. To clarify, I'm only suggesting changing
Suggested change
and callers can optionally define |
||||||
const paymentPointer = | ||||||
fetchedPaymentPointer || (await this.getPaymentPointer()) | ||||||
|
||||||
return `${paymentPointer.url}${IncomingPayment.urlPath}/${this.id}` | ||||||
} | ||||||
|
||||||
public async getPaymentPointer(): Promise<PaymentPointer> { | ||||||
return this.paymentPointer ?? (await this.$relatedQuery('paymentPointer')) | ||||||
} | ||||||
|
||||||
public async onCredit({ | ||||||
|
@@ -239,39 +249,56 @@ export class IncomingPayment | |||||
return payment | ||||||
} | ||||||
|
||||||
public toOpenPaymentsType({ | ||||||
ilpStreamConnection | ||||||
}: { | ||||||
public async toOpenPaymentsType(): Promise<OpenPaymentsIncomingPayment> | ||||||
public async toOpenPaymentsType( | ||||||
ilpStreamConnection: Connection | ||||||
}): OpenPaymentsIncomingPayment { | ||||||
return { | ||||||
id: this.url, | ||||||
paymentPointer: this.paymentPointer.url, | ||||||
): Promise<OpenPaymentsIncomingPaymentWithConnection> | ||||||
public async toOpenPaymentsType( | ||||||
ilpStreamConnection: string | ||||||
): Promise<OpenPaymentsIncomingPaymentWithConnectionUrl> | ||||||
public async toOpenPaymentsType( | ||||||
ilpStreamConnection?: Connection | string | ||||||
): Promise< | ||||||
| OpenPaymentsIncomingPaymentWithConnection | ||||||
| OpenPaymentsIncomingPaymentWithConnectionUrl | ||||||
> | ||||||
public async toOpenPaymentsType( | ||||||
ilpStreamConnection?: Connection | string | ||||||
): Promise< | ||||||
| OpenPaymentsIncomingPayment | ||||||
| OpenPaymentsIncomingPaymentWithConnection | ||||||
| OpenPaymentsIncomingPaymentWithConnectionUrl | ||||||
> { | ||||||
const paymentPointer = await this.getPaymentPointer() | ||||||
const baseIncomingPayment: OpenPaymentsIncomingPayment = { | ||||||
id: await this.getUrl(paymentPointer), | ||||||
paymentPointer: paymentPointer.url, | ||||||
incomingAmount: this.incomingAmount | ||||||
? serializeAmount(this.incomingAmount) | ||||||
: undefined, | ||||||
receivedAmount: serializeAmount(this.receivedAmount), | ||||||
completed: this.completed, | ||||||
description: this.description ?? undefined, | ||||||
externalRef: this.externalRef ?? undefined, | ||||||
createdAt: this.createdAt.toISOString(), | ||||||
updatedAt: this.updatedAt.toISOString(), | ||||||
expiresAt: this.expiresAt.toISOString(), | ||||||
expiresAt: this.expiresAt.toISOString() | ||||||
} | ||||||
|
||||||
if (!ilpStreamConnection) { | ||||||
return baseIncomingPayment | ||||||
} | ||||||
|
||||||
if (typeof ilpStreamConnection === 'string') { | ||||||
return { | ||||||
...baseIncomingPayment, | ||||||
ilpStreamConnection | ||||||
} | ||||||
} | ||||||
|
||||||
return { | ||||||
...baseIncomingPayment, | ||||||
ilpStreamConnection: ilpStreamConnection.toOpenPaymentsType() | ||||||
} | ||||||
} | ||||||
} | ||||||
|
||||||
// TODO: disallow undefined | ||||||
// https://github.com/interledger/rafiki/issues/594 | ||||||
export type IncomingPaymentJSON = { | ||||||
id: string | ||||||
paymentPointer: string | ||||||
incomingAmount?: AmountJSON | ||||||
receivedAmount: AmountJSON | ||||||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. @wilsonianb Keeping There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. 👍 I'm good with keeping amount values as strings in |
||||||
completed: boolean | ||||||
description?: string | ||||||
externalRef?: string | ||||||
createdAt: string | ||||||
updatedAt: string | ||||||
expiresAt?: string | ||||||
ilpStreamConnection?: ConnectionJSON | string | ||||||
} |
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,4 +1,3 @@ | ||
import { AccessAction } from 'open-payments' | ||
import { Logger } from 'pino' | ||
import { | ||
ReadContext, | ||
|
@@ -8,7 +7,7 @@ import { | |
} from '../../../app' | ||
import { IAppConfig } from '../../../config/app' | ||
import { IncomingPaymentService } from './service' | ||
import { IncomingPayment, IncomingPaymentJSON } from './model' | ||
import { IncomingPayment } from './model' | ||
import { | ||
errorToCode, | ||
errorToMessage, | ||
|
@@ -17,8 +16,14 @@ import { | |
} from './errors' | ||
import { AmountJSON, parseAmount } from '../../amount' | ||
import { listSubresource } from '../../payment_pointer/routes' | ||
import { ConnectionJSON } from '../../connection/model' | ||
import { Connection } from '../../connection/model' | ||
import { ConnectionService } from '../../connection/service' | ||
import { | ||
AccessAction, | ||
IncomingPayment as OpenPaymentsIncomingPayment, | ||
IncomingPaymentWithConnection as OpenPaymentsIncomingPaymentWithConnection, | ||
IncomingPaymentWithConnectionUrl as OpenPaymentsIncomingPaymentWithConnectionUrl | ||
} from 'open-payments' | ||
|
||
// Don't allow creating an incoming payment too far out. Incoming payments with no payments before they expire are cleaned up, since incoming payments creation is unauthenticated. | ||
// TODO what is a good default value for this? | ||
|
@@ -70,7 +75,7 @@ async function getIncomingPayment( | |
} | ||
if (!incomingPayment) return ctx.throw(404) | ||
const connection = deps.connectionService.get(incomingPayment) | ||
ctx.body = incomingPaymentToBody(deps, incomingPayment, connection?.toJSON()) | ||
ctx.body = await incomingPaymentToBody(incomingPayment, connection) | ||
} | ||
|
||
export type CreateBody = { | ||
|
@@ -111,11 +116,7 @@ async function createIncomingPayment( | |
|
||
ctx.status = 201 | ||
const connection = deps.connectionService.get(incomingPaymentOrError) | ||
ctx.body = incomingPaymentToBody( | ||
deps, | ||
incomingPaymentOrError, | ||
connection?.toJSON() | ||
) | ||
ctx.body = await incomingPaymentToBody(incomingPaymentOrError, connection) | ||
} | ||
|
||
async function completeIncomingPayment( | ||
|
@@ -137,7 +138,7 @@ async function completeIncomingPayment( | |
errorToMessage[incomingPaymentOrError] | ||
) | ||
} | ||
ctx.body = incomingPaymentToBody(deps, incomingPaymentOrError) | ||
ctx.body = await incomingPaymentToBody(incomingPaymentOrError) | ||
} | ||
|
||
async function listIncomingPayments( | ||
|
@@ -148,9 +149,8 @@ async function listIncomingPayments( | |
await listSubresource({ | ||
ctx, | ||
getPaymentPointerPage: deps.incomingPaymentService.getPaymentPointerPage, | ||
toBody: (payment) => | ||
incomingPaymentToBody( | ||
deps, | ||
toBody: async (payment) => | ||
await incomingPaymentToBody( | ||
payment, | ||
deps.connectionService.getUrl(payment) | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Could we either make this There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. I think it should be ok as it is, since it's a service method, not a model? There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. I'm also ok keeping it as is. |
||
) | ||
|
@@ -159,19 +159,13 @@ async function listIncomingPayments( | |
ctx.throw(500, 'Error trying to list incoming payments') | ||
} | ||
} | ||
|
||
function incomingPaymentToBody( | ||
deps: ServiceDependencies, | ||
async function incomingPaymentToBody( | ||
incomingPayment: IncomingPayment, | ||
ilpStreamConnection?: ConnectionJSON | string | ||
): IncomingPaymentJSON { | ||
const body = { | ||
...incomingPayment.toJSON(), | ||
id: incomingPayment.url, | ||
paymentPointer: incomingPayment.paymentPointer.url | ||
} as unknown as IncomingPaymentJSON | ||
if (ilpStreamConnection) { | ||
body.ilpStreamConnection = ilpStreamConnection | ||
} | ||
return body | ||
ilpStreamConnection?: Connection | string | ||
): Promise< | ||
| OpenPaymentsIncomingPayment | ||
| OpenPaymentsIncomingPaymentWithConnection | ||
| OpenPaymentsIncomingPaymentWithConnectionUrl | ||
> { | ||
return incomingPayment.toOpenPaymentsType(ilpStreamConnection) | ||
} |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
no non-null assertion since we can start removing
withGraphFetched('paymentPointer')
from the DB calls