onOrders job

This commit is contained in:
Daniel Lugo 2020-01-13 16:03:36 -04:00
parent 3b7b5d345c
commit 93960e9317
3 changed files with 109 additions and 20 deletions

View file

@ -0,0 +1,18 @@
/**
* @format
* Jobs are subscriptions to events that perform actions (write to GUN) on
* response to certain ways events can happen. These tasks need to be fired up
* at app launch otherwise certain features won't work as intended. Tasks should
* ideally be idempotent, that is, if they were to be fired up after a certain
* amount of time after app launch, everything should work as intended. For this
* to work, special care has to be put into how these respond to events. These
* tasks accept factories that are homonymous to the events on this same module.
*/
const onAcceptedRequests = require('./onAcceptedRequests')
const onOrders = require('./onOrders')
module.exports = {
onAcceptedRequests,
onOrders
}

View file

@ -1,24 +1,15 @@
/**
* @prettier
* Taks are subscriptions to events that perform actions (write to GUN) on
* response to certain ways events can happen. These tasks need to be fired up
* at app launch otherwise certain features won't work as intended. Tasks should
* ideally be idempotent, that is, if they were to be fired up after a certain
* amount of time after app launch, everything should work as intended. For this
* to work, special care has to be put into how these respond to events. These
* tasks could be hardcoded inside events but then they wouldn't be easily
* auto-testable. These tasks accept factories that are homonymous to the events
* on the same
* @format
*/
const ErrorCode = require('./errorCode')
const Key = require('./key')
const Schema = require('./schema')
const Utils = require('./utils')
const ErrorCode = require('../errorCode')
const Key = require('../key')
const Schema = require('../schema')
const Utils = require('../utils')
/**
* @typedef {import('./SimpleGUN').GUNNode} GUNNode
* @typedef {import('./SimpleGUN').ISEA} ISEA
* @typedef {import('./SimpleGUN').UserGUNNode} UserGUNNode
* @typedef {import('../SimpleGUN').GUNNode} GUNNode
* @typedef {import('../SimpleGUN').ISEA} ISEA
* @typedef {import('../SimpleGUN').UserGUNNode} UserGUNNode
*/
/**
@ -148,6 +139,4 @@ const onAcceptedRequests = async (user, SEA) => {
})
}
module.exports = {
onAcceptedRequests
}
module.exports = onAcceptedRequests

View file

@ -0,0 +1,82 @@
/**
* @format
*/
const ErrorCode = require('../errorCode')
const Key = require('../key')
const Schema = require('../schema')
const Utils = require('../utils')
/**
* @typedef {import('../SimpleGUN').GUNNode} GUNNode
* @typedef {import('../SimpleGUN').ListenerData} ListenerData
* @typedef {import('../SimpleGUN').ISEA} ISEA
* @typedef {import('../SimpleGUN').UserGUNNode} UserGUNNode
*/
let currentOrderAddr = ''
/**
* @param {string} addr
* @param {UserGUNNode} user
* @param {ISEA} SEA
* @returns {(order: ListenerData, orderID: string) => void}
*/
const listenerForAddr = (addr, user, SEA) => async (order, orderID) => {
if (addr !== currentOrderAddr) {
return
}
if (!Schema.isOrder(order)) {
throw new Error(`Expected an order instead got: ${JSON.stringify(order)}`)
}
const senderEpub = await Utils.pubToEpub(order.from)
const secret = await SEA.secret(senderEpub, user._.sea)
const amount = Number(await SEA.decrypt(order.amount, secret))
const memo = await SEA.decrypt(order.memo, secret)
// create invoice here..
const invoice = `INVOICE_${amount}_${memo}`
const encInvoice = await SEA.encrypt(invoice, secret)
user
.get(Key.ORDER_TO_RESPONSE)
.get(orderID)
.put(encInvoice, ack => {
if (ack.err) {
console.error(`error saving order response: ${ack.err}`)
}
})
}
/**
* @param {UserGUNNode} user
* @param {GUNNode} gun
* @param {ISEA} SEA
* @throws {Error} NOT_AUTH
* @returns {void}
*/
const onOrders = (user, gun, SEA) => {
if (!user.is) {
throw new Error(ErrorCode.NOT_AUTH)
}
user.get(Key.CURRENT_ORDER_ADDRESS).on(addr => {
if (typeof addr !== 'string') {
throw new TypeError('Expected current order address to be an string')
}
currentOrderAddr = addr
gun
.get(Key.ORDER_NODES)
.get(addr)
.map()
.on(listenerForAddr(currentOrderAddr, user, SEA))
})
}
module.exports = onOrders