MCPcopy Create free account
hub / github.com/cameri/nostream / onSchedule

Method onSchedule

src/app/maintenance-worker.ts:123–192  ·  view source on GitHub ↗
()

Source from the content-addressed store, hash-verified

121 }
122
123 private async onSchedule(): Promise<void> {
124 const currentSettings = this.settings()
125 const clearOldEventsPromise = this.clearOldEventsSafely()
126
127 await this.processNip05Reverifications(currentSettings)
128
129 if (!path(['payments', 'enabled'], currentSettings)) {
130 await clearOldEventsPromise
131 return
132 }
133
134 const invoices = await this.paymentsService.getPendingInvoices()
135 logger('found %d pending invoices', invoices.length)
136 const delay = () => delayMs(100 + Math.floor(Math.random() * 10))
137
138 let successful = 0
139
140 for (const invoice of invoices) {
141 try {
142 logger('getting invoice %s from payment processor: %o', invoice.id, invoice)
143 const updatedInvoice = await this.paymentsService.getInvoiceFromPaymentsProcessor(invoice)
144 await delay()
145 logger('updating invoice status %s: %o', updatedInvoice.id, updatedInvoice)
146
147 if (typeof updatedInvoice.id !== 'string' || typeof updatedInvoice.status !== 'string') {
148 continue
149 }
150 const { id, status } = updatedInvoice
151
152 await this.paymentsService.updateInvoiceStatus({ id, status })
153
154 if (
155 invoice.status !== updatedInvoice.status &&
156 updatedInvoice.status == InvoiceStatus.COMPLETED &&
157 updatedInvoice.confirmedAt
158 ) {
159 logger('confirming invoice %s & notifying %s', invoice.id, invoice.pubkey)
160
161 const update = pipe(
162 mergeDeepLeft(updatedInvoice),
163 mergeDeepLeft({ amountPaid: invoice.amountRequested }),
164 )(invoice)
165
166 await Promise.all([
167 this.paymentsService.confirmInvoice(update),
168 this.paymentsService.sendInvoiceUpdateNotification(update),
169 ])
170
171 await delay()
172 }
173 successful++
174 } catch (error) {
175 if (isNotFoundError(error) && isExpiredInvoice(invoice)) {
176 logger('marking expired invoice %s after payment processor returned 404', invoice.id)
177 await this.paymentsService.updateInvoiceStatus({
178 id: invoice.id,
179 status: InvoiceStatus.EXPIRED,
180 })

Callers 2

runMethod · 0.95

Calls 9

clearOldEventsSafelyMethod · 0.95
isExpiredInvoiceFunction · 0.90
isNotFoundErrorFunction · 0.85
getPendingInvoicesMethod · 0.65
updateInvoiceStatusMethod · 0.65
confirmInvoiceMethod · 0.65

Tested by

no test coverage detected