lamassu-server/lib/route-helpers.js
Josh Harvey e24da06a1b WIPP
2016-12-08 23:06:48 +02:00

35 lines
960 B
JavaScript

const R = require('ramda')
const dbm = require('./postgresql_interface')
const T = require('./time')
const TRANSACTION_EXPIRATION = 2 * T.days
function stateChange (deviceId, deviceTime, rec) {
const event = {
id: rec.uuid,
deviceId: deviceId,
eventType: 'stateChange',
note: JSON.stringify({state: rec.state, isIdle: rec.isIdle, txId: rec.txId}),
deviceTime: deviceTime
}
return dbm.machineEvent(event)
}
function fetchPhoneTx (phone) {
return dbm.fetchPhoneTxs(phone, TRANSACTION_EXPIRATION)
.then(txs => {
const confirmedTxs = txs.filter(tx => R.contains(tx.status, ['instant', 'confirmed']))
if (confirmedTxs.length > 0) {
const maxTx = R.reduce((acc, val) => {
return !acc || val.cryptoAtoms.gt(acc.cryptoAtoms) ? val : acc
}, null, confirmedTxs)
return {tx: maxTx}
}
if (txs.length > 0) return {pending: true}
return {}
})
}
module.exports = {stateChange, fetchPhoneTx}