-
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.
refactor: deprecate current query/subscription
- Loading branch information
Showing
13 changed files
with
274 additions
and
35 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
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
42 changes: 42 additions & 0 deletions
42
core/api/src/graphql/public/root/query/ln-invoice-payment-status.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,42 @@ | ||
import { Lightning } from "@/app" | ||
import { WalletInvoiceStatus } from "@/domain/wallet-invoices" | ||
|
||
import { GT } from "@/graphql/index" | ||
import { mapAndParseErrorForGqlResponse } from "@/graphql/error-map" | ||
import LnInvoicePaymentStatusPayload from "@/graphql/public/types/payload/ln-invoice-payment-status" | ||
import LnInvoicePaymentStatusInput from "@/graphql/public/types/object/ln-invoice-payment-status-input" | ||
|
||
const LnInvoicePaymentStatusQuery = GT.Field({ | ||
type: GT.NonNull(LnInvoicePaymentStatusPayload), | ||
deprecationReason: "Deprecated in favor of lnInvoicePaymentStatusByRequest", | ||
args: { | ||
input: { type: GT.NonNull(LnInvoicePaymentStatusInput) }, | ||
}, | ||
resolve: async (_, args) => { | ||
const { paymentRequest } = args.input | ||
if (paymentRequest instanceof Error) { | ||
return { errors: [{ message: paymentRequest.message }] } | ||
} | ||
|
||
const paymentStatusChecker = await Lightning.PaymentStatusChecker(paymentRequest) | ||
if (paymentStatusChecker instanceof Error) { | ||
return { errors: [mapAndParseErrorForGqlResponse(paymentStatusChecker)] } | ||
} | ||
|
||
const paid = await paymentStatusChecker.invoiceIsPaid() | ||
if (paid instanceof Error) { | ||
return { errors: [mapAndParseErrorForGqlResponse(paid)] } | ||
} | ||
|
||
const { paymentHash, isExpired } = paymentStatusChecker | ||
|
||
if (paid) { | ||
return { errors: [], paymentHash, paymentRequest, status: WalletInvoiceStatus.Paid } | ||
} | ||
|
||
const status = isExpired ? WalletInvoiceStatus.Expired : WalletInvoiceStatus.Pending | ||
return { errors: [], paymentHash, paymentRequest, status } | ||
}, | ||
}) | ||
|
||
export default LnInvoicePaymentStatusQuery |
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
124 changes: 124 additions & 0 deletions
124
core/api/src/graphql/public/root/subscription/ln-invoice-payment-status.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,124 @@ | ||
import { Lightning } from "@/app" | ||
|
||
import { customPubSubTrigger, PubSubDefaultTriggers } from "@/domain/pubsub" | ||
|
||
import { PubSubService } from "@/services/pubsub" | ||
import { baseLogger } from "@/services/logger" | ||
|
||
import { GT } from "@/graphql/index" | ||
import LnInvoicePaymentStatusPayload from "@/graphql/public/types/payload/ln-invoice-payment-status" | ||
import LnInvoicePaymentStatusInput from "@/graphql/public/types/object/ln-invoice-payment-status-input" | ||
import { UnknownClientError } from "@/graphql/error" | ||
import { mapAndParseErrorForGqlResponse } from "@/graphql/error-map" | ||
import { WalletInvoiceStatus } from "@/domain/wallet-invoices" | ||
|
||
const pubsub = PubSubService() | ||
|
||
type LnInvoicePaymentSubscribeArgs = { | ||
input: { | ||
paymentRequest: EncodedPaymentRequest | Error | ||
} | ||
} | ||
|
||
type LnInvoicePaymentResolveSource = { | ||
errors?: IError[] | ||
status?: string | ||
paymentHash?: PaymentHash | ||
paymentRequest?: EncodedPaymentRequest | ||
} | ||
|
||
const LnInvoicePaymentStatusSubscription = { | ||
type: GT.NonNull(LnInvoicePaymentStatusPayload), | ||
deprecationReason: "Deprecated in favor of lnInvoicePaymentStatusByRequest", | ||
args: { | ||
input: { type: GT.NonNull(LnInvoicePaymentStatusInput) }, | ||
}, | ||
resolve: async (source: LnInvoicePaymentResolveSource | undefined) => { | ||
if (source === undefined) { | ||
throw new UnknownClientError({ | ||
message: | ||
"Got 'undefined' payload. Check url used to ensure right websocket endpoint was used for subscription.", | ||
logger: baseLogger, | ||
}) | ||
} | ||
|
||
if (source.errors) { | ||
return { errors: source.errors } | ||
} | ||
|
||
let paymentRequest = source.paymentRequest | ||
if (source.paymentHash && !source.paymentRequest) { | ||
const invoice = await Lightning.getInvoiceRequestByHash({ | ||
paymentHash: source.paymentHash, | ||
}) | ||
paymentRequest = invoice instanceof Error ? paymentRequest : invoice | ||
} | ||
return { | ||
errors: [], | ||
status: source.status, | ||
paymentHash: source.paymentHash, | ||
paymentRequest, | ||
} | ||
}, | ||
|
||
subscribe: async (_source: unknown, args: LnInvoicePaymentSubscribeArgs) => { | ||
const { paymentRequest } = args.input | ||
if (paymentRequest instanceof Error) throw paymentRequest | ||
|
||
const paymentStatusChecker = await Lightning.PaymentStatusChecker(paymentRequest) | ||
|
||
if (paymentStatusChecker instanceof Error) { | ||
const lnPaymentStatusTrigger = customPubSubTrigger({ | ||
event: PubSubDefaultTriggers.LnPaymentStatus, | ||
suffix: paymentRequest, | ||
}) | ||
pubsub.publishDelayed({ | ||
trigger: lnPaymentStatusTrigger, | ||
payload: { errors: [mapAndParseErrorForGqlResponse(paymentStatusChecker)] }, | ||
}) | ||
|
||
return pubsub.createAsyncIterator({ trigger: lnPaymentStatusTrigger }) | ||
} | ||
|
||
const paymentHash = paymentStatusChecker.paymentHash | ||
const trigger = customPubSubTrigger({ | ||
event: PubSubDefaultTriggers.LnPaymentStatus, | ||
suffix: paymentHash, | ||
}) | ||
const paid = await paymentStatusChecker.invoiceIsPaid() | ||
|
||
if (paid instanceof Error) { | ||
pubsub.publishDelayed({ | ||
trigger, | ||
payload: { errors: [mapAndParseErrorForGqlResponse(paid)] }, | ||
}) | ||
} | ||
|
||
if (paid) { | ||
pubsub.publishDelayed({ | ||
trigger, | ||
payload: { paymentHash, paymentRequest, status: WalletInvoiceStatus.Paid }, | ||
}) | ||
return pubsub.createAsyncIterator({ trigger }) | ||
} | ||
|
||
const status = paymentStatusChecker.isExpired | ||
? WalletInvoiceStatus.Expired | ||
: WalletInvoiceStatus.Pending | ||
pubsub.publishDelayed({ trigger, payload: { paymentHash, paymentRequest, status } }) | ||
|
||
if (!paymentStatusChecker.isExpired) { | ||
const timeout = Math.max(paymentStatusChecker.expiresAt.getTime() - Date.now(), 0) | ||
setTimeout(() => { | ||
pubsub.publish({ | ||
trigger, | ||
payload: { paymentHash, paymentRequest, status: WalletInvoiceStatus.Expired }, | ||
}) | ||
}, timeout + 1000) | ||
} | ||
|
||
return pubsub.createAsyncIterator({ trigger }) | ||
}, | ||
} | ||
|
||
export default LnInvoicePaymentStatusSubscription |
Oops, something went wrong.