write data after invoice is paid

This commit is contained in:
hatim boufnichel 2020-12-30 19:08:14 +01:00
parent 0c5b0feb8b
commit 7b2bd7c26f

View file

@ -229,12 +229,17 @@ const listenerForAddr = (addr, SEA) => async (order, orderID) => {
// invoices should be settled right away so we can rely on this single // invoices should be settled right away so we can rely on this single
// subscription instead of life-long all invoices subscription // subscription instead of life-long all invoices subscription
/**
* @type {string|null}
*/
let maybePostId = null
if (order.targetType === 'post') { if (order.targetType === 'post') {
const { postID } = order const { postID } = order
maybePostId = postID || null
if (!Common.isPopulatedString(postID)) { if (!Common.isPopulatedString(postID)) {
throw new TypeError(`postID not a a populated string`) throw new TypeError(`postID not a a populated string`)
} }
}
const { r_hash } = invoice const { r_hash } = invoice
// A post tip order lifecycle is short enough that we can do it like this. // A post tip order lifecycle is short enough that we can do it like this.
@ -245,29 +250,37 @@ const listenerForAddr = (addr, SEA) => async (order, orderID) => {
/** /**
* @param {Common.Invoice} invoice * @param {Common.Invoice} invoice
*/ */
const onData = invoice => { const invoiceSubCb = invoice => {
if (invoice.settled) { if (!invoice.settled) {
return
}
if (order.targetType === 'post' && typeof maybePostId === 'string') {
getUser() getUser()
.get('postToTipCount') .get('postToTipCount')
.get(postID) .get(maybePostId)
.set(null) // each item in the set is a tip .set(null) // each item in the set is a tip
stream.off()
} }
const coordinate = 'lnPub + invoiceIndex + payment hash(?)' //....
const orderData = {
someInfo: 'info '
}
getUser()
.get('orders')
.get(coordinate)
.set(orderData) // each item in the set is a tip
} }
stream.on('data', onData) stream.on('data', invoiceSubCb)
stream.on('status', (/** @type {any} */ status) => { stream.on('status', (/** @type {any} */ status) => {
logger.info(`Post tip, post: ${postID}, invoice status:`, status) logger.info(`${r_hash}, invoice status:`, status)
}) })
stream.on('end', () => { stream.on('end', () => {
logger.warn(`Post tip, post: ${postID}, invoice stream ended`) logger.warn(`${r_hash}, invoice stream ended`)
}) })
stream.on('error', (/** @type {any} */ e) => { stream.on('error', (/** @type {any} */ e) => {
logger.warn(`Post tip, post: ${postID}, error:`, e) logger.warn(`${r_hash}, error:`, e)
}) })
}
logger.info(`[PERF] Added invoice to GunDB in ${invoicePutEndTime}ms`) logger.info(`[PERF] Added invoice to GunDB in ${invoicePutEndTime}ms`)