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> {
|
||||
const app = await this.storage.applicationStorage.GetApplication(appId)
|
||||
const appUser = await this.storage.applicationStorage.GetApplicationUser(app, req.user_identifier)
|
||||
try {
|
||||
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")
|
||||
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) => {
|
||||
|
|
|
|||
|
|
@ -1,13 +1,19 @@
|
|||
import { NostrSend, SendData, SendInitiator } from "./nostrPool.js"
|
||||
import { getLogger } from "../helpers/logger.js"
|
||||
export class NostrSender {
|
||||
private _nostrSend: NostrSend = () => { throw new Error('nostr send not initialized yet') }
|
||||
private isReady: boolean = false
|
||||
private onReadyCallbacks: (() => void)[] = []
|
||||
private pendingSends: { initiator: SendInitiator, data: SendData, relays?: string[] | undefined }[] = []
|
||||
private log = getLogger({ component: "nostrSender" })
|
||||
|
||||
AttachNostrSend(nostrSend: NostrSend) {
|
||||
this._nostrSend = nostrSend
|
||||
this.isReady = true
|
||||
this.onReadyCallbacks.forEach(cb => cb())
|
||||
this.onReadyCallbacks = []
|
||||
this.pendingSends.forEach(send => this._nostrSend(send.initiator, send.data, send.relays))
|
||||
this.pendingSends = []
|
||||
}
|
||||
OnReady(callback: () => void) {
|
||||
if (this.isReady) {
|
||||
|
|
@ -17,8 +23,9 @@ export class NostrSender {
|
|||
}
|
||||
}
|
||||
Send(initiator: SendInitiator, data: SendData, relays?: string[] | undefined) {
|
||||
if (!this._nostrSend) {
|
||||
throw new Error("No nostrSend attached")
|
||||
if (!this.isReady) {
|
||||
this.log("tried to send before nostr was ready, caching request")
|
||||
this.pendingSends.push({ initiator, data, relays })
|
||||
}
|
||||
this._nostrSend(initiator, data, relays)
|
||||
}
|
||||
|
|
|
|||
Loading…
Add table
Add a link
Reference in a new issue