fail cb + send before ready
This commit is contained in:
parent
008d11d047
commit
4077b0beca
2 changed files with 25 additions and 8 deletions
|
|
@ -235,12 +235,22 @@ export default class {
|
||||||
async PayAppUserInvoice(appId: string, req: Types.PayAppUserInvoiceRequest): Promise<Types.PayInvoiceResponse> {
|
async PayAppUserInvoice(appId: string, req: Types.PayAppUserInvoiceRequest): Promise<Types.PayInvoiceResponse> {
|
||||||
const app = await this.storage.applicationStorage.GetApplication(appId)
|
const app = await this.storage.applicationStorage.GetApplication(appId)
|
||||||
const appUser = await this.storage.applicationStorage.GetApplicationUser(app, req.user_identifier)
|
const appUser = await this.storage.applicationStorage.GetApplicationUser(app, req.user_identifier)
|
||||||
const paid = await this.paymentManager.PayInvoice(appUser.user.user_id, req, app, {
|
try {
|
||||||
ack: pendingOp => { this.notifyAppUserPayment(appUser, pendingOp) }
|
const paid = await this.paymentManager.PayInvoice(appUser.user.user_id, req, app, {
|
||||||
})
|
ack: pendingOp => { this.notifyAppUserPayment(appUser, pendingOp) }
|
||||||
this.notifyAppUserPayment(appUser, paid.operation)
|
})
|
||||||
getLogger({ appName: app.name })(appUser.identifier, "invoice paid", paid.amount_paid, "sats")
|
this.notifyAppUserPayment(appUser, paid.operation)
|
||||||
return paid
|
getLogger({ appName: app.name })(appUser.identifier, "invoice paid", paid.amount_paid, "sats")
|
||||||
|
return paid
|
||||||
|
} catch (e) {
|
||||||
|
const failedOp: Types.UserOperation = {
|
||||||
|
type: Types.UserOperationType.OUTGOING_INVOICE,
|
||||||
|
paidAtUnix: -1, amount: 0, confirmed: false, identifier: req.invoice, operationId: "",
|
||||||
|
inbound: false, internal: false, network_fee: 0, service_fee: 0, tx_hash: "",
|
||||||
|
}
|
||||||
|
this.notifyAppUserPayment(appUser, failedOp)
|
||||||
|
throw e
|
||||||
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
notifyAppUserPayment = (appUser: ApplicationUser, op: Types.UserOperation) => {
|
notifyAppUserPayment = (appUser: ApplicationUser, op: Types.UserOperation) => {
|
||||||
|
|
|
||||||
|
|
@ -1,13 +1,19 @@
|
||||||
import { NostrSend, SendData, SendInitiator } from "./nostrPool.js"
|
import { NostrSend, SendData, SendInitiator } from "./nostrPool.js"
|
||||||
|
import { getLogger } from "../helpers/logger.js"
|
||||||
export class NostrSender {
|
export class NostrSender {
|
||||||
private _nostrSend: NostrSend = () => { throw new Error('nostr send not initialized yet') }
|
private _nostrSend: NostrSend = () => { throw new Error('nostr send not initialized yet') }
|
||||||
private isReady: boolean = false
|
private isReady: boolean = false
|
||||||
private onReadyCallbacks: (() => void)[] = []
|
private onReadyCallbacks: (() => void)[] = []
|
||||||
|
private pendingSends: { initiator: SendInitiator, data: SendData, relays?: string[] | undefined }[] = []
|
||||||
|
private log = getLogger({ component: "nostrSender" })
|
||||||
|
|
||||||
AttachNostrSend(nostrSend: NostrSend) {
|
AttachNostrSend(nostrSend: NostrSend) {
|
||||||
this._nostrSend = nostrSend
|
this._nostrSend = nostrSend
|
||||||
this.isReady = true
|
this.isReady = true
|
||||||
this.onReadyCallbacks.forEach(cb => cb())
|
this.onReadyCallbacks.forEach(cb => cb())
|
||||||
this.onReadyCallbacks = []
|
this.onReadyCallbacks = []
|
||||||
|
this.pendingSends.forEach(send => this._nostrSend(send.initiator, send.data, send.relays))
|
||||||
|
this.pendingSends = []
|
||||||
}
|
}
|
||||||
OnReady(callback: () => void) {
|
OnReady(callback: () => void) {
|
||||||
if (this.isReady) {
|
if (this.isReady) {
|
||||||
|
|
@ -17,8 +23,9 @@ export class NostrSender {
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
Send(initiator: SendInitiator, data: SendData, relays?: string[] | undefined) {
|
Send(initiator: SendInitiator, data: SendData, relays?: string[] | undefined) {
|
||||||
if (!this._nostrSend) {
|
if (!this.isReady) {
|
||||||
throw new Error("No nostrSend attached")
|
this.log("tried to send before nostr was ready, caching request")
|
||||||
|
this.pendingSends.push({ initiator, data, relays })
|
||||||
}
|
}
|
||||||
this._nostrSend(initiator, data, relays)
|
this._nostrSend(initiator, data, relays)
|
||||||
}
|
}
|
||||||
|
|
|
||||||
Loading…
Add table
Add a link
Reference in a new issue